use not exists for orphaned persons query#419
Merged
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #419 +/- ##
=======================================
Coverage 98.51% 98.51%
=======================================
Files 33 33
Lines 1948 1950 +2
=======================================
+ Hits 1919 1921 +2
Misses 29 29 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
bamader
approved these changes
Jun 10, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Speed optimizations to the
GET /api/person/orphanedendpoint that results in ~17x improvement in query time.Additional Notes
Below is a comparison of the existing and optimized queries used to retrieve orphaned person clusters, along with the performance improvements achieved. The original query performed a join between every row in mpi_person and mpi_patient, which triggered a costly sequential scan of the entire mpi_patient table. In contrast, the optimized query uses a NOT EXISTS condition, allowing PostgreSQL to efficiently leverage indexes and quickly identify persons without any associated patient records.
🔄 Existing Query
✅ Optimized Query
Using a database with 1 million person clusters and ~8 million patient records. Calls to
GET /api/person/orphanedaveraged around 6.3s with the existing query. Using the optimized query, those dropped to around 370ms.<--------------------- REMOVE THE LINES BELOW BEFORE MERGING --------------------->
Checklist
Please review and complete the following checklist before submitting your pull request:
Checklist for Reviewers
Please review and complete the following checklist during the review process: