Skip to content

O3-5763: Fetch queue entry associations lazily - #122

Merged
ibacher merged 2 commits into
openmrs:mainfrom
UjjawalPrabhat:O3-5763-lazy-queue-associations
Aug 25, 2026
Merged

O3-5763: Fetch queue entry associations lazily#122
ibacher merged 2 commits into
openmrs:mainfrom
UjjawalPrabhat:O3-5763-lazy-queue-associations

Conversation

@UjjawalPrabhat

@UjjawalPrabhat UjjawalPrabhat commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Summary

service-queues.spec.ts in #2600 started timing out once the suite created a queue entry.

QueueEntry leaves all eight *ToOne associations at the JPA default of EAGER, and Queue does the same for its four @ManyToOne fields. Reading one entry pulls every association's own eager graph in a single statement — 21KB of SQL, 30 joins. On a fresh install, which ships zero-row table statistics, the optimizer plans that badly.

Same database, same 51 entries, the query the frontend actually makes:

eager (main) lazy (this PR)
51 queue entries never finished — still running at 26 min 1.09s cold, 0.16s warm

Worth Noting:

  • REST output is unchanged — byte-for-byte identical for v=ref, v=default, v=full and isEnded=false. Serialisation happens inside the request session, so no LazyInitializationException.
  • This trades one bad join for N+1 — primary-key lookups instead of a 30-join statement. Faster in practice, but a real change in shape. Fetch-joining in the DAO doesn't help; Patient, Visit and Concept bring their own eager graphs from core.

Related Issue

O3-5763

QueueEntry and Queue left their *ToOne associations at the JPA default of
EAGER, so reading one queue entry pulled every association's own eager
graph in a single 30-join statement. On a fresh database, which ships
zero-row statistics, that statement does not finish; lazily it returns in
about a second.

Also excluded from Lombok's equals, hashCode and toString, which would
otherwise re-initialise the proxies.
Comment thread api/src/main/java/org/openmrs/module/queue/model/Queue.java Outdated
Address review feedback: BaseOpenmrsObject already implements both in terms
of the UUID, so the entities don't need Lombok's generated versions. That
also removes the need to exclude each association from them.
@UjjawalPrabhat
UjjawalPrabhat requested a review from ibacher August 25, 2026 16:50
@sonarqubecloud

Copy link
Copy Markdown

@ibacher

ibacher commented Aug 25, 2026

Copy link
Copy Markdown
Member

Fetch-joining in the DAO doesn't help; Patient, Visit and Concept bring their own eager graphs from core.

Yeah, that's also meant to be being fixed...

@ibacher
ibacher merged commit 2afe7c5 into openmrs:main Aug 25, 2026
9 checks passed
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