| 1 | DROP TABLE IF EXISTS time_slot_partitioned CASCADE;
|
|---|
| 2 | DROP TABLE IF EXISTS appointment_partitioned CASCADE;
|
|---|
| 3 |
|
|---|
| 4 | -- ============================================================
|
|---|
| 5 | -- TIME_SLOT PARTITIONED BY QUARTERS
|
|---|
| 6 | -- ============================================================
|
|---|
| 7 |
|
|---|
| 8 | CREATE TABLE time_slot_partitioned (
|
|---|
| 9 | slot_id BIGINT NOT NULL,
|
|---|
| 10 | employee_id INT NOT NULL,
|
|---|
| 11 | business_id INT NOT NULL,
|
|---|
| 12 | date DATE NOT NULL,
|
|---|
| 13 | start_time TIME NOT NULL,
|
|---|
| 14 | end_time TIME NOT NULL,
|
|---|
| 15 | is_available BOOLEAN NOT NULL,
|
|---|
| 16 |
|
|---|
| 17 | PRIMARY KEY (slot_id, date)
|
|---|
| 18 | ) PARTITION BY RANGE (date);
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | CREATE TABLE time_slot_2026_q2 PARTITION OF time_slot_partitioned
|
|---|
| 22 | FOR VALUES FROM ('2026-04-01') TO ('2026-07-01');
|
|---|
| 23 |
|
|---|
| 24 | CREATE TABLE time_slot_2026_q3 PARTITION OF time_slot_partitioned
|
|---|
| 25 | FOR VALUES FROM ('2026-07-01') TO ('2026-10-01');
|
|---|
| 26 |
|
|---|
| 27 | CREATE TABLE time_slot_2026_q4 PARTITION OF time_slot_partitioned
|
|---|
| 28 | FOR VALUES FROM ('2026-10-01') TO ('2027-01-01');
|
|---|
| 29 |
|
|---|
| 30 | CREATE TABLE time_slot_2027_q1 PARTITION OF time_slot_partitioned
|
|---|
| 31 | FOR VALUES FROM ('2027-01-01') TO ('2027-04-01');
|
|---|
| 32 |
|
|---|
| 33 | CREATE TABLE time_slot_2027_q2 PARTITION OF time_slot_partitioned
|
|---|
| 34 | FOR VALUES FROM ('2027-04-01') TO ('2027-07-01');
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | INSERT INTO time_slot_partitioned (
|
|---|
| 38 | slot_id,
|
|---|
| 39 | employee_id,
|
|---|
| 40 | business_id,
|
|---|
| 41 | date,
|
|---|
| 42 | start_time,
|
|---|
| 43 | end_time,
|
|---|
| 44 | is_available
|
|---|
| 45 | )
|
|---|
| 46 | SELECT
|
|---|
| 47 | slot_id,
|
|---|
| 48 | employee_id,
|
|---|
| 49 | business_id,
|
|---|
| 50 | date,
|
|---|
| 51 | start_time,
|
|---|
| 52 | end_time,
|
|---|
| 53 | is_available
|
|---|
| 54 | FROM time_slot
|
|---|
| 55 | WHERE date >= '2026-04-01'
|
|---|
| 56 | AND date < '2027-07-01';
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | -- Index for available slots query
|
|---|
| 60 | CREATE INDEX idx_time_slot_partitioned_available_business_date
|
|---|
| 61 | ON time_slot_partitioned (business_id, date)
|
|---|
| 62 | WHERE is_available = TRUE;
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 | -- Count by quarter
|
|---|
| 66 | SELECT 'time_slot_2026_q2' AS partition_name, COUNT(*) FROM time_slot_2026_q2
|
|---|
| 67 | UNION ALL
|
|---|
| 68 | SELECT 'time_slot_2026_q3', COUNT(*) FROM time_slot_2026_q3
|
|---|
| 69 | UNION ALL
|
|---|
| 70 | SELECT 'time_slot_2026_q4', COUNT(*) FROM time_slot_2026_q4
|
|---|
| 71 | UNION ALL
|
|---|
| 72 | SELECT 'time_slot_2027_q1', COUNT(*) FROM time_slot_2027_q1
|
|---|
| 73 | UNION ALL
|
|---|
| 74 | SELECT 'time_slot_2027_q2', COUNT(*) FROM time_slot_2027_q2;
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | EXPLAIN ANALYZE
|
|---|
| 78 | SELECT *
|
|---|
| 79 | FROM time_slot_partitioned
|
|---|
| 80 | WHERE business_id = 1
|
|---|
| 81 | AND date = '2026-05-20'
|
|---|
| 82 | AND is_available = TRUE;
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | -- ============================================================
|
|---|
| 87 | -- APPOINTMENT PARTITIONED BY SAME QUARTERS
|
|---|
| 88 | -- ============================================================
|
|---|
| 89 |
|
|---|
| 90 | CREATE TABLE appointment_partitioned (
|
|---|
| 91 | appointment_id BIGINT NOT NULL,
|
|---|
| 92 | customer_id INT NOT NULL,
|
|---|
| 93 | employee_id INT NOT NULL,
|
|---|
| 94 | business_id INT NOT NULL,
|
|---|
| 95 | service_id INT NOT NULL,
|
|---|
| 96 | slot_id BIGINT NOT NULL,
|
|---|
| 97 | status VARCHAR NOT NULL,
|
|---|
| 98 | created_at TIMESTAMP NOT NULL,
|
|---|
| 99 |
|
|---|
| 100 | PRIMARY KEY (appointment_id, created_at)
|
|---|
| 101 | ) PARTITION BY RANGE (created_at);
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | CREATE TABLE appointment_2026_q2 PARTITION OF appointment_partitioned
|
|---|
| 105 | FOR VALUES FROM ('2026-04-01') TO ('2026-07-01');
|
|---|
| 106 |
|
|---|
| 107 | CREATE TABLE appointment_2026_q3 PARTITION OF appointment_partitioned
|
|---|
| 108 | FOR VALUES FROM ('2026-07-01') TO ('2026-10-01');
|
|---|
| 109 |
|
|---|
| 110 | CREATE TABLE appointment_2026_q4 PARTITION OF appointment_partitioned
|
|---|
| 111 | FOR VALUES FROM ('2026-10-01') TO ('2027-01-01');
|
|---|
| 112 |
|
|---|
| 113 | CREATE TABLE appointment_2027_q1 PARTITION OF appointment_partitioned
|
|---|
| 114 | FOR VALUES FROM ('2027-01-01') TO ('2027-04-01');
|
|---|
| 115 |
|
|---|
| 116 | CREATE TABLE appointment_2027_q2 PARTITION OF appointment_partitioned
|
|---|
| 117 | FOR VALUES FROM ('2027-04-01') TO ('2027-07-01');
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | INSERT INTO appointment_partitioned (
|
|---|
| 121 | appointment_id,
|
|---|
| 122 | customer_id,
|
|---|
| 123 | employee_id,
|
|---|
| 124 | business_id,
|
|---|
| 125 | service_id,
|
|---|
| 126 | slot_id,
|
|---|
| 127 | status,
|
|---|
| 128 | created_at
|
|---|
| 129 | )
|
|---|
| 130 | SELECT
|
|---|
| 131 | appointment_id,
|
|---|
| 132 | customer_id,
|
|---|
| 133 | employee_id,
|
|---|
| 134 | business_id,
|
|---|
| 135 | service_id,
|
|---|
| 136 | slot_id,
|
|---|
| 137 | status,
|
|---|
| 138 | created_at
|
|---|
| 139 | FROM appointment
|
|---|
| 140 | WHERE created_at >= '2026-04-01'
|
|---|
| 141 | AND created_at < '2027-07-01';
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | -- Index for customer appointments query
|
|---|
| 145 | CREATE INDEX idx_appointment_partitioned_customer
|
|---|
| 146 | ON appointment_partitioned (customer_id);
|
|---|
| 147 |
|
|---|
| 148 | -- Optional but useful if you often filter customer + status
|
|---|
| 149 | CREATE INDEX idx_appointment_partitioned_customer_status
|
|---|
| 150 | ON appointment_partitioned (customer_id, status);
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | -- Count by quarter
|
|---|
| 154 | SELECT 'appointment_2026_q2' AS partition_name, COUNT(*) FROM appointment_2026_q2
|
|---|
| 155 | UNION ALL
|
|---|
| 156 | SELECT 'appointment_2026_q3', COUNT(*) FROM appointment_2026_q3
|
|---|
| 157 | UNION ALL
|
|---|
| 158 | SELECT 'appointment_2026_q4', COUNT(*) FROM appointment_2026_q4
|
|---|
| 159 | UNION ALL
|
|---|
| 160 | SELECT 'appointment_2027_q1', COUNT(*) FROM appointment_2027_q1
|
|---|
| 161 | UNION ALL
|
|---|
| 162 | SELECT 'appointment_2027_q2', COUNT(*) FROM appointment_2027_q2;
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 | EXPLAIN ANALYZE
|
|---|
| 166 | SELECT *
|
|---|
| 167 | FROM appointment_partitioned
|
|---|
| 168 | WHERE customer_id = 100; |
|---|