perf: eliminate program join from enrollment count query DHIS2-20921#22972
Merged
perf: eliminate program join from enrollment count query DHIS2-20921#22972
Conversation
When a specific program is provided, resolve its ID in Java and filter with e.programid = :programId instead of joining the program table and filtering on p.uid/p.type. The program type check is already validated by EnrollmentOperationParamsMapper before reaching the store. This removes one inner join from the count query for the common case (program specified). The data query still joins program for SELECT columns.
59af617 to
df5a8f3
Compare
…20921 When the program is known and its access level is OPEN, AUDITED, or CLOSED, the trackedentity join is unnecessary in the count query. The join was only needed for the PROTECTED temp owner check in the ownership clause, which references te.trackedentityid. For tracked entity UID filters, uses a subquery instead of a join to avoid hashing all 10M rows.
df5a8f3 to
7f492da
Compare
|
muilpp
approved these changes
Feb 17, 2026
enricocolasante
approved these changes
Feb 17, 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.



Follow-up to #22970. Optimizes enrollment queries when a program is provided.
When a program is given, both data and count queries replace
p.type = 'WITH_REGISTRATION' and p.uid = :programUidwithe.programid = :programId. Thep.typecheck is safe to drop becauseEnrollmentOperationParamsMappervalidates the program is atracker program. The count query additionally drops the
programandtrackedentityjoins (thedata query still needs them for SELECT columns). When a
trackedEntityUID filter is provided inthe count query without the
trackedentityjoin, a subquery is used instead(
e.trackedentityid in (select trackedentityid from trackedentity where uid in (...))).SQL
Data query (when program is specified) -- joins unchanged, WHERE simplified:
Count query (when program is specified) --
programandtrackedentityjoins removed:Database Performance
Sierra Leone DB with 10M tracked entities (10.9M enrollments). EXPLAIN ANALYZE on the generated SQL
queries, 4 warmup runs. All
/enrollmentsrequests useprogram=ur1Edk5Oe2n&pageSize=3. Usertypes: unmarked = normal user (restricted org unit scope, 2 facilities), admin = search-all
authority with root org unit, super = superuser.
fields=enrollmentfields=enrollment(admin)fields=enrollment(super)orgUnitMode=DESCENDANTS(admin)orgUnitMode=SELECTED(admin)orgUnitMode=ALL(admin)status=ACTIVE(admin)followUp=true(admin)enrolledAfter&enrolledBefore(admin)updatedAfter(admin)updatedWithin=30d(admin)trackedEntity=<uid>(admin)enrollments=<uid>,<uid>(admin)includeDeleted=true(admin)order=enrolledAt:descorder=enrolledAt:desc(admin)order=enrolledAt:desc(super)order=createdAt:desc(admin)order=completedAt:desc(admin)order=updatedAt:descorder=updatedAt:desc(admin)totalPages=truecount (admin)fields=*(admin)orgUnitMode=DESCENDANTS&status=ACTIVE&enrolledAfter&order=enrolledAt:desc(admin)status=ACTIVE&followUp=true&fields=*(admin)/trackedEntities?fields=enrollments(admin)* orgUnitMode=SELECTED returns 0 rows due to hierarchy level mismatch (pre-existing).
The multi-second times on order-by and totalPages queries come from the admin user's root org unit
scope matching all ~10.9M enrollments with no filters. The database must sort or count all of them.
Normal users scoped to a few facilities see sub-second times. The remaining bottleneck is missing
indexes on
enrolledAt,createdAt, andcompletedAt-- unlike/trackedEntitieswhereenrolledAtlives in a different table, here all orderable fields are on theenrollmenttableand indexable.
The
order=enrolledAt:descregression for the normal user (+116%, no filters besides ownership) isa plan change: PostgreSQL picks a parallel Gather Merge with the simpler WHERE instead of a nested
loop. Both are sub-second (398ms vs 859ms). The same user with
order=updatedAt:desc(indexed)sees 48ms with no regression -- the index avoids the full sort entirely.
The last row shows the enrollment query triggered by
/trackedEntities?program=ur1Edk5Oe2n&orgUnit=ImspTQPwCqd&pageSize=3&fields=enrollments. Knownclients (web capture app, Android SDK) never call
/enrollmentsas a list endpoint. They fetchenrollments embedded in tracked entity responses, which runs the enrollment query filtered by
tracked entity UIDs (9ms, high selectivity).