-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: [PPT-2299] add bookings query indexes #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
migration/db/migrations/20251211120000000_optimize_bookings_query_indexes.sql
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| -- +micrate Up | ||
| -- SQL in section 'Up' is executed when this migration is applied | ||
|
|
||
| -- Drop the existing HASH index on tenant_id and replace with BTREE for better range query support | ||
| DROP INDEX IF EXISTS bookings_tenant_id_idx; | ||
| CREATE INDEX IF NOT EXISTS bookings_tenant_id_btree_idx ON bookings USING BTREE (tenant_id); | ||
|
|
||
| -- Create a composite index for the most common query pattern: | ||
| -- tenant_id + booking_type + deleted + time range queries | ||
| CREATE INDEX IF NOT EXISTS bookings_tenant_type_deleted_time_idx | ||
| ON bookings USING BTREE (tenant_id, booking_type, deleted, booking_start, booking_end) | ||
| WHERE deleted = FALSE; | ||
|
|
||
| -- Create a GIN index for zones array with additional conditions | ||
| -- This helps with the "ANY (zones)" queries | ||
| CREATE INDEX IF NOT EXISTS bookings_zones_gin_idx | ||
| ON bookings USING GIN (zones) | ||
| WHERE deleted = FALSE AND checked_out_at IS NULL; | ||
|
|
||
| -- Create a composite index for recurring bookings queries | ||
| CREATE INDEX IF NOT EXISTS bookings_recurring_lookup_idx | ||
| ON bookings USING BTREE (tenant_id, recurrence_type, recurrence_end, booking_start) | ||
| WHERE deleted = FALSE AND rejected_at IS NULL AND deleted_at IS NULL; | ||
|
|
||
| -- Create a partial index for active bookings (not checked out, not deleted) | ||
| CREATE INDEX IF NOT EXISTS bookings_active_idx | ||
| ON bookings USING BTREE (tenant_id, booking_type, booking_start, booking_end) | ||
| WHERE deleted = FALSE AND checked_out_at IS NULL; | ||
|
|
||
| -- Create a composite index specifically for the zone + type + time range query pattern | ||
| CREATE INDEX IF NOT EXISTS bookings_tenant_type_time_zones_idx | ||
| ON bookings USING BTREE (tenant_id, booking_type, booking_start, booking_end) | ||
| INCLUDE (zones, recurrence_type, recurrence_end, rejected_at, deleted_at, checked_out_at) | ||
| WHERE deleted = FALSE; | ||
|
|
||
| -- +micrate Down | ||
| -- SQL section 'Down' is executed when this migration is rolled back | ||
|
|
||
| DROP INDEX IF EXISTS bookings_tenant_type_time_zones_idx; | ||
| DROP INDEX IF EXISTS bookings_active_idx; | ||
| DROP INDEX IF EXISTS bookings_recurring_lookup_idx; | ||
| DROP INDEX IF EXISTS bookings_zones_gin_idx; | ||
| DROP INDEX IF EXISTS bookings_tenant_type_deleted_time_idx; | ||
| DROP INDEX IF EXISTS bookings_tenant_id_btree_idx; | ||
|
|
||
| -- Restore the original HASH index | ||
| CREATE INDEX IF NOT EXISTS bookings_tenant_id_idx ON bookings USING HASH (tenant_id); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.