Skip to content

Commit 80a77d4

Browse files
authored
Revert "[ENG-1509] Fix /v4/fills/parentSubaccount query performance (… (#3288)
1 parent 3ff1e43 commit 80a77d4

File tree

2 files changed

+6
-39
lines changed

2 files changed

+6
-39
lines changed

indexer/packages/postgres/src/stores/fill-table.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { knexReadReplica } from '../helpers/knex';
1111
import { setupBaseQuery, verifyAllRequiredFields } from '../helpers/stores-helpers';
1212
import Transaction from '../helpers/transaction';
1313
import { getUuid } from '../helpers/uuid';
14+
import { getSubaccountQueryForParent } from '../lib/parent-subaccount-helpers';
1415
import FillModel from '../models/fill-model';
1516
import {
1617
FillColumns,
@@ -30,7 +31,6 @@ import {
3031
QueryableField,
3132
QueryConfig,
3233
} from '../types';
33-
import { findIdsForParentSubaccount } from './subaccount-table';
3434

3535
export function uuid(eventId: Buffer, liquidity: Liquidity): string {
3636
// TODO(IND-483): Fix all uuid string substitutions to use Array.join.
@@ -147,11 +147,10 @@ export async function findAll(
147147
if (subaccountId !== undefined) {
148148
baseQuery = baseQuery.whereIn(FillColumns.subaccountId, subaccountId);
149149
} else if (parentSubaccount !== undefined) {
150-
// PERFORMANCE CRITICAL: Resolve subaccountIds to concrete UUIDs before querying.
151-
// Using IN (subquery) causes Postgres to misestimate cardinality and scan millions
152-
// of rows. With explicit UUIDs, Postgres uses optimal index scans per subaccount.
153-
const subaccountIds = await findIdsForParentSubaccount(parentSubaccount);
154-
baseQuery = baseQuery.whereIn(FillColumns.subaccountId, subaccountIds);
150+
baseQuery = baseQuery.whereIn(
151+
FillColumns.subaccountId,
152+
getSubaccountQueryForParent(parentSubaccount),
153+
);
155154
}
156155

157156
if (side !== undefined) {

indexer/packages/postgres/src/stores/subaccount-table.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IndexerSubaccountId } from '@dydxprotocol-indexer/v4-protos';
22
import { PartialModelObject, QueryBuilder } from 'objection';
33

44
import config from '../config';
5-
import { BUFFER_ENCODING_UTF_8, DEFAULT_POSTGRES_OPTIONS, MAX_PARENT_SUBACCOUNTS } from '../constants';
5+
import { BUFFER_ENCODING_UTF_8, DEFAULT_POSTGRES_OPTIONS } from '../constants';
66
import {
77
verifyAllRequiredFields,
88
setupBaseQuery,
@@ -201,35 +201,3 @@ export async function deleteById(
201201
Transaction.get(options.txId),
202202
).deleteById(id);
203203
}
204-
205-
/**
206-
* Retrieves all subaccount IDs associated with a parent subaccount.
207-
* A subaccount is considered a child of the parent if it has the same address
208-
* and its subaccount number follows the modulo relationship with the parent.
209-
*
210-
* @param parentSubaccount The parent subaccount object with address and subaccountNumber
211-
* @param options Query options including transaction ID
212-
* @returns A promise that resolves to an array of subaccount ID strings
213-
*/
214-
export async function findIdsForParentSubaccount(
215-
parentSubaccount: {
216-
address: string,
217-
subaccountNumber: number,
218-
},
219-
options: Options = DEFAULT_POSTGRES_OPTIONS,
220-
): Promise<string[]> {
221-
// Get all subaccounts for the address
222-
const subaccounts = await findAll(
223-
{ address: parentSubaccount.address },
224-
[],
225-
options,
226-
);
227-
228-
// Filter for subaccounts that match the parent relationship
229-
// (subaccountNumber - parentSubaccountNumber) % MAX_PARENT_SUBACCOUNTS = 0
230-
return subaccounts
231-
.filter((subaccount) => (subaccount.subaccountNumber - parentSubaccount.subaccountNumber) %
232-
MAX_PARENT_SUBACCOUNTS === 0,
233-
)
234-
.map((subaccount) => subaccount.id);
235-
}

0 commit comments

Comments
 (0)