Fix Active Dref Operations List n+1 query issue (active-dref)#2759
Open
szabozoltan69 wants to merge 1 commit into
Open
Fix Active Dref Operations List n+1 query issue (active-dref)#2759szabozoltan69 wants to merge 1 commit into
szabozoltan69 wants to merge 1 commit into
Conversation
c116213 to
941909b
Compare
susilnem
requested changes
Jun 4, 2026
Comment on lines
+355
to
+359
| Prefetch( | ||
| "dreffinalreport", | ||
| queryset=DrefFinalReport.objects.all(), | ||
| to_attr="prefetched_final_report", | ||
| ), |
Member
There was a problem hiding this comment.
This is 1:1 Field, So no need to Prefetch DrefFinalReport.
| filterset_class = ActiveDrefFilterSet | ||
| queryset = ( | ||
| Dref.objects.prefetch_related("planned_interventions", "needs_identified", "national_society_actions", "users") | ||
| Dref.objects.select_related("country") |
Member
There was a problem hiding this comment.
Suggested change
| Dref.objects.select_related("country") | |
| Dref.objects.select_related("country", "dreffinalreport",) |
| return sum(1 for op in prefetched if op.status != Dref.Status.APPROVED) | ||
| return DrefOperationalUpdate.objects.filter(dref_id=obj.id).exclude(status=Dref.Status.APPROVED).count() | ||
|
|
||
| def get_unpublished_final_report_count(self, obj) -> int: |
Member
There was a problem hiding this comment.
Do we need this field? Given that DrefFinalReport is a OneToOneField, the count can only ever be 0 or 1, so it doesn't seem to provide much additional information.
If it's required for backward compatibility, we could annotate it in the queryset instead of calculating it in the serializer.
| return None | ||
| return MiniDrefFinalReportActiveSerializer(final_report).data | ||
|
|
||
| def get_has_ops_update(self, obj) -> bool: |
Member
There was a problem hiding this comment.
We can shift most of these checks on the query level.
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.
Refers to Sentry issue /organizations/ifrc-go/issues/6882.
Maybe these changes makes the code too unreadable, and the benefit is not so much. Should we go on with merge?