perf: back delivery list reads with a composite (status, created_at_chain) index- - #169
Merged
Conversation
DeliveryRepository.list filters deliveries by status (and optionally sender/recipient/driver address) but orders by createdAtChain desc, a shape the four single-column indexes could not serve from one index — Postgres filtered the matched rows then sorted them in a second pass on every read. The indexer is the table's only writer and growth is unbounded, so this degrades monotonically as deliveries accumulate. Back the query with deliveries(status, created_at_chain desc) so the planner resolves the predicate and the order from a single index, and drop the single-column deliveries(status) index it fully supersedes (it is covered as the composite's leading column) to avoid duplicate write/maintenance cost. sender_address/recipient_address/driver_address indexes stay: they are filter-only columns with no covering composite. Verified against a Dockerized Postgres with ~60k seeded deliveries via EXPLAIN ANALYZE: the filter resolves through deliveries_status_created_at_chain_idx, and the paginated shape (LIMIT/cursor) becomes a sort-free ordered index scan. The unbounded no-take query still sorts all matches, which is inherent to missing pagination and tracked separately (GitHub fanilabs#19); this change is not a regression there and unlocks the sort-free path once a take lands. Also documents the full read-path index rationale (notifications, audit_logs, deliveries, escrows, driver_profiles) in docs/DATABASE.md. Generated with Codebuff 🤖 Co-Authored-By: Codebuff <noreply@codebuff.com>
perf: back delivery list reads with a composite (status, created_at_chain) index
|
@micmusjnr20 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Closes #104
Closes #105
Closes #106
Problem
DeliveryRepository.list(src/modules/deliveries/infrastructure/prisma-delivery-repository.ts) filters on one column (status, optionallysender_address/recipient_address/driver_address) but sorts on another (created_at_chain DESC). Thedeliveriestable had only four single-column indexes and no composite covering filter-plus-sort, so Postgres matched rows then ran a second-passSorton every read. Because the blockchain indexer is the table's only writer and growth is unbounded, this degrades monotonically as deliveries accumulate.This closes the last open case from the filter-and-sort indexing audit — the
notifications,audit_logs,escrows, anddriver_profilescomposites (older tickets #46/#101) are already present in the current tree.Change
deliveries(status, created_at_chain desc)so the planner resolves the predicate and the order from a single index.deliveries(status)index it fully supersedes (covered as the composite's leading column) — removes duplicate write/maintenance cost.sender_address/recipient_address/driver_addressindexes: filter-only columns with no covering composite.docs/DATABASE.md(missing until now).Verification
Migration
20260830183747_delivery_filter_sort_indexapplies cleanly andprisma migrate statusreports no drift. Verified withEXPLAIN ANALYZEagainst a Dockerized Postgres seeded to ~60k deliveries:deliveries_status_idxis gone;deliveries_status_created_at_chain_idxpresent.Index Cond: status = ...).ORDER BY created_at_chain DESC+LIMIT) becomes a sort-free ordered Index Scan:takequery (current query — missing pagination, GitHubGET /api/v1/deliveriesreturns the entire deliveries table, unauthenticated and unpaginated #19) still sorts all matches because it returns every match; that is inherent to no pagination and not a regression from the previous bitmap-on-status+ sort plan. This composite lands the sort-free path the moment atake/cursor lands.Testing
pnpm typecheck: identical count of pre-existing errors tomain— zero new.pnpm test: unit suite shows no regression (unrelated pre-existing failures onmain— indexer fakeReferenceError, notifications env typing; DB/Redis-dependent flakiness).Notes (out of scope)
prisma/seed.tshas a pre-existingprisma.escrow.upsertamount(BigInt vs Decimal) failure unrelated to this change — flagged so it isn't mistaken for a regression.fanilabs/backendupstream returned a GitHub-side HTTP 500; opening against this fork'smaininstead (same base commit as upstreamfanilabs/backend).