perf(d1): index pruning and failed send queries

Pruning and failure views should not scan all historical rows.
Index the high-value access paths without adding a write-heavy
index for infrequent delivery-detail lookups.
This commit is contained in:
翅膀 committed 2026-10-23 23:22:14 +08:00
1 parent b71cd7b6bb
commit a98ff778a2
1 file changed
+19
+19
View File
@@ -0,0 +1,19 @@
-- Every index also costs a D1 row write whenever its columns are written, so
-- only indexes whose reads would otherwise cost far more are added here:
-- delivery state changes a few times per delivery, the failure indexes are
-- written for failed sends only, and the audit index replaces another.
-- The storage-prune task (every 5 minutes) deletes delivery state by age:
-- an index on updated_at keeps it from scanning the whole table each run.
CREATE INDEX IF NOT EXISTS idx_delivery_state_updated_at ON delivery_state (updated_at);
-- The metrics page lists the latest failed sends: failures are rare, so
-- without these (partial, failures only) indexes the query walks every
-- successful row in time order before finding them.
CREATE INDEX IF NOT EXISTS idx_send_logs_failed_ts ON send_logs (ts DESC) WHERE ok = 0;
CREATE INDEX IF NOT EXISTS idx_send_logs_failed_group_ts ON send_logs (group_id, ts DESC) WHERE ok = 0;
-- A group's audit log is read newest first: (group_id, ts) serves it in order
-- (and every lookup by group_id), so the single-column index is replaced.
CREATE INDEX IF NOT EXISTS idx_audit_group_ts ON audit_logs (group_id, ts DESC);
DROP INDEX IF EXISTS idx_audit_group;