Skip to content

Commit a0e25d1

Browse files
committed
Optimize recurring bookings indexes with separate recurring/regular booking indexes
1 parent a28428b commit a0e25d1

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

migration/db/migrations/20251211120000000_optimize_bookings_query_indexes.sql

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ CREATE INDEX IF NOT EXISTS bookings_zones_gin_idx
1818
WHERE deleted = FALSE AND checked_out_at IS NULL;
1919

2020
-- Create a composite index for recurring bookings queries
21-
CREATE INDEX IF NOT EXISTS bookings_recurring_lookup_idx
22-
ON bookings USING BTREE (tenant_id, recurrence_type, recurrence_end, booking_start)
23-
WHERE deleted = FALSE AND rejected_at IS NULL AND deleted_at IS NULL;
21+
CREATE INDEX CONCURRENTLY IF NOT EXISTS bookings_recurring_idx
22+
ON bookings (tenant_id, booking_start, recurrence_end)
23+
WHERE recurrence_type <> 'NONE';
24+
25+
-- Create a composite index for regular (non-recurring) bookings time range queries
26+
CREATE INDEX CONCURRENTLY IF NOT EXISTS bookings_regular_time_idx
27+
ON bookings (tenant_id, booking_start, booking_end)
28+
WHERE recurrence_type = 'NONE';
2429

2530
-- Create a partial index for active bookings (not checked out, not deleted)
2631
CREATE INDEX IF NOT EXISTS bookings_active_idx
@@ -38,7 +43,8 @@ CREATE INDEX IF NOT EXISTS bookings_tenant_type_time_zones_idx
3843

3944
DROP INDEX IF EXISTS bookings_tenant_type_time_zones_idx;
4045
DROP INDEX IF EXISTS bookings_active_idx;
41-
DROP INDEX IF EXISTS bookings_recurring_lookup_idx;
46+
DROP INDEX IF EXISTS bookings_regular_time_idx;
47+
DROP INDEX IF EXISTS bookings_recurring_idx;
4248
DROP INDEX IF EXISTS bookings_zones_gin_idx;
4349
DROP INDEX IF EXISTS bookings_tenant_type_deleted_time_idx;
4450
DROP INDEX IF EXISTS bookings_tenant_id_btree_idx;

0 commit comments

Comments
 (0)