Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
urtho opened this issue Feb 4, 2024 · 1 comment
Closed
Labels
new-feature-request Feature request that needs triage

Comments

@urtho
Copy link
Contributor

urtho commented Feb 4, 2024

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)
@gmalouf
Copy link
Contributor

gmalouf commented Jun 6, 2024

Addressed in #1615 - thanks @urtho !

@gmalouf gmalouf closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-feature-request Feature request that needs triage
Projects
None yet
Development

No branches or pull requests

2 participants