Skip to content

perf: eliminate program table join in single event queries [DHIS2-20891]#23047

Merged
enricocolasante merged 2 commits intomasterfrom
DHIS2-20891
Feb 26, 2026
Merged

perf: eliminate program table join in single event queries [DHIS2-20891]#23047
enricocolasante merged 2 commits intomasterfrom
DHIS2-20891

Conversation

@enricocolasante
Copy link
Copy Markdown
Contributor

@enricocolasante enricocolasante commented Feb 24, 2026

Eliminate program and programstage table joins from single event queries when the respective parameter is known. The columns (uid, code, name, attributeValues) are already available from the Java objects loaded during request validation, so we populate them directly in the row mapper instead of joining the tables.

Similar approach as #23044 for tracker events.

The program parameter is mandatory for /api/tracker/events, so the program join is always eliminated and, in the case of single events, a program of type WITHOUT_REGISTRATION only has one programstage, hence also the program stage join can be eliminated.

Before:

select
    ...
    p.uid as p_uid, p.code as p_code, p.name as p_name,
    p.attributevalues as p_attributevalues,
    ps.uid as ps_uid, ps.code as ps_code, ps.name as ps_name,
    ps.attributevalues as ps_attributevalues,
    ...
    p.type as p_type,
    ...
from singleevent ev
inner join program p on p.programid = en.programid
inner join programstage ps on ps.programstageid = ev.programstageid
...
where
    p.programid = 1150449

After (with program known):

select
    ...
    ev.eventid as ev_id, ev.status as ev_status,
    ...
from singleevent ev
...
where
    ev.programstageid = 860005

Performance

Database

Request Base p95 Cand p95 Diff
Go to first page of program VBqh0ynB2wv 10ms 10ms +0%
Go to second page of program VBqh0ynB2wv 10ms 10ms +0%
Search single events assigned to any user in program VBqh0ynB2wv 6ms 7ms +17%
Search single events in date interval in program VBqh0ynB2wv 22ms 22ms +0%
Search single events not assigned to a user in program VBqh0ynB2wv 10ms 10ms +0%
Request Base p95 Cand p95 Diff
Go to first page of program VBqh0ynB2wv 11ms 10ms -9%
Go to second page of program VBqh0ynB2wv 11ms 10ms -9%
Search single events assigned to any user in program VBqh0ynB2wv 7ms 7ms +0%
Search single events in date interval in program VBqh0ynB2wv 10ms 10ms +0%
Search single events not assigned to a user in program VBqh0ynB2wv 10ms 10ms +0%

No regressions and no visible improvement

@enricocolasante enricocolasante force-pushed the DHIS2-20891 branch 2 times, most recently from 7c4b14d to fe59d02 Compare February 24, 2026 15:02
@enricocolasante enricocolasante marked this pull request as ready for review February 25, 2026 11:19
@enricocolasante enricocolasante requested a review from a team as a code owner February 25, 2026 11:19

return caseStatement.toString();
sqlParameters.addValue("skipSyncDataElements", dataElementUids);
return "(SELECT jsonb_object_agg(key, value)"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a text block or single line

@enricocolasante enricocolasante enabled auto-merge (squash) February 26, 2026 08:26
@sonarqubecloud
Copy link
Copy Markdown

@enricocolasante enricocolasante merged commit 477f29c into master Feb 26, 2026
16 checks passed
@enricocolasante enricocolasante deleted the DHIS2-20891 branch February 26, 2026 08:42
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.

3 participants