Skip to content

Use existence queries for org group membership checks - #261

Merged
norkans7 merged 2 commits into
mainfrom
org-perm-queries
Jul 27, 2026
Merged

Use existence queries for org group membership checks#261
norkans7 merged 2 commits into
mainfrom
org-perm-queries

Conversation

@norkans7

@norkans7 norkans7 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Org.get_user_org_group checked membership with user in queryset, which loads the entire administrators/editors/viewers member lists — and it runs on every request via the context processor and permission checks, so large orgs loaded thousands of User rows per request.

Membership is now checked with .filter(id=user.id).exists(), with identical return values, and the computed group is cached on the user object (mirroring the existing _org pattern) so the repeated per-request calls only pay for the lookup once.

The test asserts the SQL shape of the membership checks — each must be an existence query (SELECT 1 AS ...) that doesn't hydrate user columns — which fails against the old implementation, unlike a plain query count. It also covers the anonymous user case and verifies cached lookups issue no queries.

norkans7 added 2 commits July 27, 2026 13:34
Org.get_user_org_group previously used 'user in queryset' checks which
load all members of each role; with large orgs this fetched thousands of
user rows on every request via the context processor and permission
checks. Replace with .filter(id=...).exists() lookups and add a query
count test.
Cache the computed org group on the user object so repeated calls within
a request (context processor plus permission checks) only query once.
Replace the query count test with assertions on the SQL shape of the
membership checks, since counting queries alone did not distinguish
existence queries from fetching full member lists. Also cover the
anonymous user case.
@norkans7
norkans7 merged commit 69c26c3 into main Jul 27, 2026
14 checks passed
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