Skip to content

Commit

Permalink
fix: enable optimisation only when pageSize is one
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Salaün <[email protected]>
  • Loading branch information
altitude committed Feb 6, 2024
1 parent 4f4f849 commit 1551d38
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions pkg/storage/sqlstorage/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,24 @@ func (s *Store) buildTransactionsQuery(flavor Flavor, p ledger.TransactionsQuery
if !endTime.IsZero() {
sb.Where(sb.L("timestamp", endTime.UTC()))

// We nudge the query planner in the right direction,
// by reducing the search space according to the end time.
// We have to use a raw query as the sqlbuilder
// does not support LTE+subqueries in the where clause.
sb.SQL(fmt.Sprintf(`
AND "id" <= (
SELECT "id"
FROM "%s".transactions
WHERE "timestamp" < '%s'::timestamptz
ORDER BY "timestamp" DESC, "id" DESC
LIMIT 1
)`,
s.schema.Name(),
endTime.UTC().Format(time.RFC3339),
))
if p.PageSize == 1 {
// We nudge the query planner in the right direction,
// by reducing the search space according to the end time.
// We have to use a raw query as the sqlbuilder
// does not support LTE+subqueries in the where clause.
sb.SQL(fmt.Sprintf(`
AND "id" <= (
SELECT "id"
FROM "%s".transactions
WHERE "timestamp" < '%s'::timestamptz
ORDER BY "timestamp" DESC, "id" DESC
LIMIT 1
)`,
s.schema.Name(),
endTime.UTC().Format(time.RFC3339),
))
}

t.EndTime = endTime
}

Expand Down

0 comments on commit 1551d38

Please sign in to comment.