Optimization: Fix Gateway N+1 Queries via Eager Loading#2204
Optimization: Fix Gateway N+1 Queries via Eager Loading#2204crivetimihai merged 1 commit intomainfrom
Conversation
b896fb8 to
6861616
Compare
6861616 to
dda02c3
Compare
Add email_team relationship and team property to Gateway model, mirroring the optimization applied to Server model in PR #1962 and #1975. Changes: - Add email_team relationship to Gateway model (db.py) - Add team property for convenient access to team name - Update gateway_service.py to use joinedload(DbGateway.email_team) - Remove _get_team_name helper method (no longer needed) - Update tests to mock db.execute instead of db.get This reduces database queries by ~50% for gateway operations that need team information, eliminating the N+1 query pattern. Closes #1994 Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
dda02c3 to
e545808
Compare
Review & Testing SummaryChanges Made During Review
Testing PerformedSQL Query Verification (with
API Response Verification: Unit Tests: All 5,262 tests pass (3 pre-existing failures unrelated to this PR) Consistency CheckImplementation follows the same pattern as Server model (PR #1962, #1975) and Tool model. Commits squashed into single clean commit with original author attribution preserved. |
Add email_team relationship and team property to Gateway model, mirroring the optimization applied to Server model in PR IBM#1962 and IBM#1975. Changes: - Add email_team relationship to Gateway model (db.py) - Add team property for convenient access to team name - Update gateway_service.py to use joinedload(DbGateway.email_team) - Remove _get_team_name helper method (no longer needed) - Update tests to mock db.execute instead of db.get This reduces database queries by ~50% for gateway operations that need team information, eliminating the N+1 query pattern. Closes IBM#1994 Signed-off-by: Keval Mahajan <mahajankeval23@gmail.com> Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> Co-authored-by: Mihai Criveti <crivetimihai@gmail.com>
✨ Performance Enhancement PR
🔗 Epic / Issue
Closes #1994
🚀 Summary (1-2 sentences)
This PR resolves the N+1 query performance issue in the Gateway service by implementing eager loading for the team relationship. It introduces the email_team relationship and team property to the Gateway model and updates service methods to fetch all necessary data in a single database query, mirroring the optimization recently applied to the Server model.
🛠️ Fix Description
This PR addresses the N+1 query issue where retrieving a Gateway entity resulted in an additional database query to fetch the associated team name.
1. Database Model Updates (mcpgateway/db.py)
2. Service Layer Optimization (
mcpgateway/services/gateway_service.py)get_gateway,update_gateway,set_gateway_state,create_gateway,delete_gateway, and_sync_gateway_with_oauthmethod to usedb.execute()withjoinedload(DbGateway.email_team). This instructs SQLAlchemy to fetch the Gateway and its associatedEmailTeamin a single SQL query using a LEFT JOIN._get_team_namehelper method, which was previously responsible for the secondary query effectively eliminating the N+1 problem.3. Test Updates (
tests/unit/mcpgateway/services/test_gateway_service.py)⚡ Impact Assessment
The following table demonstrates the reduction in database queries for key Gateway operations:
get_gateway()update_gateway()set_gateway_state()delete_gateway()_sync_gateway_with_oauth()🧪 Checks
make lintpassesmake testpasses📓 Notes (optional)
This implementation follows the pattern established in PR #1962 and #1975 for the Server model. The _get_team_name helper method, which was responsible for the extra N+1 query, has been removed entirely.