Skip to content

perf(prefetch): load prefetched locations' tags once per page - #17063

Open
Maffooch wants to merge 2 commits into
devfrom
fix/location-prefetch-tags-n-plus-one
Open

Maffooch wants to merge 2 commits into
devfrom
fix/location-prefetch-tags-n-plus-one

Conversation

@Maffooch

Copy link
Copy Markdown
Contributor

Problem

GET /api/v2/location_findings/?prefetch=location (and any other ?prefetch= that resolves to a Location) ran about three extra queries for every distinct location it prefetched. A page of 250 references to distinct locations ran a few hundred queries.

LocationSerializer returns tags and inherited_tags through Location.readable_tags / readable_inherited_tags. Each of those:

  • checks whether the caller may read the row's shared tag set (location_tags_readable, at least one query per row unless the page was annotated first), and
  • reads tags.all() / inherited_tags.all(), which nothing had prefetched.

The locations list view already avoids this with mark_tags_readable on the page. The prefetch path (_Prefetcher._prefetch_single / _prefetch_many) never did, and Location had no entry in _PREFETCH_PREFETCH_RELATED.

Fix

  • Add Location: ("tags", "inherited_tags") to _PREFETCH_PREFETCH_RELATED. Both prefetch paths already apply it to the authorized objects.
  • In both paths, call mark_tags_readable once on the authorized locations before serializing, so the readability check is one query per page instead of one per location.

The payload is unchanged. No migration.

Tests

unittests/test_apiv2_prefetch_location_tags_queries.py:

  • test_query_count_does_not_grow_with_prefetched_locations: the query count for a page whose references point at 2 locations equals the count for 8 locations. Before the fix it was 12 vs 30.
  • test_prefetched_locations_carry_their_own_tags: each prefetched location still returns its own tags.

The existing prefetch suites also pass locally: test_apiv2_prefetch_locations, _rbac, _reverse_fk, _duplicate_finding, test_api_product_prefetch and test_dedupe_location_prefetch, 45 tests in all.

Pro impact

ProPrefetcher overrides _prefetch_single / _prefetch_many but calls super() in both. It maps Location to the OSS LocationSerializer, so Pro gets the fix with no Pro change.

Related alert thread: https://defectdojo-workspace.slack.com/archives/C0BK62Q76P8/p1795450855187169

🤖 Generated with Claude Code

?prefetch=location serialized each location with LocationSerializer, whose
tags and inherited_tags read Location.readable_tags. The prefetcher loaded
neither tag relation nor the tag-readability check, so each prefetched
location cost three more queries (a page of 250 references to distinct
locations: about 750).

Prefetch tags and inherited_tags for Location like the other prefetched
models, and set the readability flag for the whole page with
mark_tags_readable, as the locations list view already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Maffooch Maffooch added this to the 3.5.400 milestone Nov 24, 2026
…routes are not mounted

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant