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

Optimize the N+1 query occurrence in the /api/v1/facility/ endpoint to improve performance and reduce database load. #2256

Closed
gigincg opened this issue Jun 9, 2024 · 1 comment

Comments

@gigincg
Copy link
Member

gigincg commented Jun 9, 2024

Feature Request

Description
Optimize the N+1 query occurrence in the /api/v1/facility/ endpoint to improve performance and reduce database load.

  • ViewSet File: care/facility/api/viewsets/facility.py
  • Serializer: care/facility/api/serializers/facility.py

Suggested Solution
To address the N+1 query problem, use Django's select_related and prefetch_related methods:

  1. Use select_related: For single-valued relationships (e.g., foreign keys, one-to-one relationships) to perform SQL joins and fetch related objects in a single query.
  2. Use prefetch_related: For multi-valued relationships (e.g., many-to-many, reverse foreign keys) to fetch related objects in separate queries and join them in Python.

Additional Context
Optimizing these queries should enhance the endpoint's performance, reduce the number of database queries, and lower the overall load on the database.

dhruv-goyal-10 added a commit to dhruv-goyal-10/care that referenced this issue Jun 10, 2024
Annotated the count of beds & patients in queryset

Fixes ohcnetwork#2256.
@rithviknishad
Copy link
Member

Closing as duplicate and new solution proposed in #2481

@rithviknishad rithviknishad closed this as not planned Won't fix, can't repro, duplicate, stale Sep 22, 2024
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 a pull request may close this issue.

2 participants