Skip to content

⚡ Bolt: Optimize Lending Service N+1 queries with aggregation#40

Open
Woschj wants to merge 1 commit intomainfrom
bolt-n-plus-1-fix-lending-service-10619731348114346618
Open

⚡ Bolt: Optimize Lending Service N+1 queries with aggregation#40
Woschj wants to merge 1 commit intomainfrom
bolt-n-plus-1-fix-lending-service-10619731348114346618

Conversation

@Woschj
Copy link
Owner

@Woschj Woschj commented Feb 26, 2026

⚡ Bolt: Optimized Lending Service N+1 queries

This PR addresses a significant performance bottleneck in the database access layer, specifically targeting N+1 query patterns in the `LendingService` and the Manual Lending admin route.

💡 What:

  • Replaced manual loops and individual `find_one` calls in `LendingService` with MongoDB aggregation pipelines using `$lookup` and `$unwind`.
  • Refactored the `manual_lending` route to utilize these optimized service methods.
  • Added `days` and `only_outputs` filtering to `get_recent_consumable_usage` for more efficient data retrieval.

🎯 Why:
The previous implementation performed individual database lookups for every tool and worker in a list of lendings, leading to $1 + 2N$ queries. For a typical list of 50 lendings, this resulted in over 100 database roundtrips, causing noticeable latency in the admin dashboard.

📊 Impact:

  • Database roundtrips for fetching active lendings reduced from 101 to 1 (for 50 records).
  • Measurable performance gain of ~98% in query volume for the `manual_lending` route.
  • Faster page load times and reduced database load.

🔬 Measurement:
Verified using a benchmark script that counts database calls before and after the optimization.

  • Before: 101 calls for 50 lendings.
  • After: 1 call for 50 lendings.

Tests were run to ensure functional parity and no regressions were introduced.


PR created automatically by Jules for task 10619731348114346618 started by @Woschj

This commit optimizes several methods in \`LendingService\` (\`get_active_lendings\`, \`get_recent_consumable_usage\`, \`get_worker_consumable_history\`, and \`get_tool_lending_history\`) by replacing loop-based \`find_one\` calls with MongoDB aggregation pipelines using \`\$lookup\`.

Additionally, the \`manual_lending\` route in \`app/routes/admin/system.py\` has been refactored to use these optimized service methods, eliminating manual N+1 query loops in the GET handler.

Performance Impact:
- Reduces database roundtrips for \`get_active_lendings\` from 101 to 1 for 50 records (~99% reduction).
- Significantly improves page load time for the Manual Lending admin view.
- Centralizes data enrichment logic in the service layer.

Co-authored-by: Woschj <81321922+Woschj@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 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