Skip to content

snpdb/annotation: fix N+1 queries on the variant details page #1730 - #1732

Merged
davmlaw merged 1 commit into
masterfrom
issue_1730_n_plus_one_snpdb
Aug 14, 2026
Merged

snpdb/annotation: fix N+1 queries on the variant details page #1730#1732
davmlaw merged 1 commit into
masterfrom
issue_1730_n_plus_one_snpdb

Conversation

@davmlaw

@davmlaw davmlaw commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

🤖 Written by Claude.

Addresses part of #1730 — the snpdb/annotation fixes that are just select_related. Split out of #1727; the classification half is in a separate PR.

Variant details page

  • The sample classifications table used latest_for_user, which applies no select_related, so each row refetched Classification — including the wide evidence JSONB — plus lab.
  • The locus counts table fetched Variant rows without their locus/sequence rows, and str(v) then costs three queries each via locus.contig.name, locus.ref.seq and alt.seq.

Transcript table

gene_version loaded lazily over a loop that pulls every transcript version for the variant's gene symbols — often 50-300 rows. Used by the variant page, classification autopopulate and the classification detail view.

Not included

The grid items in the issue (the AlleleLiftover grids, the variant tag can_write) need the pre_render(qs) hook to bulk-load a page's objects into a dict. That is a consistent little pattern rather than a one-line change, so it is better as its own PR than bolted onto this one.

Testing

python3 manage.py test --keepdb snpdb.tests annotation.tests — 551 tests pass.

The variant page sample table refetched Classification (wide evidence JSONB)
and lab per row, and its locus counts table fetched Variant without the
locus/sequence rows that str(v) reaches through to.

The transcript table (variant page, classification autopopulate, classification
detail) loaded gene_version lazily over a loop that pulls every transcript
version for the variant's gene symbols.
@davmlaw
davmlaw merged commit 0fce626 into master Aug 14, 2026
3 checks passed
@davmlaw
davmlaw deleted the issue_1730_n_plus_one_snpdb branch August 21, 2026 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant