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 N+1 queries in /api/v1/patient/ #1337

Open
Tracked by #1333
sainak opened this issue Jun 2, 2023 · 4 comments
Open
Tracked by #1333

Fix N+1 queries in /api/v1/patient/ #1337

sainak opened this issue Jun 2, 2023 · 4 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@sainak
Copy link
Member

sainak commented Jun 2, 2023

Sentry Issue: CARE-NF

Transaction Name /api/v1/patient/
Parent Span django.view - patientregistration-list
Repeating Spans (0) db - SELECT hcx_policy.id, hcx_policy.external_id, hcx_policy.created_date, hcx_policy.modified_date, hcx_policy.deleted, hcx_policy.patient_id, hcx_policy.subscriber_id, hcx_policy.policy_id, hcx_policy.insurer_id, hcx_policy.insurer_name, hcx_policy.status, hcx_policy.priority, hcx_policy.purpose, hcx_policy.outcome, hcx_policy.error_text, hcx_policy.created_by_id, hcx_policy.last_modified_by_id FROM hcx_policy WHERE (hcx_policy."delete...
db - SELECT COUNT(*) AS __count FROM facility_patientregistration WHERE (facility_patientregistration.deleted = %s AND facility_patientregistration.facility_id = %s AND facility_patientregistration.is_active = %s)
db - SELECT COUNT(*) AS __count FROM facility_bed WHERE (facility_bed.deleted = %s AND facility_bed.facility_id = %s)
@sainak sainak mentioned this issue Jun 2, 2023
7 tasks
@sainak sainak added the good first issue Good for newcomers label Jun 6, 2023
@satyam0601
Copy link

i would like to work on this can you assign me this issue?

@rithviknishad
Copy link
Member

Hey @satyam0601
Any updates on this? Any blockers?

@ClawedCatalyst
Copy link

ClawedCatalyst commented Feb 23, 2024

hey,
@sainak @rithviknishad I want to work on this issue.
Can you please assign this issue to me?

@ClawedCatalyst
Copy link

Hey @sainak @rithviknishad,

I believe the primary issue with the hcx_policy query comes from https://github.com/coronasafe/care/blob/411e89c63c7daa4b13cc76a1754debec9c19bea7/care/facility/api/serializers/patient.py#L93

    def get_has_eligible_policy(self, patient):
        eligible_policies = Policy.objects.filter(
            (Q(error_text="") | Q(error_text=None)),
            outcome="complete",
            patient=patient.id,
        )
        return bool(len(eligible_policies))

in the patient serializer.

To address this, we can utilize prefetch_related and then filter the patient object with the necessary policy set. Additionally, I suggest replacing len with count, as both accomplish the same task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants