==先看结果==
有两个结构一样的表,no_part_tab 和 part_tab,一个使用了分区,一个不使用,结果如下,
神奇的MySQL分区
。?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
mysql> select * from no_part_tab where c1 = '80000';
+-------+--------------------+------------+
| c1 | c2 | c3 |
+-------+--------------------+------------+
| 80000 | testing partitions | 1995-05-25 |
+-------+--------------------+------------+
1 row in set (1.57 sec)
mysql> select * from part_tab where c1 = '80000';
+-------+--------------------+------------+
| c1 | c2 | c3 |
+-------+--------------------+------------+
| 80000 | testing partitions | 1995-05-25 |
+-------+--------------------+------------+
1 row in set (0.02 sec)
mysql> update part_tab set c2='zhmsong' where c1 = '80000';
Query OK, 1 row affected (0.21 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update no_part_tab set c2='zhmsong' where c1 = '80000';
Query OK, 1 row affected (15.55 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from no_part_tab where c1 = '80000';
Query OK, 1 row affected (1.46 sec)
mysql> delete from part_tab where c1 = '80000';
Query OK, 1 row affected (0.02 sec)
mysql> select count(*) from no_part_tab;
+----------+
| count(*) |
+----------+
| 7999999 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from part_tab;
+----------+
| count(*) |
+----------+
| 7999999 |
+----------+
1 row in set (0.01 sec)
mysql> select count(*) from part_tab where c1 >= '80000' and c1 <= '150000';
+----------+
| count(*) |
+----------+
| 70000 |
+----------+
1 row in set (0.05 sec)
mysql> select count(*) from no_part_tab where c1 >= '80000' and c1 <= '150000';
+----------+
| count(*) |
+----------+
| 70000 |
+----------+
1 row in set (1.89 sec)
mysql> select count(*) from no_part_tab where c2 like '%test%';
+----------+
| count(*) |
+----------+
| 7999999 |
+----------+
1 row in set (1.52 sec)
mysql> select count(*) from part_tab where c2 like '%test%';
+----------+
| count(*) |
+----------+
| 7999999 |
+----------+
1 row in set (1.59 sec)
mysql> select * from no_part_tab where c2 like '%zhmsong%' limit 10 offset 100;
+---------+---------+------------+
| c1 | c2 | c3 |
+---------+---------+------------+
| 1100100 | zhmsong | 2004-05-13 |
| 1100101 | zhmsong | 1999-05-21 |
| 1100102 | zhmsong | 2004-05-26 |
| 1100103 | zhmsong | 1999-06-03 |
| 1100104 | zhmsong | 2004-06-09 |
| 1100105 | zhmsong | 1999-06-16 |
| 1100106 | zhmsong | 2004-06-22 |
| 1100107 | zhmsong | 1999-06-30 |
| 1100108 | zhmsong | 2004-07-05 |
| 1100109 | zhmsong | 1999-07-13 |
+---------+---------+------------+
10 rows in set (0.22 sec)
mysql> select * from part_tab where c2 like '%zhmsong%' limit 10 offset 100;
+---------+---------+------------+
| c1 | c2 | c3 |
+---------+---------+------------+
| 1100100 | zhmsong | 2004-05-13 |
| 1100101 | zhmsong | 1999-05-21 |
| 1100102 | zhmsong | 2004-05-26 |
| 1100103 | zhmsong | 1999-06-03 |
| 1100104 | zhmsong | 2004-06-09 |
| 1100105 | zhmsong | 1999-06-16 |
| 1100106 | zhmsong | 2004-06-22 |
| 1100107 | zhmsong | 1999-06-30 |
| 1100108 | zhmsong | 2004-07-05 |
| 1100109 | zhmsong | 1999-07-13 |
+---------+---------+------------+
10 rows in set (0.22 sec)
== 实现==
01
DROP TABLE IF EXISTS part_tab;
02
CREATE TABLE part_tab (
03
c1 int default NULL,
04
c2 varchar(30) default NULL,
05
c3 date default NULL
06
) engine=myisam
07
PARTITION BY RANGE (c1) (
08
PARTITION p1 VALUES LESS THAN (100000),
09
PARTITION p2 VALUES LESS THAN (200000),
10
PARTITION p3 VALUES LESS THAN (300000),
11
PARTITION p4 VALUES LESS THAN (400000),
12
PARTITION p5 VALUES LESS THAN (500000),
13
PARTITION p6 VALUES LESS THAN (600000),
14
PARTITION p7 VALUES LESS THAN (700000),
15
PARTITION p8 VALUES LESS THAN (800000),
16
PARTITION p9 VALUES LESS THAN (900000),
17
PARTITION p10 VALUES LESS THAN (1000000),
18
PARTITION p11 VALUES LESS THAN (1100000),
19
PARTITION p12 VALUES LESS THAN (1200000),
20
PARTITION p13 VALUES LESS THAN (1300000),
21
PARTITION p14 VALUES LESS THAN (1400000),
22
PARTITION p15 VALUES LESS THAN (1500000),
23
PARTITION p16 VALUES LESS THAN (1600000),
24
PARTITION p17 VALUES LESS THAN (1700000),
25
PARTITION p18 VALUES LESS THAN (1800000),
26
PARTITION p19 VALUES LESS THAN (1900000),
27
PARTITION p20 VALUES LESS THAN (2000000),
28
PARTITION p21 VALUES LESS THAN (2100000),
29
PARTITION p22 VALUES LESS THAN (2200000),
30
PARTITION p23 VALUES LESS THAN (2300000),
31
PARTITION p24 VALUES LESS THAN (2400000),
32
PARTITION p25 VALUES LESS THAN (2500000),
33
PARTITION p26 VALUES LESS THAN (2600000),
34
PARTITION p27 VALUES LESS THAN (2700000),
35
PARTITION p28 VALUES LESS THAN (2800000),
36
PARTITION p29 VALUES LESS THAN (2900000),
37
PARTITION p30 VALUES LESS THAN (3000000),
38
PARTITION p31 VALUES LESS THAN MAXVALUE
39
);
40
41
DROP TABLE IF EXISTS no_part_tab;
42
create table no_part_tab (
43
c1 int(11) default NULL,
44
c2 varchar(30) default NULL,
45
c3 date default NULL
46
) engine=myisam;
47
48
DROP PROCEDURE IF EXISTS load_part_tab;
49
50
delimiter //
51
52
create PROCEDURE load_part_tab()
53
begin
54
declare v int default 0;
55
while v < 8000000
56
do
57
insert into part_tab values (v,'testing partitions',adddate('1995-01-01',(rand(v)*36520) mod 3652));
58
set v = v + 1;
59
end while;
60
end
61
//
62
delimiter ;