|
| 1 | +# Database Indexes |
| 2 | + |
| 3 | +This document records non-obvious composite indexes and the query shapes they |
| 4 | +are intended to support. |
| 5 | + |
| 6 | +## Dashboard Composites |
| 7 | + |
| 8 | +The dashboard and export paths filter by tenant/campaign and status before |
| 9 | +ordering or constraining by creation time. Single-column indexes on `orgId`, |
| 10 | +`campaignId`, `status`, or `createdAt` do not give PostgreSQL the same ordered |
| 11 | +access path as a composite index with equality filters first and the ordered |
| 12 | +column last. |
| 13 | + |
| 14 | +| Model | Index | Query shape | |
| 15 | +| ------------ | ------------------------------------------ | ----------------------------------------------------------------------------------------------- | |
| 16 | +| `Claim` | `@@index([campaignId, status])` | Claims within a campaign filtered by status. | |
| 17 | +| `Campaign` | `@@index([orgId, status, createdAt])` | Organization dashboard/export queries filtered by `orgId` and `status`, ordered by `createdAt`. | |
| 18 | +| `AidPackage` | `@@index([campaignId, status, createdAt])` | Package dashboard queries filtered by `campaignId` and `status`, ordered by `createdAt`. | |
| 19 | + |
| 20 | +## CI EXPLAIN Check |
| 21 | + |
| 22 | +For PostgreSQL-backed CI environments, verify the dashboard plan with |
| 23 | +`EXPLAIN ANALYZE` after migrations are applied. The expected plan should use an |
| 24 | +index scan or bitmap index scan over the matching composite index, not a |
| 25 | +sequential scan. |
| 26 | + |
| 27 | +```sql |
| 28 | +EXPLAIN ANALYZE |
| 29 | +SELECT id, name, status, "createdAt" |
| 30 | +FROM "Campaign" |
| 31 | +WHERE "orgId" = 'org_test' |
| 32 | + AND status = 'active' |
| 33 | +ORDER BY "createdAt" DESC |
| 34 | +LIMIT 50; |
| 35 | + |
| 36 | +EXPLAIN ANALYZE |
| 37 | +SELECT id, status, "createdAt" |
| 38 | +FROM "AidPackage" |
| 39 | +WHERE "campaignId" = 'campaign_test' |
| 40 | + AND status = 'active' |
| 41 | +ORDER BY "createdAt" DESC |
| 42 | +LIMIT 50; |
| 43 | + |
| 44 | +EXPLAIN ANALYZE |
| 45 | +SELECT id, status |
| 46 | +FROM "Claim" |
| 47 | +WHERE "campaignId" = 'campaign_test' |
| 48 | + AND status = 'approved' |
| 49 | +LIMIT 50; |
| 50 | +``` |
0 commit comments