Skip to content

perf: avoid loading org unit group members for OUG{} indicator counts (2.42.5.1)#24402

Open
enricocolasante wants to merge 1 commit into
patch/2.42.5.1from
perf-getmetadata-oug-member-count-2.42.5.1
Open

perf: avoid loading org unit group members for OUG{} indicator counts (2.42.5.1)#24402
enricocolasante wants to merge 1 commit into
patch/2.42.5.1from
perf-getmetadata-oug-member-count-2.42.5.1

Conversation

@enricocolasante

Copy link
Copy Markdown
Contributor

Backport of #24260

…#24260)

* perf: avoid loading org unit group members for OUG{} indicator counts

substituteIndicatorExpressions (called on every dhis-web-dataentry getMetaData request) resolved each OUG{uid} indicator reference via group.getMembers().size(), which initialises the entire org unit group members collection - on a large instance, 70k+ OrganisationUnit entities hydrated into the session per request.

This dominated getMetaData: a ~5s floor and spikes to 14-22s, from the members load, a single-OU rehydration N+1 when the entity cache was cold, and commit-time dirty-checking of the hydrated entities (~2GB allocated per request). Measured floor on a large production instance dropped from ~5.2s to ~0.2s.

Resolve the count with a count subquery instead: new OrganisationUnitGroupStore.getOrganisationUnitGroupMemberCounts(uids) uses HQL size(g.members), which never initialises the collection; substituteIndicatorExpressions looks up only the referenced groups and substitutes counts directly.

Also mark substituteIndicatorExpressions @transactional(readOnly = true): it only mutates the transient Indicator.exploded* display fields, so it never writes; readOnly sets flush mode MANUAL as a guard against dirty-checking.

Behaviour preserved: the count is the global group membership total, as before. The runtime, org-unit-scoped OUG{} count used in analytics is a separate path via orgUnitCountMap.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test: assert OUG{} substitution output is unchanged end-to-end

Integration test exercising the real substituteIndicatorExpressions against Postgres: asserts the exploded numerator/denominator equal String.valueOf(group.getMembers().size()) - i.e. byte-identical to the pre-change behaviour - while the count now comes from the count query.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor: address SonarQube smells in OUG count fix

- collect referenced org unit group UIDs with a stream instead of a separate loop, leaving a single loop over indicators (keeps the single batched count query)

- drop redundant Mockito eq() matcher in the substitution unit test

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs: reword OUG count comment to avoid SonarQube commented-code false positive

java:S125 flagged the explanatory comment as commented-out code because it contained code-like tokens (method-call and brace syntax). Reworded as plain prose; no behaviour change.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@enricocolasante enricocolasante marked this pull request as ready for review July 9, 2026 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants