Skip to content

Commit

Permalink
Optimize funding index update query (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Li authored Feb 9, 2024
1 parent 2657b35 commit 72cc5d6
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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('*');
Expand Down

0 comments on commit 72cc5d6

Please sign in to comment.