Skip to content

[ENG-1509] Fix /v4/fills/parentSubaccount query performance (backport #3279)#3280

Closed
mergify[bot] wants to merge 1 commit intorelease/indexer/v9.5.xfrom
mergify/bp/release/indexer/v9.5.x/pr-3279
Closed

[ENG-1509] Fix /v4/fills/parentSubaccount query performance (backport #3279)#3280
mergify[bot] wants to merge 1 commit intorelease/indexer/v9.5.xfrom
mergify/bp/release/indexer/v9.5.x/pr-3279

Conversation

@mergify
Copy link
Contributor

@mergify mergify bot commented Dec 10, 2025

Problem

The /v4/fills/parentSubaccountNumber endpoint was timing out for users with large fill histories. Root cause: using IN (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:

  1. Fetch ALL fills for ALL child subaccounts
  2. Sort the entire result set
  3. Only then apply the LIMIT

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:

Approach Execution Time Rows Scanned Pages Read
IN (subquery) 195s 5,306,867 962,324
IN (Explicit UUIDs) 280ms ~13,807 ~2,500
Screenshot 2025-12-10 at 12 21 01 PM

With the "subaccountId" IN (Explicit UUIDs) approach,
Screenshot 2025-12-10 at 12 22 09 PM

Summary by CodeRabbit

  • Refactor

    • Replaced subquery-based subaccount filtering with pre-resolved subaccount ID filtering for faster, more efficient retrieval.
  • New Features

    • Added a public subaccount ID lookup to resolve related subaccounts for a given parent.
  • Chores

    • CI workflow updated to trigger builds for an additional development branch.

✏️ 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).

@mergify mergify bot requested a review from a team as a code owner December 10, 2025 22:05
@linear
Copy link

linear bot commented Dec 10, 2025

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 10, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

1 participant