Skip to content

fix: batch database queries in getPlaylistsForDJ to eliminate N+1#191

Open
jakebromberg wants to merge 2 commits intomainfrom
fix/29-playlist-n-plus-1
Open

fix: batch database queries in getPlaylistsForDJ to eliminate N+1#191
jakebromberg wants to merge 2 commits intomainfrom
fix/29-playlist-n-plus-1

Conversation

@jakebromberg
Copy link
Member

Summary

  • Fixed N+1 query problem in getPlaylistsForDJ: the previous implementation ran 3-4 sequential DB queries per show inside a for loop, resulting in ~4N queries for N shows (e.g., 17 queries for 5 shows).
  • Refactored to batched IN queries using inArray from drizzle-orm: one query each for shows, show DJs, specialty shows, and flowsheet entries, then assembles results in JS. Total: ~5 constant queries regardless of show count.
  • Added unit test that asserts db.select() is called at most 6 times for 5 shows, catching any regression back to O(N) queries.

Test plan

  • New test djs.getPlaylistsForDJ.test.ts verifies query count is bounded (≤6 for 5 shows)
  • New test verifies returned data structure matches ShowPeek shape
  • All 122 existing unit tests continue to pass
  • Manual verification: call GET /djs/:id/playlists on staging and confirm response shape is unchanged

Made with Cursor

Replaced sequential per-show queries with batched IN queries,
reducing ~4N queries to ~5 constant queries.

Co-authored-by: Cursor <cursoragent@cursor.com>
@jakebromberg jakebromberg force-pushed the fix/29-playlist-n-plus-1 branch from 9f61e78 to 0ea5a6b Compare February 27, 2026 05:56
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