Skip to content

⚡ Bolt: optimize manual lending N+1 queries#22

Open
Woschj wants to merge 1 commit intomainfrom
bolt-optimize-manual-lending-755098390866371333
Open

⚡ Bolt: optimize manual lending N+1 queries#22
Woschj wants to merge 1 commit intomainfrom
bolt-optimize-manual-lending-755098390866371333

Conversation

@Woschj
Copy link
Owner

@Woschj Woschj commented Feb 19, 2026

The `manual_lending` route in `app/routes/admin/system.py` suffered from a classic N+1 query problem. For every active lending and every recent consumable usage displayed on the page, the application performed two additional `find_one` queries to retrieve tool/consumable and worker names.

This optimization replaces these iterative lookups with two single MongoDB aggregation pipelines. By using `$lookup` (joins) and `$project` (formatting), we've moved the data association to the database layer.

Performance Impact:

  • Before: $1 + 2 \times (L + U)$ queries (where $L$ is active lendings and $U$ is recent usages).
  • After: Exactly $2$ queries for the list.
  • Result: Measurable speed improvement on pages with significant activity, reduced database load, and fewer network roundtrips.

Verification was performed using a benchmark script in `mongomock` to ensure data correctness and logic parity.


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

Optimized the \`manual_lending\` route by replacing O(N) loop-based database lookups
with efficient MongoDB aggregation pipelines. This reduces the number of database
roundtrips from O(N) to O(1), significantly improving page load performance
when many active lendings or recent consumable usages exist.

- Replaced active tool lending loop with aggregation using \$lookup and \$project.
- Replaced recent consumable usage loop with aggregation using \$lookup and \$project.
- Maintained existing sorting logic and data structure for full compatibility.

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