Skip to content

Commit ff4b0dc

Browse files
committed
partition data - range, list, hash
1 parent ee49b8d commit ff4b0dc

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/************ Partitioning Data ***************/
2+
3+
---------- Partition by Range ---------------------------
4+
5+
/* creat new table with partition table (with partition key) */
6+
CREATE TABLE iot_measurement(
7+
location_id INT NOT NULL,
8+
measure_date DATE NOT NULL,
9+
temp_celcius INT,
10+
rel_humidity INT
11+
) PARTITION BY RANGE(measure_date);
12+
13+
/* If we check iot_measurement table > Partitions > we will see No Nodes yet, because none has been created */
14+
15+
/* Create partition Nodes and add to the original table */
16+
17+
CREATE TABLE iot_measurement_week1_2019 PARTITION OF iot_measurement
18+
FOR VALUES FROM ('2019-01-01') TO ('2019-01-08');
19+
20+
CREATE TABLE iot_measurement_week2_2019 PARTITION OF iot_measurement
21+
FOR VALUES FROM ('2019-01-08') TO ('2019-01-15');
22+
23+
CREATE TABLE iot_measurement_week3_2019 PARTITION OF iot_measurement
24+
FOR VALUES FROM ('2019-01-15') TO ('2019-01-22');
25+
26+
27+
/*
28+
If we check iot_measurement table > Partitions > we will see created 3 Nodes.
29+
Now, we can create constraints, indexes, triggers on each node.
30+
Notice that there is no columns, because columns are inherited from Parent Table.
31+
*/
32+
33+
34+
---------- Partition by List ---------------------------
35+
36+
/* create table with partition by list */
37+
CREATE TABLE products(
38+
product_id INT NOT NULL,
39+
product_name TEXT NOT NULL,
40+
product_short_desc TEXT NOT NULL,
41+
product_long_desc TEXT NOT NULL,
42+
product_category VARCHAR NOT NULL
43+
)PARTITION BY LIST(product_category);
44+
45+
/* create partitions nodes */
46+
CREATE TABLE product_clothing PARTITION OF products
47+
FOR VALUES IN('casual_clothing', 'business_attire', 'formal_clothing');
48+
49+
CREATE TABLE product_electronics PARTITION OF products
50+
FOR VALUES IN('mobile_phones', 'tablets', 'laptop_computers');
51+
52+
CREATE TABLE product_kitchen PARTITION OF products
53+
FOR VALUES IN ('food_processors', 'cutlery', 'blenders');
54+
55+
56+
57+
---------- Partition by Hash ---------------------------
58+
59+
/* create table with partition by hash */
60+
CREATE TABLE customer_interaction(
61+
cid INT NOT NULL,
62+
ci_url TEXT NOT NULL,
63+
time_at_url INT NOT NULL,
64+
click_sequence INT NOT NULL
65+
)PARTITION BY HASH(cid);
66+
67+
/*
68+
Create partitions nodes
69+
As we want 5 partitions, we will use Modulus of 5.
70+
*/
71+
-- for first partition, every customer id divided by 5 with remainder of getting 0 will be put into this block of partition.
72+
CREATE TABLE customer_interaction_1 PARTITION OF customer_interaction
73+
FOR VALUES WITH (MODULUS 5, REMAINDER 0);
74+
75+
CREATE TABLE customer_interaction_2 PARTITION OF customer_interaction
76+
FOR VALUES WITH (MODULUS 5, REMAINDER 1);
77+
78+
CREATE TABLE customer_interaction_3 PARTITION OF customer_interaction
79+
FOR VALUES WITH (MODULUS 5, REMAINDER 2);
80+
81+
CREATE TABLE customer_interaction_4 PARTITION OF customer_interaction
82+
FOR VALUES WITH (MODULUS 5, REMAINDER 3);
83+
84+
CREATE TABLE customer_interaction_5 PARTITION OF customer_interaction
85+
FOR VALUES WITH (MODULUS 5, REMAINDER 4);

0 commit comments

Comments
 (0)