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

Fixed N+1 queries issue in /api/v1/bed/ #1908

Merged
merged 7 commits into from
Mar 4, 2024

Conversation

dhruv-goyal-10
Copy link
Contributor

@dhruv-goyal-10 dhruv-goyal-10 commented Feb 23, 2024

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

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

@dhruv-goyal-10
Copy link
Contributor Author

@sainak @rithviknishad @vigneshhari Can you please review this PR?

@rithviknishad rithviknishad requested review from sainak, vigneshhari and rithviknishad and removed request for sainak February 23, 2024 10:25
@sainak
Copy link
Member

sainak commented Feb 26, 2024

hey @dhruv-goyal-10 thanks for the pr,
after looking at the bed model i can see that the is_occupied property on the model is not being used
https://github.com/coronasafe/care/blob/6cb4b3f26c03bece09f818299f4dccfb321ab86b/care/facility/models/bed.py#L43-L45

so we can just annotate the queryset with is_occupied directly if self.action == list

Removed is_occupied property from Bed model as it is not used anywhere

Annotated is_occupied field in the queryset instead of _is_occupied
@dhruv-goyal-10
Copy link
Contributor Author

@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
@dhruv-goyal-10
Copy link
Contributor Author

Hey @sainak! I have done changes suggested by you. Can you please have a look at my PR now?

@sainak sainak requested a review from Ashesh3 February 26, 2024 18:10
@dhruv-goyal-10
Copy link
Contributor Author

dhruv-goyal-10 commented Feb 26, 2024

@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).
Isn't it?

@sainak
Copy link
Member

sainak commented Feb 26, 2024

@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).
Isn't it?

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

dhruv-goyal-10 and others added 2 commits February 27, 2024 01:05
Annotated is_occupied field irrespective of action
@dhruv-goyal-10
Copy link
Contributor Author

@sainak Made the changes as suggested by you.
Also, thanks for an insightful discussion.

Copy link

codecov bot commented Feb 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.99%. Comparing base (6cb4b3f) to head (a8372ba).
Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

@dhruv-goyal-10
Copy link
Contributor Author

@sainak Do I need to write test cases for this PR to get merged?

@sainak
Copy link
Member

sainak commented Feb 28, 2024

Yes!
For this case create a bunch of records and checked the number of queries executed

@dhruv-goyal-10
Copy link
Contributor Author

@sainak I have added the test case for this as suggested by you, Please have a look now!

@sainak sainak requested review from rithviknishad and Ashesh3 March 1, 2024 04:59
@vigneshhari vigneshhari merged commit 8ca6e55 into ohcnetwork:master Mar 4, 2024
5 checks passed
Ashesh3 pushed a commit that referenced this pull request Mar 5, 2024
* 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>
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.

5 participants