care
care copied to clipboard
fix N+1 queries in /api/v1/resource/
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
Can you add proof as well, the queries executed before vs after ?
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"
),
)
)
The implementation for counts is to be switched to redis, closing this MR as a part of that effort.