Skip to content

fix: resolve N+1 query in allocations getByUserIdAndThreshold#400

Open
alex-vydrin wants to merge 1 commit intoOWASP:masterfrom
alex-vydrin:fix/allocations-n-plus-1-query
Open

fix: resolve N+1 query in allocations getByUserIdAndThreshold#400
alex-vydrin wants to merge 1 commit intoOWASP:masterfrom
alex-vydrin:fix/allocations-n-plus-1-query

Conversation

@alex-vydrin
Copy link
Copy Markdown

Summary

  • Fix N+1 database queries in allocations-dao.js getByUserIdAndThreshold: replaced the per-allocation getUserById loop (one MongoDB round-trip per allocation) with a single batched getUsersByIds call using $in, reducing DB round-trips from N to 1.
  • Added getUsersByIds to user-dao.js — accepts an array of user IDs, deduplicates them, and fetches all matching users in one query.

Test plan

  • Verify getByUserIdAndThreshold returns correct user details (userName, firstName, lastName) on each allocation
  • Confirm only a single users collection query is issued regardless of allocation count
  • Check edge case where multiple allocations reference the same userId (no duplicate fetches)

Made with Cursor

Replace per-allocation getUserById calls with a single batched
getUsersByIds query using $in, deduplicating userIds to minimize
the MongoDB round-trips from N to 1.

Made-with: Cursor
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