indexer icon indicating copy to clipboard operation
indexer copied to clipboard

Speed up pagination in buildAccountQuery when Asset or App id are provided

Open urtho opened this issue 1 year ago • 0 comments

Problem

Next token is ignored in some CTEs causing full table scans during pagination.

Solution

This patch shows x5 speedup in average query exec time. Not an ultimate solution but a quick win.

--- a/idb/postgres/postgres.go
+++ b/idb/postgres/postgres.go
@@ -1797,13 +1797,25 @@ func (db *IndexerDb) buildAccountQuery(opts idb.AccountQueryOptions, countOnly b
                        whereArgs = append(whereArgs, *opts.AssetLT)
                        partNumber++
                }
+               if len(opts.GreaterThanAddress) > 0 {
+                       aq += fmt.Sprintf(" AND addr > $%d", partNumber)
+                       whereArgs = append(whereArgs, opts.GreaterThanAddress)
+                       partNumber++
+               }
                aq = "qasf AS (" + aq + ")"
                withClauses = append(withClauses, aq)
        }
        if opts.HasAppID != 0 {
-               withClauses = append(withClauses, fmt.Sprintf("qapf AS (SELECT addr FROM account_app WHERE app = $%d)", partNumber))
+               aq := fmt.Sprintf("SELECT addr FROM account_app WHERE app = $%d", partNumber)
                whereArgs = append(whereArgs, opts.HasAppID)
                partNumber++
+               if len(opts.GreaterThanAddress) > 0 {
+                       aq += fmt.Sprintf(" AND addr > $%d", partNumber)
+                       whereArgs = append(whereArgs, opts.GreaterThanAddress)
+                       partNumber++
+               }
+               aq = "qapf AS (" + aq + ")"
+               withClauses = append(withClauses, aq)

urtho avatar Feb 04 '24 20:02 urtho