-
-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(optimizer): Avoid merging prefetches when using aliases #698
Conversation
Merging querysets is usually fine, but when using an alias it might be that one queryset is filtering for something, and the other is filtering for something else, for the same field. That can lead to wrong results being returned. From now on, if a field is specified more than once with an alias, the optimizer will skip it, possibly causing n+1 issues, but avoiding wrong results (which is worse). Fix #695
Reviewer's Guide by SourceryThis pull request modifies the query optimizer to avoid merging prefetches when using aliases. This prevents incorrect results when filtering on the same field with different aliases, but may lead to n+1 issues in some cases. Sequence diagram for query optimization with aliasessequenceDiagram
participant Client
participant Optimizer
participant QuerySet
Client->>Optimizer: Execute query with aliases
Note over Optimizer: Check field selection count
alt Field has multiple selections (aliases)
Optimizer-->>QuerySet: Skip optimization for field
Note over QuerySet: May cause N+1 queries
else Field has single selection
Optimizer->>QuerySet: Apply optimization
Note over QuerySet: Optimized query execution
end
QuerySet-->>Client: Return results
Flow diagram for field optimization decisionflowchart TD
A[Start Field Processing] --> B{Field count > 1?}
B -->|Yes| C[Skip Optimization]
B -->|No| D[Apply Optimization]
C --> E[Add parent relation fields]
D --> E
E --> F[Return Optimized Store]
style C fill:#ffcccc
style D fill:#ccffcc
File-Level Changes
Assessment against linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @bellini666 - I've reviewed your changes and they look great!
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #698 +/- ##
==========================================
- Coverage 89.05% 88.12% -0.93%
==========================================
Files 42 42
Lines 3837 3850 +13
==========================================
- Hits 3417 3393 -24
- Misses 420 457 +37 ☔ View full report in Codecov by Sentry. |
Edit: |
Merging querysets is usually fine, but when using an alias it might be that one queryset is filtering for something, and the other is filtering for something else, for the same field. That can lead to wrong results being returned.
From now on, if a field is specified more than once with an alias, the optimizer will skip it, possibly causing n+1 issues, but avoiding wrong results (which is worse).
Fix #695
Summary by Sourcery
Fix issue where using aliases with the same field could lead to incorrect results. Skip prefetching for fields specified multiple times with aliases to prevent this issue, which may cause n+1 problems.
Bug Fixes:
Tests: