Release 1.143.4#3428
Merged
Merged
Conversation
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
OpenAPI ChangesShow/hide ## Changes for v0.yaml:Unexpected changes? Ensure your branch is up-to-date with |
Comment on lines
+154
to
+158
| redemption_date = str(discount.contract_redemptions.last().created_on) | ||
|
|
||
| if discount.contract_redemptions.exists(): | ||
| redeemed_by = discount.contract_redemptions.last().user.email | ||
| redeemed_on = str(discount.contract_redemptions.last().created_on) |
There was a problem hiding this comment.
Bug: The _get_discount_cells method causes an N+1 query issue by repeatedly querying the database inside a loop, bypassing the prefetch_related cache with .count() and .last() calls.
Severity: HIGH
Suggested Fix
Refactor the logic to utilize the prefetched data. Instead of calling .count(), use the attach_redemption_count annotation that is already available. Access the prefetched contract_redemptions list in memory to check for existence and retrieve the last item, avoiding new database queries inside the loop.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: b2b/sheets.py#L154-L158
Potential issue: The `_get_discount_cells` method introduces an N+1 query performance
issue. Within a loop that iterates over discount codes, several database queries are
executed for each code using methods like `discount.contract_redemptions.count()`,
`discount.contract_redemptions.last()`, and `discount.contract_redemptions.exists()`.
These calls bypass the existing `prefetch_related` cache, leading to a large number of
unnecessary database queries. For a contract with 100 learners, this could result in
approximately 600 extra queries, causing significant slowdowns in asynchronous tasks and
increased database load.
Did we get this right? 👍 / 👎 to inform future reviews.
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.
Chris Chudzicki
James Kachel
annagav