Skip to content

⚡ Bolt: Fix N+1 query in PoolController.getPoolData by batching local keys fetch - #64

Open
zaydiscold wants to merge 1 commit into
masterfrom
bolt-fix-pool-controller-n-plus-one-16955008338495311246
Open

⚡ Bolt: Fix N+1 query in PoolController.getPoolData by batching local keys fetch#64
zaydiscold wants to merge 1 commit into
masterfrom
bolt-fix-pool-controller-n-plus-one-16955008338495311246

Conversation

@zaydiscold

Copy link
Copy Markdown
Owner

💡 What: The optimization implements a single batched database query store.getAllLocalKeys to fetch all local key records for a user at once, replacing the N individual database queries previously executed inside the Promise.allSettled loop in PoolController.getPoolData. The retrieved keys are indexed into an in-memory Map for O(1) lookups during iteration.

🎯 Why: The previous implementation suffered from an N+1 query problem, hitting the database once for each account. As the number of accounts grows, this pattern causes high database load and latency.

📊 Impact: Reduces the number of database queries in the getPoolData endpoint from N+1 (one for accounts, plus one for every account's keys) to exactly 2, regardless of how many accounts the user has.

🔬 Measurement: Verify the improvement by running a trace on database queries during a request to /api/pool. The number of SELECT * FROM Key queries will now be 1, rather than scaling linearly with the number of accounts.


PR created automatically by Jules for task 16955008338495311246 started by @zaydiscold

… keys fetch

- Added `getAllLocalKeys` to fetch all keys for a user in a single query.
- Replaced inside-the-loop querying in `PoolController.getPoolData` with O(1) map lookups.
- Prevents database connection exhaustion and reduces latency.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
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