perf: skip DISTINCT ON for enrolledAt order on onlyEnrollOnce programs DHIS2-20863#22953
Merged
perf: skip DISTINCT ON for enrolledAt order on onlyEnrollOnce programs DHIS2-20863#22953
Conversation
2d11780 to
08e45cc
Compare
|
muilpp
approved these changes
Feb 13, 2026
enricocolasante
approved these changes
Feb 13, 2026
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.

When ordering tracked entities by
enrolledAt, the query usesDISTINCT ON (trackedentityid)to deduplicate rows from the enrollment JOIN (a TE can have multiple enrollments in the same program). This requires sorting all enrollment rows before applying LIMIT, which can be millions.Programs with
onlyEnrollOnce = trueguarantee at most one enrollment per TE, making DISTINCT ON unnecessary. Skipping it lets PostgreSQL apply LIMIT early via a simple JOIN instead of sorting everything first.Also consolidates duplicate event filter logic:
addEventExistsForEnrollmentJoinandaddEventFiltershared the same conditions (event status, program stage, assigned user, deleted). Extracted into a singleaddEventFilterConditionsmethod used by both the EXISTS subquery path and the enrollment JOIN path.When does this apply?
Only when all three conditions are met:
order=enrolledAtis requestedprogramis specified (already required fororder=enrolledAt)onlyEnrollOnce = trueonlyEnrollOnceprograms are fairly common in practice -- child programmes, immunization programs and similar where a person is enrolled once but can have many repeatable stages. Programs withonlyEnrollOnce = falseare unaffected and still useDISTINCT ON.SQL
Before (
DISTINCT ON, two sorts):After (plain JOIN, single sort, LIMIT pushdown):
Database
Sierra Leone DB with 10M tracked entities (10.9M enrollments). EXPLAIN ANALYZE, 4 warmup runs.
DISTINCT ON forces two full sorts spilling to disk: first sort 10.9M enrollment rows by
trackedentityidfor deduplication, then re-sort the deduplicated result byenrollmentdate.Without it, PG uses nested loop index lookups with a single sort and can stop early via LIMIT.