Guardian read-permission filter is slow on large tables: about 66 s per lab-filtered classification export, about 125 ms per variant tag query

Aperta
#2,873 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
django, postgresql, python

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

🤖 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 since date 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, since and 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.
Lingua principale
Python
Stelle
31
Fork
4
Merge medio
4h 52m
PR unite (30g)
183

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SACGF/variantgrid

Tutte le issue di SACGF/variantgrid

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.