-
Notifications
You must be signed in to change notification settings - Fork 615
[PERFORMANCE]: Fix N+1 Query in list_prompts - Missing joinedload for gateway #1880
Copy link
Copy link
Closed
Copy link
Labels
performancePerformance related itemsPerformance related itemspythonPython / backend development (FastAPI)Python / backend development (FastAPI)
Milestone
Description
Summary
The list_prompts() method in prompt_service.py triggers N+1 queries when accessing the gateway_slug property, identical to the pattern in list_tools (#1879).
Root Cause
Location: mcpgateway/services/prompt_service.py
The list_prompts() method builds queries without eager loading the gateway relationship. When convert_prompt_to_read() accesses gateway_slug (line 338):
"gateway_slug": getattr(db_prompt, "gateway_slug", None),The gateway_slug hybrid property (db.py:3354-3360) triggers lazy loading:
@hybrid_property
def gateway_slug(self) -> Optional[str]:
return self.gateway.slug if self.gateway else NoneAffected Methods
list_prompts()- line ~908 - needsjoinedload(DbPrompt.gateway)list_prompts_for_user()- line ~1065 - needsjoinedload(DbPrompt.gateway)
Fix
Add joinedload(DbPrompt.gateway) to queries:
from sqlalchemy.orm import joinedload
query = select(DbPrompt).options(joinedload(DbPrompt.gateway)).order_by(...)Related
- [PERFORMANCE]: Fix N+1 Query in list_tools - Missing joinedload for gateway #1879 - Same pattern in
list_tools list_resourcesdoes NOT have this issue (nogateway_slugaccess inconvert_resource_to_read)list_serversdoes NOT have gateway relationship
Acceptance Criteria
- Add
joinedload(DbPrompt.gateway)tolist_prompts()query - Add
joinedload(DbPrompt.gateway)tolist_prompts_for_user()query - Verify with load test
- Passes
make verify
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
performancePerformance related itemsPerformance related itemspythonPython / backend development (FastAPI)Python / backend development (FastAPI)