From 8a106506ba4e7a6cac5fcb0e4d8cc92727c93fae Mon Sep 17 00:00:00 2001 From: Gary Malouf <982483+gmalouf@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:50:10 -0400 Subject: [PATCH] Optimize sub-queries for assets/apps against a given address. --- idb/postgres/postgres.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/idb/postgres/postgres.go b/idb/postgres/postgres.go index 0a095e51..8cad4c4d 100644 --- a/idb/postgres/postgres.go +++ b/idb/postgres/postgres.go @@ -1799,13 +1799,28 @@ func (db *IndexerDb) buildAccountQuery(opts idb.AccountQueryOptions, countOnly b whereArgs = append(whereArgs, *opts.AssetLT) partNumber++ } + + // We want to limit the size of the results in this query to what could actually be needed + 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) } // filters against main account table if len(opts.GreaterThanAddress) > 0 {