From 72cc5d69a45e2e3805796fbc6da529124f5fbb37 Mon Sep 17 00:00:00 2001 From: Christopher-Li <Christopher-Li@users.noreply.github.com> Date: Fri, 9 Feb 2024 13:49:02 -0500 Subject: [PATCH] Optimize funding index update query (#1061) --- .../postgres/src/stores/funding-index-updates-table.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/indexer/packages/postgres/src/stores/funding-index-updates-table.ts b/indexer/packages/postgres/src/stores/funding-index-updates-table.ts index 4d34c69034..dce9a47028 100644 --- a/indexer/packages/postgres/src/stores/funding-index-updates-table.ts +++ b/indexer/packages/postgres/src/stores/funding-index-updates-table.ts @@ -193,9 +193,17 @@ export async function findFundingIndexMap( options, ); + // Assuming block time of 1 second, this should be 4 hours of blocks + const FOUR_HOUR_OF_BLOCKS = Big(3600).times(4); const fundingIndexUpdates: FundingIndexUpdatesFromDatabase[] = await baseQuery .distinctOn(FundingIndexUpdatesColumns.perpetualId) .where(FundingIndexUpdatesColumns.effectiveAtHeight, '<=', effectiveBeforeOrAtHeight) + // Optimization to reduce number of rows needed to scan + .where( + FundingIndexUpdatesColumns.effectiveAtHeight, + '>', + Big(effectiveBeforeOrAtHeight).minus(FOUR_HOUR_OF_BLOCKS).toFixed(), + ) .orderBy(FundingIndexUpdatesColumns.perpetualId) .orderBy(FundingIndexUpdatesColumns.effectiveAtHeight, Ordering.DESC) .returning('*');