Skip to content

Commit

Permalink
Merge pull request #315 from stellar/fix-effects-query
Browse files Browse the repository at this point in the history
Fix slow effects page query
  • Loading branch information
nullstyle authored Feb 15, 2018
2 parents ad8a2e5 + 79f2809 commit b577268
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions services/horizon/internal/db2/history/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,28 @@ func (q *EffectsQ) Page(page db2.PageQuery) *EffectsQ {
idx = math.MaxInt32
}

// NOTE: Remember to test the queries below with EXPLAIN / EXPLAIN ANALYZE
// before changing them.
// This condition is using multicolumn index and it's easy to write it in a way that
// DB will perform a full table scan.
switch page.Order {
case "asc":
q.sql = q.sql.
Where(`(
heff.history_operation_id > ?
OR (
heff.history_operation_id = ?
AND heff.order > ?
))`, op, op, idx).
heff.history_operation_id >= ?
AND (
heff.history_operation_id > ? OR
(heff.history_operation_id = ? AND heff.order > ?)
))`, op, op, op, idx).
OrderBy("heff.history_operation_id asc, heff.order asc")
case "desc":
q.sql = q.sql.
Where(`(
heff.history_operation_id < ?
OR (
heff.history_operation_id = ?
AND heff.order < ?
))`, op, op, idx).
heff.history_operation_id <= ?
AND (
heff.history_operation_id < ? OR
(heff.history_operation_id = ? AND heff.order < ?)
))`, op, op, op, idx).
OrderBy("heff.history_operation_id desc, heff.order desc")
}

Expand Down

0 comments on commit b577268

Please sign in to comment.