perf(db): composite indexes for every list-query pattern#70
Merged
Conversation
Every `GET /v1/<entity>/bycompany|bycustomer|byvendor|byheader|byjob/:id`
endpoint runs the same query shape:
SELECT ... WHERE "<parent>Id" = ? AND "<arch>" = false
ORDER BY ... LIMIT ?;
Without an index on `(<parent>Id, <arch>)` that's a sequential
scan against a growing table. TimeEntry already has these
indexes (added in setup/TimeEntry.sql). This migration adds the
same pattern to the other 13 entities:
Direct-compId scoping:
Customer, Worker, BillingType, InventoryItem,
PurchaseOrderVendors, InventoryTransactions
Customer-scoped:
Job, Invoice, CustomerPayment
Header/vendor/job-scoped:
PurchaseOrderHeaders (povId), PurchaseOrderLines (polpoh),
InvoiceJob (invId), ProductEntry (jobId, penArch typo)
All `CREATE INDEX IF NOT EXISTS` so operators who've added some
of these by hand don't trip. The down() reverses with
`DROP INDEX IF EXISTS` (covers a partial-failed up).
Verified locally against the running compose Postgres: migration
applies in 45ms, all 13 indexes present in `\\di dbo.*`.
Suite: 261 / 261 + 4 integration skipped (no app-code changes).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every
bycompany/bycustomer/byvendor/byheader/byjobendpoint runsWHERE <parent>Id = ? AND <arch> = false. TimeEntry already had composite indexes for this; the other 13 entities were sequential-scanning. Migration20260517100000-list-query-indexesadds matchingIF NOT EXISTSindexes everywhere.Test plan
\di dbo.*confirms all 13 new indexesDROP INDEX IF EXISTSIndex list
Customer_compId_arch_idxWorker_compId_arch_idxBillingType_compId_arch_idxInventoryItem_compId_arch_idxPOVendors_compId_arch_idxInventoryTransactions_compId_arch_idxJob_custId_arch_idxInvoice_custId_arch_idxCustomerPayment_custId_arch_idxPOHeaders_povId_arch_idxPOLines_pohId_arch_idxInvoiceJob_invId_arch_idxProductEntry_jobId_arch_idxProudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/