⚡ Bolt: Eliminate N+1 bottlenecks in LendingService and Manual Lending route#66
⚡ Bolt: Eliminate N+1 bottlenecks in LendingService and Manual Lending route#66
Conversation
Eliminated N+1 query bottlenecks in `LendingService` methods and the `manual_lending` route by implementing MongoDB aggregation pipelines. Key improvements: - `LendingService.get_active_lendings`: Replaced loop with `$lookup` aggregation. - `LendingService.get_recent_consumable_usage`: Replaced loop with `$lookup` aggregation and added filtering parameters. - `LendingService.get_worker_consumable_history` & `get_tool_lending_history`: Replaced manual joins with aggregation. - `manual_lending` route: Refactored to use the new optimized service methods. Performance Impact: - Reduces database round-trips from ~201 to 1-2 for lists of 100 items (~99% reduction). - Significantly faster page load for the manual lending view. Co-authored-by: Woschj <81321922+Woschj@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. |
This PR optimizes the retrieval of lending and consumable usage lists by replacing inefficient N+1 query patterns with MongoDB aggregation pipelines.
Previously, fetching a list of active lendings or recent consumable usages required separate database queries for every item to fetch associated tool, worker, or consumable details. This resulted in significant performance degradation as the data volume grew.
The optimized implementation uses
$lookupto join related documents at the database level and$addFieldsto flatten the results. This reduces the number of database round-trips to a constant number (typically 1 or 2), regardless of the number of items being retrieved.Benchmarks confirmed a reduction from 201 database calls down to 1 call for a list of 100 items.
PR created automatically by Jules for task 4667119673843442985 started by @Woschj