Expression index on Sequence length so SV routing by alt length stays a hashed subplan
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 58/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- postgresql, python
- Domain
- database, performance
Research direction
Read annotation/annotation_pipeline_routing.py to locate the routing predicate, then inspect Sequence.Meta.indexes and the migrations that create indexes concurrently. Add an expression index on length(seq) and run ANALYZE on snpdb_sequence; verify with EXPLAIN that the subquery stays a hashed SubPlan and uses a bitmap index scan. The issue reports local measurements but does not name a test file.
Written by the indexing model from the issue text.
Description
🤖 Written by Claude
Follow-up to #1708 (PR #2428), which left open "the cost of the Length(seq) subquery on a database with many long sequences".
Problem
_explicit_annotated_as_structural_q (annotation/annotation_pipeline_routing.py) routes an explicit variant whose alt is long to the SV pipeline with:
alt_id IN (SELECT id FROM snpdb_sequence WHERE length(seq) >= 1000)
It sits inside an OR, so Postgres can't turn it into a join and plans it as a SubPlan. That has two regimes:
- Hashed SubPlan: one sequential scan of
snpdb_sequenceper query (per parallel worker), building a hash of the matching ids. ~20 ms at 100k sequences, ~0.2 s at 1M. Fine. - Materialised SubPlan: there are no statistics for
length(seq), so the planner assumes a third of the rows match. A SubPlan is only hashed whenestimated rows × 32 bytesfits inwork_mem × hash_mem_multiplier(4 MB × 2 on Postgres 15+, × 1 before). Past ~786k sequences (15+) or ~393k (13/14) at default settings the planner stops hashing, materialises the matching ids and scans that list for every variant the OR reaches, i.e. nearly every explicit variant with a short ref.
Measured on a developer database (100k-variant pk slice, 758 sequences ≥ 1 kb, parallelism off, cliff forced with work_mem = 512kB):
| plan | time |
|---|---|
| hashed SubPlan | 40 ms |
| materialised SubPlan | 1,418 ms |
Materialised cost scales as variants × long sequences. With the 11,592 long sequences counted in #1708, a 25k-variant annotation run's query would take ~5 s and anything applying the predicate to a whole build (annotation/backfill_columns.py, a whole-build count) goes from seconds to about an hour at 17M variants. The scheduler runs the predicate at least twice per annotation run (the count in annotation_scheduler_task.py and the dump).
Only a VariantAnnotationVersion with explicit_structural_variant_min_size set uses the predicate, so deployments whose versions predate #2428 aren't affected until the next version is created.
Fix
An expression index on length(seq), with an ANALYZE so the planner gets statistics for the expression. Tested locally:
- Built in 38 ms / 776 kB on 107k rows; on 1M rows it detoasts every sequence once, so expect under a few minutes.
- After ANALYZE the row estimate is 769 rather than 34,752, so the SubPlan stays hashed even at
work_mem = 512kB. - The subquery itself becomes a bitmap index scan: 0.25 ms instead of 20 ms.
Index added as Sequence.Meta.indexes, created concurrently in the migration so VCF import isn't blocked writing sequences, followed by ANALYZE snpdb_sequence.
To check a deployment is over the cliff: EXPLAIN the SV routing query; SubPlan with a Materialize under it, rather than hashed SubPlan, is the slow plan.
- Dominant language
- Python
- Stars
- 31
- Forks
- 4
- Avg merge
- 4h 52m
- Merged PRs (30d)
- 183
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from SACGF/variantgrid
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
SACGF/variantgrid#2964 · 1 comment ·
Maintainers usually reply within 1 day
-
Genome build now duplicated in "imported as" on allele pagePossibly taken @TheMadBug claimed this 3 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
SACGF/variantgrid#2950 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
SACGF/variantgrid#2865 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
SACGF/variantgrid#2647 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
SACGF/variantgrid#2341 · 3 comments ·
Maintainers usually reply within 1 day
All issues in SACGF/variantgrid
Similar issues
-
Test: to_hijri() accepts datetime.datetime and uses only the date partPossibly taken A pull request linked to this issue is open or already merged. Opengood first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 · 3 comments ·
Maintainers usually reply within 1 day
-
polish
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
521xueweihan/HelloGitHub#4729 ·
-
[Bug]: Score parser reads parts of scientific notation as valid scoresPossibly taken @AshutoshSingh840 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
UKGovernmentBEIS/control-arena#992 ·
Maintainers usually reply within 3 days
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
mvalentsev/awesome-free-ai-coding#70 ·
Maintainers usually reply within 1 day