⚡ Bolt: Remove N+1 pagination loop in KnowledgebaseService#82
⚡ Bolt: Remove N+1 pagination loop in KnowledgebaseService#82nkissick-del wants to merge 1 commit intomainfrom
Conversation
Optimized `get_all_kb_by_tenant_ids` by replacing manual offset/limit pagination loop with a single query, eliminating N+1 query performance anti-pattern. Also fixed a bug where `order_by` was not being applied correctly. Includes a regression test verifying the fix. Co-authored-by: nkissick-del <236767245+nkissick-del@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What:
Removed the manual
offset/limitloop inKnowledgebaseService.get_all_kb_by_tenant_ids.Fixed a bug where
order_byresult was ignored (Peeweeorder_byis not in-place).Added a regression test
test/unit_test/services/test_knowledgebase_perf.py.🎯 Why:
The original code fetched knowledge bases in batches of 50 using
offset. This is inefficient for large datasets (O(N^2) complexity for scanning offset) and causes multiple database round-trips. It was also marked with a# optimize latercomment.📊 Impact:
Significantly reduces database queries for tenants with many knowledge bases. Reduces from
ceil(N/50)queries to 1 query.🔬 Measurement:
Verified with
test/unit_test/services/test_knowledgebase_perf.pywhich mocks the DB interface and asserts thatoffsetandlimitare not called, whiledicts()is called on the sorted query.PR created automatically by Jules for task 15047153084982131651 started by @nkissick-del