Skip to content

Commit a037694

Browse files
committed
designing for scalability - partitioning data
1 parent 6d8163b commit a037694

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Challenge\n",
8+
"\n",
9+
"Define a a sales history table partitioned by month of sale:\n",
10+
"\n",
11+
"- Months are numbers from 1 to 12\n",
12+
"- Attributes should include:\n",
13+
" - product ID\n",
14+
" - product name\n",
15+
" - product type\n",
16+
" - total units sold\n",
17+
" - month of sale\n",
18+
"- Create a primary key using month of sale and product ID"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"# Solution"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"**Create table with partitioned by range**\n",
33+
"\n",
34+
"`CREATE TABLE sales_history(\n",
35+
" product_id INT NOT NULL,\n",
36+
"\tproduct_name CHARACTER VARYING(50) NOT NULL,\n",
37+
"\tproduct_type CHARACTER VARYING(50) NOT NULL,\n",
38+
"\ttotal_units_sold INT NOT NULL,\n",
39+
"\tmonth_of_sale INT NOT NULL,\n",
40+
"\tPRIMARY KEY(month_of_sale, product_id)\n",
41+
") PARTITION BY RANGE(month_of_sale);`\n",
42+
"\n",
43+
"\n",
44+
"**create partition nodes tables**\n",
45+
"\n",
46+
"`CREATE TABLE sales_history_month_1 PARTITION OF sales_history\n",
47+
"FOR VALUES FROM (1) TO (2);`\n",
48+
"\n",
49+
"`CREATE TABLE sales_history_month_2 PARTITION OF sales_history\n",
50+
"FOR VALUES FROM (2) TO (3);`\n",
51+
"\n",
52+
"`CREATE TABLE sales_history_month_3 PARTITION OF sales_history\n",
53+
"FOR VALUES FROM (3) TO (4);`\n",
54+
"\n",
55+
"`CREATE TABLE sales_history_month_4 PARTITION OF sales_history\n",
56+
"FOR VALUES FROM (4) TO (5);`\n",
57+
"\n",
58+
"`CREATE TABLE sales_history_month_5 PARTITION OF sales_history\n",
59+
"FOR VALUES FROM (5) TO (6);`\n",
60+
"\n",
61+
"`CREATE TABLE sales_history_month_6 PARTITION OF sales_history\n",
62+
"FOR VALUES FROM (6) TO (7);`\n",
63+
"\n",
64+
"`CREATE TABLE sales_history_month_7 PARTITION OF sales_history\n",
65+
"FOR VALUES FROM (7) TO (8);`\n",
66+
"\n",
67+
"`CREATE TABLE sales_history_month_8 PARTITION OF sales_history\n",
68+
"FOR VALUES FROM (8) TO (9);`\n",
69+
"\n",
70+
"`CREATE TABLE sales_history_month_9 PARTITION OF sales_history\n",
71+
"FOR VALUES FROM (9) TO (10);`\n",
72+
"\n",
73+
"`CREATE TABLE sales_history_month_10 PARTITION OF sales_history\n",
74+
"FOR VALUES FROM (10) TO (11);`\n",
75+
"\n",
76+
"`CREATE TABLE sales_history_month_11 PARTITION OF sales_history\n",
77+
"FOR VALUES FROM (11) TO (12);`\n",
78+
"\n",
79+
"`CREATE TABLE sales_history_month_12 PARTITION OF sales_history\n",
80+
"FOR VALUES FROM (12) TO (13);`\n"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": []
89+
}
90+
],
91+
"metadata": {
92+
"kernelspec": {
93+
"display_name": "Python 3",
94+
"language": "python",
95+
"name": "python3"
96+
},
97+
"language_info": {
98+
"codemirror_mode": {
99+
"name": "ipython",
100+
"version": 3
101+
},
102+
"file_extension": ".py",
103+
"mimetype": "text/x-python",
104+
"name": "python",
105+
"nbconvert_exporter": "python",
106+
"pygments_lexer": "ipython3",
107+
"version": "3.8.5"
108+
}
109+
},
110+
"nbformat": 4,
111+
"nbformat_minor": 4
112+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Define a a sales history table partitioned by month of sale:
3+
4+
- Months are numbers from 1 to 12
5+
- Attributes should include:
6+
- product ID
7+
- product name
8+
- product type
9+
- total units sold
10+
- month of sale
11+
- Create a primary key using month of sale and product ID
12+
*/
13+
14+
-- Create table with partitioned by range
15+
CREATE TABLE sales_history(
16+
product_id INT NOT NULL,
17+
product_name CHARACTER VARYING(50) NOT NULL,
18+
product_type CHARACTER VARYING(50) NOT NULL,
19+
total_units_sold INT NOT NULL,
20+
month_of_sale INT NOT NULL,
21+
PRIMARY KEY(month_of_sale, product_id)
22+
) PARTITION BY RANGE(month_of_sale);
23+
24+
25+
-- create partition nodes tables
26+
CREATE TABLE sales_history_month_1 PARTITION OF sales_history
27+
FOR VALUES FROM (1) TO (2);
28+
29+
CREATE TABLE sales_history_month_2 PARTITION OF sales_history
30+
FOR VALUES FROM (2) TO (3);
31+
32+
CREATE TABLE sales_history_month_3 PARTITION OF sales_history
33+
FOR VALUES FROM (3) TO (4);
34+
35+
CREATE TABLE sales_history_month_4 PARTITION OF sales_history
36+
FOR VALUES FROM (4) TO (5);
37+
38+
CREATE TABLE sales_history_month_5 PARTITION OF sales_history
39+
FOR VALUES FROM (5) TO (6);
40+
41+
CREATE TABLE sales_history_month_6 PARTITION OF sales_history
42+
FOR VALUES FROM (6) TO (7);
43+
44+
CREATE TABLE sales_history_month_7 PARTITION OF sales_history
45+
FOR VALUES FROM (7) TO (8);
46+
47+
CREATE TABLE sales_history_month_8 PARTITION OF sales_history
48+
FOR VALUES FROM (8) TO (9);
49+
50+
CREATE TABLE sales_history_month_9 PARTITION OF sales_history
51+
FOR VALUES FROM (9) TO (10);
52+
53+
CREATE TABLE sales_history_month_10 PARTITION OF sales_history
54+
FOR VALUES FROM (10) TO (11);
55+
56+
CREATE TABLE sales_history_month_11 PARTITION OF sales_history
57+
FOR VALUES FROM (11) TO (12);
58+
59+
CREATE TABLE sales_history_month_12 PARTITION OF sales_history
60+
FOR VALUES FROM (12) TO (13);

0 commit comments

Comments
 (0)