AdvancedTopic: partitioning.sql

File partitioning.sql, 5.0 KB (added by 231184, 8 days ago)

partitioning

Line 
1DROP TABLE IF EXISTS time_slot_partitioned CASCADE;
2DROP TABLE IF EXISTS appointment_partitioned CASCADE;
3
4-- ============================================================
5-- TIME_SLOT PARTITIONED BY QUARTERS
6-- ============================================================
7
8CREATE 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
21CREATE TABLE time_slot_2026_q2 PARTITION OF time_slot_partitioned
22 FOR VALUES FROM ('2026-04-01') TO ('2026-07-01');
23
24CREATE TABLE time_slot_2026_q3 PARTITION OF time_slot_partitioned
25 FOR VALUES FROM ('2026-07-01') TO ('2026-10-01');
26
27CREATE TABLE time_slot_2026_q4 PARTITION OF time_slot_partitioned
28 FOR VALUES FROM ('2026-10-01') TO ('2027-01-01');
29
30CREATE TABLE time_slot_2027_q1 PARTITION OF time_slot_partitioned
31 FOR VALUES FROM ('2027-01-01') TO ('2027-04-01');
32
33CREATE TABLE time_slot_2027_q2 PARTITION OF time_slot_partitioned
34 FOR VALUES FROM ('2027-04-01') TO ('2027-07-01');
35
36
37INSERT 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)
46SELECT
47 slot_id,
48 employee_id,
49 business_id,
50 date,
51 start_time,
52 end_time,
53 is_available
54FROM time_slot
55WHERE date >= '2026-04-01'
56 AND date < '2027-07-01';
57
58
59-- Index for available slots query
60CREATE 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
66SELECT 'time_slot_2026_q2' AS partition_name, COUNT(*) FROM time_slot_2026_q2
67UNION ALL
68SELECT 'time_slot_2026_q3', COUNT(*) FROM time_slot_2026_q3
69UNION ALL
70SELECT 'time_slot_2026_q4', COUNT(*) FROM time_slot_2026_q4
71UNION ALL
72SELECT 'time_slot_2027_q1', COUNT(*) FROM time_slot_2027_q1
73UNION ALL
74SELECT 'time_slot_2027_q2', COUNT(*) FROM time_slot_2027_q2;
75
76
77EXPLAIN ANALYZE
78SELECT *
79FROM time_slot_partitioned
80WHERE 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
90CREATE 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
104CREATE TABLE appointment_2026_q2 PARTITION OF appointment_partitioned
105 FOR VALUES FROM ('2026-04-01') TO ('2026-07-01');
106
107CREATE TABLE appointment_2026_q3 PARTITION OF appointment_partitioned
108 FOR VALUES FROM ('2026-07-01') TO ('2026-10-01');
109
110CREATE TABLE appointment_2026_q4 PARTITION OF appointment_partitioned
111 FOR VALUES FROM ('2026-10-01') TO ('2027-01-01');
112
113CREATE TABLE appointment_2027_q1 PARTITION OF appointment_partitioned
114 FOR VALUES FROM ('2027-01-01') TO ('2027-04-01');
115
116CREATE TABLE appointment_2027_q2 PARTITION OF appointment_partitioned
117 FOR VALUES FROM ('2027-04-01') TO ('2027-07-01');
118
119
120INSERT 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)
130SELECT
131 appointment_id,
132 customer_id,
133 employee_id,
134 business_id,
135 service_id,
136 slot_id,
137 status,
138 created_at
139FROM appointment
140WHERE created_at >= '2026-04-01'
141 AND created_at < '2027-07-01';
142
143
144-- Index for customer appointments query
145CREATE INDEX idx_appointment_partitioned_customer
146 ON appointment_partitioned (customer_id);
147
148-- Optional but useful if you often filter customer + status
149CREATE INDEX idx_appointment_partitioned_customer_status
150 ON appointment_partitioned (customer_id, status);
151
152
153-- Count by quarter
154SELECT 'appointment_2026_q2' AS partition_name, COUNT(*) FROM appointment_2026_q2
155UNION ALL
156SELECT 'appointment_2026_q3', COUNT(*) FROM appointment_2026_q3
157UNION ALL
158SELECT 'appointment_2026_q4', COUNT(*) FROM appointment_2026_q4
159UNION ALL
160SELECT 'appointment_2027_q1', COUNT(*) FROM appointment_2027_q1
161UNION ALL
162SELECT 'appointment_2027_q2', COUNT(*) FROM appointment_2027_q2;
163
164
165EXPLAIN ANALYZE
166SELECT *
167FROM appointment_partitioned
168WHERE customer_id = 100;