Repository navigation
Conversation
?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>
…routes are not mounted Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GET /api/v2/location_findings/?prefetch=location(and any other?prefetch=that resolves to aLocation) ran about three extra queries for every distinct location it prefetched. A page of 250 references to distinct locations ran a few hundred queries.LocationSerializerreturnstagsandinherited_tagsthroughLocation.readable_tags/readable_inherited_tags. Each of those:location_tags_readable, at least one query per row unless the page was annotated first), andtags.all()/inherited_tags.all(), which nothing had prefetched.The locations list view already avoids this with
mark_tags_readableon the page. The prefetch path (_Prefetcher._prefetch_single/_prefetch_many) never did, andLocationhad no entry in_PREFETCH_PREFETCH_RELATED.Fix
Location: ("tags", "inherited_tags")to_PREFETCH_PREFETCH_RELATED. Both prefetch paths already apply it to the authorized objects.mark_tags_readableonce 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_prefetchandtest_dedupe_location_prefetch, 45 tests in all.Pro impact
ProPrefetcheroverrides_prefetch_single/_prefetch_manybut callssuper()in both. It mapsLocationto the OSSLocationSerializer, so Pro gets the fix with no Pro change.Related alert thread: https://defectdojo-workspace.slack.com/archives/C0BK62Q76P8/p1795450855187169
🤖 Generated with Claude Code