Skip to content

Commit 15de583

Browse files
committed
perf(db): scope the workspace covering index's claim to created_at windows
1 parent f4ba91d commit 15de583

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

‎packages/db/migrations/0446_usage_log_workspace_created_at_cost_idx.sql‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
-- Replaces usage_log_workspace_created_at_idx with a covering index under the same two leading
22
-- columns.
33
--
4-
-- Every Workspaces drill-down read filters on the billing entity, the workspace and a created_at
5-
-- window, and reads cost with user_id, source or workflow_id. The old index held only
4+
-- A Workspaces drill-down over a created_at window (a range, or a reporting period) filters on the
5+
-- billing entity, the workspace and that window, and reads cost with user_id, source or
6+
-- workflow_id. The old index held only
67
-- (workspace_id, created_at), so a drill-down matched the window there or on
78
-- usage_log_billing_entity_created_at_cost_idx and fetched every matched row from the heap to
89
-- check the rest: the workspace's rows on one, the whole organization's on the other. Dropping
910
-- usage_log_billing_entity_period_idx in 0441 took away the bitmap that used to narrow the second.
1011
-- With the entity and every column those reads return in the key, each one is an index-only scan
11-
-- bounded by the workspace's rows in the window.
12+
-- bounded by the workspace's rows in the window. A drill-down over a Stripe period matches the
13+
-- period stamps instead, which this index does not carry, so it keeps the plans it had.
1214
--
1315
-- The new key extends the old one, so it serves every read the old index served in the same
1416
-- order: the workspace_id foreign key's ON DELETE SET NULL, and the ledger list filtered to a

‎packages/db/schema.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6116,10 +6116,11 @@ export const usageLog = pgTable(
61166116
)`
61176117
),
61186118
/**
6119-
* Serves the Workspaces drill-down index-only: every analytics read narrowed to
6120-
* one workspace (entity + `workspace_id` + a `created_at` window) reads its
6121-
* totals, series, and member, source and workflow rankings from here, and skips
6122-
* the workspace's rows billed to another entity without a heap fetch.
6119+
* Serves the Workspaces drill-down index-only: an analytics read narrowed to one
6120+
* workspace over a `created_at` window (entity + `workspace_id` + the window)
6121+
* reads its totals, series, and member, source and workflow rankings from here,
6122+
* and skips the workspace's rows billed to another entity without a heap fetch.
6123+
* A Stripe-period drill-down matches the period stamps, which are not here.
61236124
*
61246125
* `workspace_id` and `created_at` lead so the scan is bounded by the
61256126
* workspace's rows in the window rather than its organization's, and so the

0 commit comments

Comments
 (0)