Skip to content

Commit 43b8aeb

Browse files
Optimize funding index update query (#1061)
1 parent 08fb729 commit 43b8aeb

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

indexer/packages/postgres/src/stores/funding-index-updates-table.ts

+8
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,17 @@ export async function findFundingIndexMap(
193193
options,
194194
);
195195

196+
// Assuming block time of 1 second, this should be 4 hours of blocks
197+
const FOUR_HOUR_OF_BLOCKS = Big(3600).times(4);
196198
const fundingIndexUpdates: FundingIndexUpdatesFromDatabase[] = await baseQuery
197199
.distinctOn(FundingIndexUpdatesColumns.perpetualId)
198200
.where(FundingIndexUpdatesColumns.effectiveAtHeight, '<=', effectiveBeforeOrAtHeight)
201+
// Optimization to reduce number of rows needed to scan
202+
.where(
203+
FundingIndexUpdatesColumns.effectiveAtHeight,
204+
'>',
205+
Big(effectiveBeforeOrAtHeight).minus(FOUR_HOUR_OF_BLOCKS).toFixed(),
206+
)
199207
.orderBy(FundingIndexUpdatesColumns.perpetualId)
200208
.orderBy(FundingIndexUpdatesColumns.effectiveAtHeight, Ordering.DESC)
201209
.returning('*');

0 commit comments

Comments
 (0)