Skip to content

Commit

Permalink
Implement sort index hints for postgres eventstore
Browse files Browse the repository at this point in the history
  • Loading branch information
IhostVlad committed Jan 18, 2022
1 parent 71e76ad commit bcfde5c
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@ const createLoadQuery = (

if (limit !== undefined) {
return [
`SELECT "unitedEvents".* FROM (
${vectorConditions
.map(
(threadCounter, threadId) =>
`(${[
`SELECT * FROM ${databaseNameAsId}.${eventsTableAsId}`,
`WHERE (${resultQueryCondition}) AND "threadId" = ${threadId} AND "threadCounter" >= ${threadCounter}::${INT8_SQL_TYPE}`,
`LIMIT ${limit}`,
].join(' ')})`
)
.join(' UNION ALL \n')}
) "unitedEvents"`,
`ORDER BY "unitedEvents"."timestamp" ASC, "unitedEvents"."threadCounter" ASC, "unitedEvents"."threadId" ASC`,
`LIMIT ${+limit}`,
`SELECT "sortedEvents".* FROM (`,
` SELECT "unitedEvents".* FROM (`,
vectorConditions
.map(
(threadCounter, threadId) =>
`(${[
`SELECT * FROM ${databaseNameAsId}.${eventsTableAsId}`,
`WHERE (${resultQueryCondition}) AND "threadId" = ${threadId} AND "threadCounter" >= ${threadCounter}::${INT8_SQL_TYPE}`,
`ORDER BY "threadCounter" ASC`,
`LIMIT ${limit}`,
].join(' ')})`
)
.join(' UNION ALL \n'),
` ) "unitedEvents"`,
` ORDER BY "unitedEvents"."timestamp" ASC`,
` LIMIT ${+limit}`,
`) "sortedEvents"`,
`ORDER BY "sortedEvents"."threadCounter" ASC,`,
`"sortedEvents"."threadId" ASC`,
].join('\n')
} else {
const resultVectorConditions = `${vectorConditions
Expand Down

0 comments on commit bcfde5c

Please sign in to comment.