-
Notifications
You must be signed in to change notification settings - Fork 342
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
Fixed N+1 queries issue in /api/v1/bed/ #1908
Conversation
@sainak @rithviknishad @vigneshhari Can you please review this PR? |
hey @dhruv-goyal-10 thanks for the pr, so we can just annotate the queryset with is_occupied directly if |
Removed is_occupied property from Bed model as it is not used anywhere Annotated is_occupied field in the queryset instead of _is_occupied
@sainak @rithviknishad @vigneshhari I have completed the changes as suggested by @sainak. Can you review my PR now? |
Changed is_occupied from SerializerMethodField to Boolean Field
Hey @sainak! I have done changes suggested by you. Can you please have a look at my PR now? |
@sainak I later realised this, commenting out the is_occupied property will although not affect the results in case of list method, but it will affect the result in case of retrieve method (fetching a particular object using id), coz we aren't annotating is_occupied property in that case and it will always corresponds to False (the default value). |
Currently we are not using bed retrieve on Fe but you are correct that will give inconsistent results, It won't affect the performance so lets remove the if condition |
Annotated is_occupied field irrespective of action
@sainak Made the changes as suggested by you. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1908 +/- ##
==========================================
+ Coverage 61.94% 61.99% +0.04%
==========================================
Files 221 221
Lines 12168 12168
Branches 1735 1735
==========================================
+ Hits 7538 7543 +5
+ Misses 4324 4316 -8
- Partials 306 309 +3 ☔ View full report in Codecov by Sentry. |
@sainak Do I need to write test cases for this PR to get merged? |
Yes! |
@sainak I have added the test case for this as suggested by you, Please have a look now! |
* Fixed N+1 queries issue in /api/v1/bed/ * Feat: PR review changes for issue #1338 Removed is_occupied property from Bed model as it is not used anywhere Annotated is_occupied field in the queryset instead of _is_occupied * Feat: PR review changes Changed is_occupied from SerializerMethodField to Boolean Field * Feat: PR Review Changes Annotated is_occupied field irrespective of action * Added the test case for listing beds API * add comment explaining number of queries --------- Co-authored-by: Aakash Singh <mail@singhaakash.dev>
Proposed Changes
is_occupied is the property defined in the Bed Model, which was contributing to N + 1 queries issue, and I have annotated the attribute _is_occupied in the queryset, which basically corresponds to the same value as the is_occupied property in Bed Model. I have handled the various attribute cases, in BedSerializer, by defining is_occupied field as SerializerMethodField, hence there will be no change in API response.
Have added location__facility in the select_related field, as this was also contributing to the N + 1 queries issue.
Associated Issue
/api/v1/bed/
#1338Merge Checklist
/docs
Only PR's with test cases included and passing lint and test pipelines will be reviewed
@coronasafe/care-backend-maintainers @coronasafe/care-backend-admins