Fix N+1 query issue in releases field#3767
Fix N+1 query issue in releases field#3767HarshitVerma109 wants to merge 12 commits intoOWASP:mainfrom
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughReplace RepositoryNode's simple releases access with a Django Prefetch that prefilters releases (no drafts, no pre-releases, non-null published_at, ordered by published_at desc), stores results on Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
1 issue found across 2 files
Confidence score: 3/5
- There is a concrete behavior bug: the
[:RECENT_RELEASES_LIMIT]slice in thePrefetchwill cap releases across all repositories, so multi-repo queries can return too few releases per repo. - Given the severity (8/10) and high confidence, this poses a real user-facing regression risk if the endpoint is used with multiple repositories.
- Pay close attention to
backend/apps/github/api/internal/nodes/repository.py- the global slice inPrefetchlimits releases across all repositories.
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="backend/apps/github/api/internal/nodes/repository.py">
<violation number="1" location="backend/apps/github/api/internal/nodes/repository.py:89">
P1: The slice `[:RECENT_RELEASES_LIMIT]` inside `Prefetch` applies globally, not per repository. When querying multiple repositories, this will return at most 5 releases total across ALL repositories instead of 5 per repository.
Remove the slice from the `Prefetch` queryset and apply it in the resolver instead, similar to how `issues` and `recent_milestones` are implemented in this file.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
@cubic-dev-ai review this pull request |
@HarshitVerma109 I have started the AI code review. It will take a few minutes to complete. |
|
@arkid15r , please review it and let me know if any changes are required. |
ahmedxgouda
left a comment
There was a problem hiding this comment.
Thank you for working on this. We are near.
24de799
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@backend/apps/github/api/internal/nodes/repository.py`:
- Around line 81-96: The Prefetch for Release.objects.filter used in the
strawberry_django.field decorator (with to_attr="prefetched_releases") is
missing prefetching of repository__project_set, causing N+1 queries when
ReleaseNode.project_name is resolved; update the Prefetch queryset (the lambda
returning Prefetch) to include .prefetch_related("repository__project_set") so
that repository__project_set is loaded with the prefetched_releases and the
ReleaseNode.project_name resolver can read project data without extra queries.
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="backend/apps/github/api/internal/nodes/repository.py">
<violation number="1" location="backend/apps/github/api/internal/nodes/repository.py:107">
P2: Fallback query path is missing `select_related`/`prefetch_related` that the `Prefetch` queryset applies. If this fallback is ever reached, it will trigger N+1 queries on `author__owasp_profile`, `repository__organization`, and `repository__project_set` — the same issue this PR is fixing. Consider applying the same optimizations to the fallback queryset.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
@ahmedxgouda, I think the problem is completely solved now. |
|
@HarshitVerma109 |
|
Sorry for the pings! I didn't intend to spam; I was just worried the message might have been missed. I’ll be more patient in the future and wait for your feedback. Thanks for your time! |
|
@HarshitVerma109 It is not about the pings, it is about deleting and posting the same comment. Any way, sorry for the delays. |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3767 +/- ##
=======================================
Coverage 96.34% 96.34%
=======================================
Files 512 512
Lines 15815 15823 +8
Branches 2174 2176 +2
=======================================
+ Hits 15237 15245 +8
Misses 431 431
Partials 147 147
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
|
@arkid15r , please review it and let me know if any changes are required. |
Proposed change
Resolves #3662
Use
Prefetchto fix N+1 query issue in releases field.Checklist
make check-testlocally: all warnings addressed, tests passed