Guardian read-permission filter is slow on large tables: about 66 s per lab-filtered classification export, about 125 ms per variant tag query
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Domain
- backend, databases, performance
Research direction
Start with GuardianPermissionsMixin._object_permission_for_pks_qs in library/django_utils/guardian_permissions_mixin.py and compare it with the get_objects_for_user calls in classification/views/exports/classification_export_filter.py and analysis/models/models_variant_tag.py. Measure the listed classification export filters, Tagged Variants CSV, and grid page as a lab user and superuser. Done means the permission filtering is faster and the exported records and displayed rows remain unchanged.
Written by the indexing model from the issue text.
Description
🤖 Written by Claude
Guardian's read-permission filter (get_objects_for_user) is slow on large tables, and it runs on every query. Two places show it on a test deployment.
1. Classification export filtered to particular labs: about 66 s per export
The older classification export (Classifications → Export and /classification/api/classifications/export) spends about 66 s on one query when filtered to particular labs, before it reads a record. Every export pays it once, and CSV twice (the evidence-column pass reads the records again). It was already there before #2868. #2868 made it run once per batch, which PR #2872 fixes.
Measured (126k classifications, a user in one lab, exporting two labs, 7.6k records):
- the export's id query: 66 s
- the same ids fetched without the filter: 0.1 s
- with no lab filter, or with
exclude_labs: 0.7 s - a
sincedate covering most of the records ran for over 5 minutes in the same query before it was stopped
Cause: ClassificationFilter.cms_qs (classification/views/exports/classification_export_filter.py) calls get_objects_for_user(user, perm, cms, ...). Guardian builds (id)::varchar IN (SELECT object_pk FROM guardian_userobjectpermission ... WHERE object_pk IN (SELECT id::varchar FROM <cms with its filters>)), plus the same for groups. With the lab filter inside it, Postgres plans the user-permission branch as a nested-loop semi join matching object_pk against id::varchar row by row.
2. Variant tags: about 125 ms per query for a lab user
VariantTag permissions come through groups. A lab user here holds 200,042 view_varianttag group permission rows, and every query that applies the filter builds a hash of all of them (hashed SubPlan on guardian's group rows), whatever the query's size.
Measured on the Variants → Tagged Variants grid (200k tags, GRCh37, as a lab user):
- CSV download: each 1000-row batch takes about 195 ms with the grid's filters and 70 ms fetching the same rows without them. That's 41 s for the CSV on master, against 9.5 s before #2868 (which ran the filter once). PR #2872 reads grid CSVs 5000 rows per query, which brings it to 26 s. The rest is this filter, once per batch.
- Grid pages: a page takes 11.8 s for a lab user and 11.2 s for a superuser, who skips the filter. So the filter costs about 0.3 s per page query, but it isn't what makes pages slow. That's a DISTINCT on the count and every display join being computed before the page is chosen, which is #2878.
Existing fix pattern
library/django_utils/guardian_permissions_mixin.py:GuardianPermissionsMixin._object_permission_for_pks_qs already avoids both problems for write permissions. It casts the pk to text and matches guardian's rows as a plain IN on object_pk, restricted to the pks being asked about, which uses the (user|group, permission, object_pk) unique index. A read-permission form of it would:
- in
cms_qs, avoid nesting the filtered queryset inside guardian's subquery; - for a batch or a page, look up only that batch's permission rows rather than hashing all of the user's.
ClassificationModification.filter_for_user, VariantTag's filtering and the other get_objects_for_user callers need measuring individually.
To check
- The classification export id query with
include_labs,exclude_labs,sinceand no filter, before and after. - The Tagged Variants CSV (26 s with PR #2872) and a grid page, as a lab user and as a superuser.
- That the records exported and the rows shown are unchanged.
- 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