Skip to content

use not exists for orphaned persons query#419

Merged
ericbuckley merged 1 commit into
mainfrom
feature/optimize-orphaned-persons
Jun 10, 2025
Merged

use not exists for orphaned persons query#419
ericbuckley merged 1 commit into
mainfrom
feature/optimize-orphaned-persons

Conversation

@ericbuckley

@ericbuckley ericbuckley commented Jun 9, 2025

Copy link
Copy Markdown
Collaborator

Description

Speed optimizations to the GET /api/person/orphaned endpoint 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

SELECT mpi_person.id, mpi_person.reference_id
FROM mpi_person
LEFT OUTER JOIN mpi_patient ON mpi_patient.person_id = mpi_person.id
WHERE mpi_patient.id IS NULL

✅ Optimized Query

SELECT id, reference_id
FROM mpi_person
WHERE NOT EXISTS (
    SELECT 1
    FROM mpi_patient
    WHERE mpi_patient.person_id = mpi_person.id
)

Using a database with 1 million person clusters and ~8 million patient records. Calls to GET /api/person/orphaned averaged 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:

  • I have ensured that the pull request is of a manageable size, allowing it to be reviewed within a single session.
  • I have reviewed my changes to ensure they are clear, concise, and well-documented.
  • I have updated the documentation, if applicable.
  • I have added or updated test cases to cover my changes, if applicable.
  • I have minimized the number of reviewers to include only those essential for the review.

Checklist for Reviewers

Please review and complete the following checklist during the review process:

  • The code follows best practices and conventions.
  • The changes implement the desired functionality or fix the reported issue.
  • The tests cover the new changes and pass successfully.
  • Any potential edge cases or error scenarios have been considered.

@ericbuckley ericbuckley self-assigned this Jun 9, 2025
@ericbuckley ericbuckley marked this pull request as ready for review June 9, 2025 21:17
@codecov

codecov Bot commented Jun 9, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.51%. Comparing base (f6ef4bf) to head (b1b262a).
Report is 3 commits behind head on main.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ericbuckley ericbuckley merged commit 4a24cb2 into main Jun 10, 2025
17 checks passed
@ericbuckley ericbuckley deleted the feature/optimize-orphaned-persons branch June 10, 2025 14:04
@ericbuckley ericbuckley added this to the v25.8.0 milestone Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants