Skip to content

Commit cb79987

Browse files
committed
partition Data and query Partitioned Table (location-temp dataset)
1 parent 312bd99 commit cb79987

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*********** Partitioning Data ****************/
2+
3+
4+
-------- Create new table with associated Partion Column (event hour: 0 - 24 hours) --------
5+
6+
/* Create a table of location and temperature measurements */
7+
CREATE TABLE time_series.location_temp_p(
8+
event_time TIMESTAMP NOT NULL,
9+
event_hour INT,
10+
temp_celcius INT,
11+
location_id CHARACTER VARYING COLLATE pg_catalog."default"
12+
)PARTITION BY RANGE(event_hour);
13+
14+
15+
16+
/* Create 12 partition nodes for that above table */
17+
-- range start value is inclusive, end value is exclusive
18+
CREATE TABLE time_series.location_temp_p1 PARTITION OF time_series.location_temp_p
19+
FOR VALUES FROM (0) TO (2);
20+
21+
-- together we will create index event_time for each partition node table
22+
CREATE INDEX idx_loc_temp_p1 ON time_series.location_temp_p1(event_time);
23+
24+
25+
26+
/* continue creating another 11 nodes */
27+
CREATE TABLE time_series.location_temp_p2 PARTITION OF time_series.location_temp_p
28+
FOR VALUES FROM (2) TO (4);
29+
CREATE INDEX idx_loc_temp_p2 ON time_series.location_temp_p2(event_time);
30+
31+
32+
CREATE TABLE time_series.location_temp_p3 PARTITION OF time_series.location_temp_p
33+
FOR VALUES FROM (4) TO (6);
34+
CREATE INDEX idx_loc_temp_p3 ON time_series.location_temp_p3(event_time);
35+
36+
37+
CREATE TABLE time_series.location_temp_p4 PARTITION OF time_series.location_temp_p
38+
FOR VALUES FROM (6) TO (8);
39+
CREATE INDEX idx_loc_temp_p4 ON time_series.location_temp_p4(event_time);
40+
41+
42+
CREATE TABLE time_series.location_temp_p5 PARTITION OF time_series.location_temp_p
43+
FOR VALUES FROM (8) TO (10);
44+
CREATE INDEX idx_loc_temp_p5 ON time_series.location_temp_p5(event_time);
45+
46+
47+
CREATE TABLE time_series.location_temp_p6 PARTITION OF time_series.location_temp_p
48+
FOR VALUES FROM (10) TO (12);
49+
CREATE INDEX idx_loc_temp_p6 ON time_series.location_temp_p5(event_time);
50+
51+
52+
CREATE TABLE time_series.location_temp_p7 PARTITION OF time_series.location_temp_p
53+
FOR VALUES FROM (12) TO (14);
54+
CREATE INDEX idx_loc_temp_p7 ON time_series.location_temp_p7(event_time);
55+
56+
57+
CREATE TABLE time_series.location_temp_p8 PARTITION OF time_series.location_temp_p
58+
FOR VALUES FROM (14) TO (16);
59+
CREATE INDEX idx_loc_temp_p8 ON time_series.location_temp_p8(event_time);
60+
61+
62+
CREATE TABLE time_series.location_temp_p9 PARTITION OF time_series.location_temp_p
63+
FOR VALUES FROM (16) TO (18);
64+
CREATE INDEX idx_loc_temp_9 ON time_series.location_temp_p9(event_time);
65+
66+
67+
CREATE TABLE time_series.location_temp_p10 PARTITION OF time_series.location_temp_p
68+
FOR VALUES FROM (18) TO (20);
69+
CREATE INDEX idx_loc_temp_p10 ON time_series.location_temp_p10(event_time);
70+
71+
72+
CREATE TABLE time_series.location_temp_p11 PARTITION OF time_series.location_temp_p
73+
FOR VALUES FROM (20) TO (22);
74+
CREATE INDEX idx_loc_temp_p11 ON time_series.location_temp_p11(event_time);
75+
76+
77+
CREATE TABLE time_series.location_temp_p12 PARTITION OF time_series.location_temp_p
78+
FOR VALUES FROM (22) TO (24);
79+
CREATE INDEX idx_loc_temp_p12 ON time_series.location_temp_p12(event_time);
80+
81+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*********** Querying Partitioned Table Data ****************/
2+
3+
/* 1) get the average temp of march 5, 2019*/
4+
5+
-- non partition version on original table: cost 34,090.84
6+
EXPLAIN SELECT location_id, AVG(temp_celcius)
7+
FROM time_series.location_temp
8+
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06'
9+
GROUP BY location_id;
10+
11+
12+
-- 2) partitioned version on new table: cost 166.23
13+
EXPLAIN SELECT location_id, AVG(temp_celcius)
14+
FROM time_series.location_temp_p
15+
WHERE event_time BETWEEN '2019-03-05' AND '2019-03-06'
16+
GROUP BY location_id;
17+
18+
-- 3) partitioned version on new table using event hour: cost 78.52
19+
EXPLAIN SELECT location_id, AVG(temp_celcius)
20+
FROM time_series.location_temp_p
21+
WHERE event_hour BETWEEN 0 AND 4
22+
GROUP BY location_id;
23+
24+
/*
25+
In summary the closer WHERE clause is to our partition, the more benefit we can get from Partitioning our data.
26+
*/
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+

0 commit comments

Comments
 (0)