Skip to content

Commit aa7dc4e

Browse files
committed
perf(billing): bound cycle close's previous-period lookup below the elapsed period
The covering usage_log period index added in 0441 has unique keys, so B-tree deduplication can't compress an entity's current period the way the old four-column index did, and max(billing_period_start) for the previous period walked every current-period entry first. The elapsed period starts before it ends, so bounding the start below it changes no answer and lets the scan skip the current period.
1 parent d299e7b commit aa7dc4e

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎apps/sim/lib/billing/cycle-close.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,11 @@ export async function closeElapsedBillingPeriod(
373373
and(
374374
eq(usageLog.billingEntityType, billingEntity.type),
375375
eq(usageLog.billingEntityId, billingEntity.id),
376-
eq(usageLog.billingPeriodEnd, periodStart)
376+
eq(usageLog.billingPeriodEnd, periodStart),
377+
// The elapsed period starts before it ends; stating it lets the backward scan of the
378+
// covering period index skip the current period's entries, which its unique keys can't
379+
// deduplicate, instead of walking all of them to reach the previous period.
380+
lt(usageLog.billingPeriodStart, periodStart)
377381
)
378382
)
379383
const expectedPrevStart = prevStamp?.start ?? minusOneInterval(periodStart, sub.billingInterval)

0 commit comments

Comments
 (0)