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/resource/ #2035

Closed
wants to merge 1 commit into from

Conversation

adityasuthar
Copy link

Proposed Changes

Fix the N+1 queries in the resources/ API

Associated Issue

#1336

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

@vigneshhari
Copy link
Member

Can you add proof as well, the queries executed before vs after ?

Copy link
Member

Choose a reason for hiding this comment

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

these will still cause n+1 db queries
use queryset.annotate instead

@adityasuthar
Copy link
Author

adityasuthar commented Apr 26, 2024

Hi, @vigneshhari @sainak I tried to read the DRF docs for serializers and ORM, tried various solutions and finally was able to do it for bed_count, will need your review if this is what we wanted

but I got stuck again when doing the same for patient_count because the query started to give incorrect results when I also annotated the patientregistration table.

ORM started to join the bed table with patientregistration and fetch extra results
maybe we can achieve this by using raw SQL query

Here is the query I tried to replicate the same process for patient_count also

    queryset = (
        ResourceRequest.objects.all()
        .select_related(
            "origin_facility",
            "origin_facility__ward",
            "origin_facility__local_body",
            "origin_facility__district",
            "origin_facility__state",
            "approving_facility",
            "approving_facility__ward",
            "approving_facility__local_body",
            "approving_facility__district",
            "approving_facility__state",
            "assigned_facility",
            "assigned_facility__ward",
            "assigned_facility__local_body",
            "assigned_facility__district",
            "assigned_facility__state",
            "assigned_to",
            "created_by",
            "last_edited_by",
        )
        .annotate(
            origin_facility_bed_count=Count("origin_facility__bed"),
            approving_facility_bed_count=Count("approving_facility__bed"),
            assigned_facility_bed_count=Count("assigned_facility__bed"),
        )
        .annotate(
            origin_facility_patient_count=Count("origin_facility__patientregistration"),
            approving_facility_patient_count=Count(
                "approving_facility__patientregistration"
            ),
            assigned_facility_patient_count=Count(
                "assigned_facility__patientregistration"
            ),
        )
    )

@vigneshhari
Copy link
Member

The implementation for counts is to be switched to redis, closing this MR as a part of that effort.

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.

3 participants