perf: use UNION ALL with DISTINCT for bookings query optimization#27841
Merged
keithwillcode merged 12 commits intomainfrom Feb 11, 2026
Merged
perf: use UNION ALL with DISTINCT for bookings query optimization#27841keithwillcode merged 12 commits intomainfrom
keithwillcode merged 12 commits intomainfrom
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
E2E results are ready! |
Contributor
Author
|
@paragon-evolve |
keithwillcode
commented
Feb 10, 2026
Contributor
There was a problem hiding this comment.
1 issue found across 1 file (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="packages/trpc/server/routers/viewer/bookings/get.handler.ts">
<violation number="1">
P1: `countAll()` will over-count duplicates from the UNION ALL subquery. Use `COUNT(DISTINCT union_subquery.id)` so totalCount matches the deduplicated list.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Contributor
Devin AI is addressing Cubic AI's review feedbackA Devin session has been created to address the issues identified by Cubic AI. |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Paragon: tests updated2 updated tests generated for this PR. Updated Tests
DetailsUpdated Tests
|
Contributor
Author
Tests Added by ParagonThe following test files have been added to this PR:
These tests were generated from an approved test proposal. Generated with Paragon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


What does this PR do?
Optimizes the slowest database query in the bookings list endpoint (
getBookings) by changingUNION→UNION ALLand addingSELECT DISTINCTon the outer query. This was validated via query plan analysis to yield a ~40% execution time reduction.Changes:
acc.union(query)→acc.unionAll(query)— stops the DB from deduplicating within each union step.distinct()on the outer select — deduplicates once at the end insteadfn.countAll()→fn.count("union_subquery.id").distinct()— ensures totalCount still returns the correct deduplicated countget.handler.integration-test.ts) covering the UNION query and totalCount correctnessunionAllanddistinctmethodsIntegration tests
Seven integration tests run
getBookingsagainst a real database with freshly created users, teams, bookings, and attendees:totalCount === bookings.length(exact match, not>=)take/skippagination returns distinct pages in correctstartTimeorderTests were verified passing on both
main(withUNION) and this branch (withUNION ALL+DISTINCT).Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
Integration tests (recommended):
Unit tests:
Manual SQL verification:
Run
EXPLAIN ANALYZEon the generated SQL before and after on a database with a non-trivial number of bookings and confirm:UNION ALLbetween subqueries (notUNION)SELECT DISTINCTCOUNT(DISTINCT "union_subquery"."id")Human Review Checklist
fn.count("union_subquery.id").distinct()generatesCOUNT(DISTINCT "union_subquery"."id")in the compiled SQL (not e.g.COUNT(DISTINCT *))DISTINCT+ORDER BY+LIMIT/OFFSETinteraction on the outer query doesn't affect result ordering or paginationtotalCount === bookings.lengthis sufficient to catch count bugs (test usestake: 50with only 4 bookings, so all fit in one page)booking4test scenario actually hits multiple union branches (user is creator, user is attendee, team event type)