Add partial indexes for authorized finding-list queries#15064
Open
rossops wants to merge 1 commit into
Open
Conversation
Maffooch
approved these changes
Jun 23, 2026
valentijnscholten
approved these changes
Jun 23, 2026
Two partial indexes on dojo_finding backing the heaviest finding-list queries, which combine sparse per-user visibility with ORDER BY/LIMIT: * idx_finding_sev_active (severity, numerical_severity DESC) WHERE active -- severity-filtered lists ordered by severity (e.g. the default active- findings views and dashboard widgets). Replaces a backward numerical_severity scan that filtered millions of rows away with a bounded index range scan. * idx_finding_riskaccepted_date (date DESC) WHERE risk_accepted -- accepted-risks list; replaces a full-table backward scan with a direct ordered scan of just the risk-accepted rows. Built CONCURRENTLY (atomic = False) so the migration takes no ACCESS EXCLUSIVE lock on the large dojo_finding table. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
5a41918 to
5503ec6
Compare
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
Adds two partial indexes on
dojo_findingthat back the heaviest finding-list queries — the ones that combine a filtered finding list withORDER BY … LIMIT/OFFSET:idx_finding_sev_active—(severity, numerical_severity DESC) WHERE activeServes severity-filtered active-finding lists ordered by severity (default finding views, dashboard widgets). Without it,
WHERE severity=? AND active ORDER BY numerical_severity DESC LIMIT … OFFSET …walks thenumerical_severityindex backward and filters millions of rows away.idx_finding_riskaccepted_date—(date DESC) WHERE risk_acceptedServes the accepted-risks list ordered by date.
Both are declared on
Finding.Meta.indexesand created viaAddIndexConcurrently(atomic = False) so the migration takes noACCESS EXCLUSIVElock on the largedojo_findingtable.Why
On a large instance (~10M findings) these list endpoints did full-table-scale work. For the severity-ordered active list:
The accepted-risks list similarly drops from a full-table backward
datescan to a direct ordered scan of the (much smaller) risk-accepted set.WHERE active(rather than a narroweractive AND NOT verified) is deliberate: it also serves the open/unverified variants (severity AND active AND NOT verified) via the same index + a residual filter, so one index covers both rather than maintaining two.How it was tested
On a stage DB (
dojo_finding≈ 10.1M rows / 21 GB):EXPLAIN (ANALYZE, BUFFERS)before/after as above — the severity-ordered active list goes 4,614 ms → 0.19 ms; the accepted-risks list and the open/unverified severityCOUNT(*)likewise become index scans.idx_finding_sev_active≈ 6.6 s / 56 MB;idx_finding_riskaccepted_date≈ 5 s / 2.8 MB.sqlmigrateemitsCREATE INDEX CONCURRENTLY … WHERE …with no transaction wrapping; applied and reversed cleanly against a dev DB;makemigrations --checkreports no drift.Notes
CONCURRENTLYbuild time is dominated by waiting for in-flight transactions to drain (twice); on a busy instance it can take longer than measured here, but never blocks reads or writes.🤖 Generated with Claude Code