Skip to content
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 facility n+1 #2089

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

AnkurPrabhu
Copy link
Contributor

@AnkurPrabhu AnkurPrabhu commented Apr 15, 2024

Proposed Changes

  • Brief of changes made.
    using prefetch related for both the fields (PatientRegistration and Bed)

Associated Issue

fixes: #1920

Architecture changes

  • Remove this section if not used

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Any other necessary step

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@coronasafe/care-backend-maintainers @coronasafe/care-backend-admins

@AnkurPrabhu AnkurPrabhu requested a review from a team as a code owner April 15, 2024 19:17
rithviknishad
rithviknishad previously approved these changes Apr 16, 2024
Copy link
Member

@rithviknishad rithviknishad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

"district",
"state",
)
.prefetch_related("patientregistration_set", "bed_set")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of prefetch try annotating the count using subquery, and in the serializer check if the annotated attribute is present else fallback to filtering

return PatientRegistration.objects.filter(
facility=facility, is_active=True
).count()
return facility.patientregistration_set.count()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return facility.patientregistration_set.count()
if getattr(self, "annotated_count...":
return self.annotated_count...
return facility.patientregistration_set.filter(is_active=True).count()

@AnkurPrabhu
Copy link
Contributor Author

@sainak have resolved your comments pls take a look

@Ashesh3 Ashesh3 requested a review from sainak April 24, 2024 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

N+1 Query /api/v1/facility/
4 participants