care icon indicating copy to clipboard operation
care copied to clipboard

fix N+1 queries in /api/v1/resource/

Open adityasuthar opened this issue 10 months ago • 2 comments

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

adityasuthar avatar Mar 29 '24 18:03 adityasuthar

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

vigneshhari avatar Mar 30 '24 06:03 vigneshhari

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"
            ),
        )
    )

adityasuthar avatar Apr 26 '24 12:04 adityasuthar

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

vigneshhari avatar Sep 22 '24 17:09 vigneshhari