Skip to content

fix: O3-5473 Optimize Queue Module database queries (Fix N+1 fetching and duplicate queries)#98

Open
bhavya-jpg wants to merge 1 commit intoopenmrs:mainfrom
bhavya-jpg:fix/backend-performance-queries
Open

fix: O3-5473 Optimize Queue Module database queries (Fix N+1 fetching and duplicate queries)#98
bhavya-jpg wants to merge 1 commit intoopenmrs:mainfrom
bhavya-jpg:fix/backend-performance-queries

Conversation

@bhavya-jpg
Copy link

@bhavya-jpg bhavya-jpg commented Mar 6, 2026

Requirements:

Problem Statement: The backend currently suffers from severe performance degradation when querying queue entries due to deep N+1 fetches and duplicate query execution. This creates latency on the frontend dashboard when scaling the data in the queue lists.

Summary:
Solution: This PR addresses the core query inefficiencies by enforcing lazy loading on all heavy relationships (QueueEntry, Queue, QueueRoom) and fixing a redundant .uniqueResult() execution in the AbstractBaseQueueDaoImpl.

Key Changes

Bug Fix:

Optimized fetching: Switched all @ManyToOne and @OneToOne entity relationships in the Queue models to FetchType.LAZY.
Removed Implicit Proxy Triggers: Dropped Lombok @EqualsAndHashCode(callSuper = true) from entities and added @ToString.Exclude to relational fields to prevent accidental proxy fetching during standard property stringification.
Fixed Duplicate Queries: Removed the consecutive .uniqueResult() invocation in AbstractBaseQueueDaoImpl#get(String uuid) which caused identical database requests to run twice in succession.

Related Issue
Jira ticket: https://openmrs.atlassian.net/browse/O3-5473

Other
Impact: Resolves massive N+1 issues resulting in 59+ cascading joins for basic fetching operations, vastly reducing database latency and memory strain.
Testing: Verified locally against native maven tests. Passed all 96 unit block and integration tests smoothly without triggering unexpected LazyInitializationExceptions.

…e queries)

- Enforce Lazy Loading on QueueEntry, Queue and QueueRoom
- Remove Problematic Lombok Annotations that forced proxy loading
- Fix AbstractBaseQueueDaoImpl.java to avoid duplicate querying
@bhavya-jpg bhavya-jpg changed the title Optimize Queue Module database queries (Fix N+1 fetching and duplicate queries) fix: O3-5473 Optimize Queue Module database queries (Fix N+1 fetching and duplicate queries) Mar 6, 2026
@RoshanMadival08
Copy link

RoshanMadival08 commented Mar 18, 2026

Good fix for the N+1 problem. Switching to FetchType.LAZY is the right approach here. A few thoughts:

Have you verified that no existing service layer code relies on eager loading of these relationships? Switching to LAZY can cause LazyInitializationException outside of a transaction context.
The duplicate .uniqueResult() fix is clean and straightforward.
Consider adding a comment explaining why LAZY was chosen here to help future contributors understand the intent.

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