[ENG-1509] Fix /v4/fills/parentSubaccount query performance (backport #3279)#3280
Closed
mergify[bot] wants to merge 1 commit intorelease/indexer/v9.5.xfrom
Closed
[ENG-1509] Fix /v4/fills/parentSubaccount query performance (backport #3279)#3280mergify[bot] wants to merge 1 commit intorelease/indexer/v9.5.xfrom
mergify[bot] wants to merge 1 commit intorelease/indexer/v9.5.xfrom
Conversation
(cherry picked from commit c275378)
Contributor
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
/v4/fills/parentSubaccountNumberendpoint was timing out for users with large fill histories. Root cause: usingIN (subquery)to find fills across parent + child subaccounts caused Postgres to misestimate row counts and choose a catastrophically inefficient query plan—scanning millions of rows instead of using optimal index scans.Why this happens:
When Postgres plans a query with
IN (SELECT ...), it must estimate the subquery's cardinality before execution. For our subaccount lookup, it estimated ~314 rows when the actual result was 5.3M fills (testing with the subaccount causing the issue). This caused Postgres to:With explicit UUIDs, Postgres can use "Index Scan Backward" per subaccount and stop early at the LIMIT.
Fix:
Resolve subaccount IDs to concrete UUIDs in a separate query before the main fills query, replacing the inline subquery.
Testing:
Tested on testnet indexer database (240M fills) against address
dydx1qzq0dx4xjj06d4nxfx7ls53d8wru625s445mhs— with 5.3M fills in subaccount 0.Query performance comparison using
EXPLAIN ANALYZE:With the

"subaccountId" IN (Explicit UUIDs)approach,Summary by CodeRabbit
Refactor
New Features
Chores
✏️ Tip: You can customize this high-level summary in your review settings.
This is an automatic backport of pull request #3279 done by [Mergify](https://mergify.com).