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
Open
Conversation
…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
|
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. |
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.
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 theAbstractBaseQueueDaoImpl.Key Changes
Bug Fix:
Optimized fetching: Switched all
@ManyToOneand@OneToOneentity relationships in the Queue models toFetchType.LAZY.Removed Implicit Proxy Triggers: Dropped Lombok
@EqualsAndHashCode(callSuper = true)from entities and added@ToString.Excludeto relational fields to prevent accidental proxy fetching during standard property stringification.Fixed Duplicate Queries: Removed the consecutive
.uniqueResult()invocation inAbstractBaseQueueDaoImpl#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.