Skip to content

⚡ Bolt: Remove N+1 pagination loop in KnowledgebaseService#82

Open
nkissick-del wants to merge 1 commit intomainfrom
bolt-remove-nplus1-loop-15047153084982131651
Open

⚡ Bolt: Remove N+1 pagination loop in KnowledgebaseService#82
nkissick-del wants to merge 1 commit intomainfrom
bolt-remove-nplus1-loop-15047153084982131651

Conversation

@nkissick-del
Copy link
Copy Markdown
Owner

💡 What:
Removed the manual offset/limit loop in KnowledgebaseService.get_all_kb_by_tenant_ids.
Fixed a bug where order_by result was ignored (Peewee order_by is 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 later comment.

📊 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.py which mocks the DB interface and asserts that offset and limit are not called, while dicts() is called on the sorted query.


PR created automatically by Jules for task 15047153084982131651 started by @nkissick-del

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>
@google-labs-jules
Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant