Add endpoint to return orphaned persons#225
Merged
Merged
Conversation
…ature/163-manage-orphan-data
…ature/163-get-orphaned-patients
…m/CDCgov/RecordLinker into feature/163-get-orphaned-persons
…ature/163-get-orphaned-persons
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #225 +/- ##
==========================================
+ Coverage 97.69% 97.72% +0.02%
==========================================
Files 32 32
Lines 1651 1672 +21
==========================================
+ Hits 1613 1634 +21
Misses 38 38 ☔ View full report in Codecov by Sentry. |
ericbuckley
reviewed
Feb 27, 2025
ericbuckley
previously approved these changes
Feb 27, 2025
ericbuckley
left a comment
Collaborator
There was a problem hiding this comment.
Looks great @m-goggins. Thanks for wrapping up all the MPIAPI work!
ericbuckley
reviewed
Feb 28, 2025
ericbuckley
approved these changes
Feb 28, 2025
bamader
pushed a commit
that referenced
this pull request
Mar 19, 2025
## Description The PR adds an endpoint to return a paginated list of all the persons with no members. While working on this PR I also made some small changes to `get_orphaned_patients`: - added an explicit `ORDER_BY` after discovering some inconsistent results when I included a `limit` but not a `cursor`. I also updated the tests accordingly. - genericized `schemas.PaginatedRefs` so that the paginated results for both get orphaned patients and persons are the same. ## Related Issues #163 ## Additional Notes I spent a good amount of time thinking about how we should execute this query to make it as efficient as possible. My working assumptions were that: 1) the patient table will be larger than the person table, 2) both tables will be large, and 3) orphaned persons will be relatively rare. I considered two approaches, "LEFT JOIN" (which I ultimately landed on) and "NOT EXISTS", but I am open to hearing others. LEFT JOIN approach: ``` SELECT p.* FROM mpi_person p LEFT JOIN mpi_patient pt ON pt.person_id = p.id WHERE pt.id IS NULL ``` From `EXPLAIN QUERY PLAN`, we can see that we are scanning `mpi_person`, using a Bloom filter when scanning the larger patient table to more quickly eliminate rows that definitely do not match any rows in Person, and uses a covering index on `mpi_patient.id`, which should keep things as quick as possible. NOT EXISTS approach: ``` SELECT * FROM mpi_person p WHERE NOT EXISTS ( SELECT 1 FROM mpi_patient pt WHERE pt.person_id = p.id ) ``` This approach is less efficient because of the subquery executes multiple times checking for matching rows in Patient for each row in Person. It would be more efficient if we add an index on Patient.person_id, but I still think LEFT JOIN is a better choice given the assumptions stated above (especially #1). I also briefly considered a view, but given the number of updates to the Patient and Person tables, I don't think this is our best path forward. All that said, I am open to other approaches and would love to get folks' thoughts. --------- Co-authored-by: Eric Buckley <eric.buckley@gmail.com>
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
The PR adds an endpoint to return a paginated list of all the persons with no members.
While working on this PR I also made some small changes to
get_orphaned_patients:ORDER_BYafter discovering some inconsistent results when I included alimitbut not acursor. I also updated the tests accordingly.schemas.PaginatedRefsso that the paginated results for both get orphaned patients and persons are the same.Related Issues
#163
Additional Notes
I spent a good amount of time thinking about how we should execute this query to make it as efficient as possible. My working assumptions were that:
I considered two approaches, "LEFT JOIN" (which I ultimately landed on) and "NOT EXISTS", but I am open to hearing others.
LEFT JOIN approach:
From
EXPLAIN QUERY PLAN, we can see that we are scanningmpi_person, using a Bloom filter when scanning the larger patient table to more quickly eliminate rows that definitely do not match any rows in Person, and uses a covering index onmpi_patient.id, which should keep things as quick as possible.NOT EXISTS approach:
This approach is less efficient because of the subquery executes multiple times checking for matching rows in Patient for each row in Person. It would be more efficient if we add an index on Patient.person_id, but I still think LEFT JOIN is a better choice given the assumptions stated above (especially #1).
I also briefly considered a view, but given the number of updates to the Patient and Person tables, I don't think this is our best path forward.
All that said, I am open to other approaches and would love to get folks' thoughts.
<--------------------- 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: