perf(issues): Fix N+1 queries in starred views endpoint#112424
Merged
perf(issues): Fix N+1 queries in starred views endpoint#112424
Conversation
The starred views serializer was calling serialize() per item, which triggered separate get_attrs() calls for each starred view — roughly 4N+1 queries for N views. Batch-serialize all views in get_attrs() instead. Also adds prefetch_related for the projects M2M and switches values_list() to .all() so the prefetch cache is actually used. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
wedamija
reviewed
Apr 7, 2026
Comment on lines
+34
to
+40
| starred_views = ( | ||
| GroupSearchViewStarred.objects.filter( | ||
| organization=organization, user_id=request.user.id | ||
| ) | ||
| .select_related("group_search_view") | ||
| .prefetch_related("group_search_view__projects") | ||
| ) |
Member
There was a problem hiding this comment.
I think it might be better to do this in get_attrs instead, so this this is fixed for all callers. You can use prefetch_related_objects to perform on a list of objects and just do it on everything passed to the get_attrs call
Move `prefetch_related` for the projects M2M into `get_attrs` using `prefetch_related_objects` so all callers benefit, not just the starred endpoint. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
markstory
approved these changes
Apr 8, 2026
george-sentry
pushed a commit
that referenced
this pull request
Apr 9, 2026
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.
The starred views serializer was calling
serialize()individually per item, triggering separateget_attrs()calls for each view - roughly 4N+1 queries for N starred views. Saw this endpoint taking 1.3s in production.Batch-serialize all views in a single
get_attrs()call instead. Also addsprefetch_relatedfor the projects M2M and switchesvalues_list()to.all()so Django's prefetch cache is actually used.example trace w/ n+1
transaction summary