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
drop table t_user;
create table t_user(
id int(7) primary key comment '主键',
name varchar(20) not null unique comment '姓名',
birthday date default '2022-02-08'
);

drop table t_account;
create table t_account(
id int(7),
blance double(5,2),
u_id int(7) ,
constraint t_account_id_pk primary key(id),
constraint t_account_id_fk foreign key(u_id) references t_user(id)
);



drop table t_student;
drop table t_course;
drop table t_sc

create table t_student(
id int(7) primary key,
name varchar(20) unique not null,
age int(7) default 20
);

create table t_course(
id int(7) primary key,
name varchar(20) not null
);

create table t_sc(
id int(7),
s_id int (7),
c_id int(7),
constraint t_sc_id_pk primary key(id),
constraint t_sc_s_id_fk foreign key(s_id) references t_student(id),
constraint t_sc_c_id_fk foreign key(c_id) references t_course(id)
);

insert into t_student values(1,'tom',20),(2,'cca',22),(3,'kkp',21),(4,'see',null);
insert into t_course values(1,'math'),(2,'english'),(3,'chinses'),(4,'chesmatical');
insert into t_sc values(1,1,1),(2,2,3),(3,4,2),(4,3,4);

1
2
3
4
5
6
7
8
9
10
11
12
13
--测试表格及数据
drop table hashTest;
create table hashTest(
id int(7) primary key,
name varchar(20),
age int(7)
)PARTITION BY HASH(id) PARTITIONS 4;;
insert into hashTest values(1,'tom',20),(2,'cca',22),(3,'kkp',21),(4,'see',null),(5,'tom',20),(6,'cca',22),(7,'kkp',21),(8,'see',null),(9,'tom',20),(10,'cca',22),(11,'kkp',21),(12,'see',null);

--查询分区结构
SELECT PARTITION_NAME,PARTITION_METHOD,PARTITION_EXPRESSION,PARTITION_DESCRIPTION,TABLE_ROWS
FROM `information_schema`.`PARTITIONS`
WHERE TABLE_SCHEMA=SCHEMA() AND TABLE_NAME='hashTest';

hash分区 PARTITIONS=4(4个分区,根据取模确定不同id的占据区域)

1
2
3
4
5
6
7
8
9
+----------------+------------------+----------------------+-----------------------+------------+
| PARTITION_NAME | PARTITION_METHOD | PARTITION_EXPRESSION | PARTITION_DESCRIPTION | TABLE_ROWS |
+----------------+------------------+----------------------+-----------------------+------------+
| p0 | HASH | `id` | NULL | 3 |
| p1 | HASH | `id` | NULL | 3 |
| p2 | HASH | `id` | NULL | 3 |
| p3 | HASH | `id` | NULL | 3 |
+----------------+------------------+----------------------+-----------------------+------------+

Range分区 分成6个分区

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
drop table hashTest;
create table hashTest(
id int(7) primary key,
name varchar(20),
age int(7)
)
PARTITION BY RANGE (id) PARTITIONS 6 (
PARTITION part0 VALUES LESS THAN (2) ,
PARTITION part1 VALUES LESS THAN (4) ,
PARTITION part2 VALUES LESS THAN (6) ,
PARTITION part3 VALUES LESS THAN (8) ,
PARTITION part4 VALUES LESS THAN (10),
PARTITION part5 VALUES LESS THAN (13)
);
insert into hashTest values(1,'tom',20),(2,'cca',22),(3,'kkp',21),(4,'see',null),(5,'tom',20),(6,'cca',22),(7,'kkp',21),(8,'see',null),(9,'tom',20),(10,'cca',22),(11,'kkp',21),(12,'see',null);

分区结果:
+----------------+------------------+----------------------+-----------------------+------------+
| PARTITION_NAME | PARTITION_METHOD | PARTITION_EXPRESSION | PARTITION_DESCRIPTION | TABLE_ROWS |
+----------------+------------------+----------------------+-----------------------+------------+
| part0 | RANGE | `id` | 2 | 1 |
| part1 | RANGE | `id` | 4 | 2 |
| part2 | RANGE | `id` | 6 | 2 |
| part3 | RANGE | `id` | 8 | 2 |
| part4 | RANGE | `id` | 10 | 2 |
| part5 | RANGE | `id` | 13 | 3 |
+----------------+------------------+----------------------+-----------------------+------------+

list分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
drop table hashTest;
create table hashTest(
id int(7) primary key,
name varchar(20),
age int(7)
)

PARTITION BY LIST(id) (
PARTITION pNorth VALUES IN (1,2,3),
PARTITION pEast VALUES IN (5,6,7),
PARTITION pWest VALUES IN (4,8,9),
PARTITION pCentral VALUES IN (10,11,12)
);
insert into hashTest values(1,'tom',20),(2,'cca',22),(3,'kkp',21),(4,'see',null),(5,'tom',20),(6,'cca',22),(7,'kkp',21),(8,'see',null),(9,'tom',20),(10,'cca',22),(11,'kkp',21),(12,'see',null);
--分区结果:
+----------------+------------------+----------------------+-----------------------+------------+
| PARTITION_NAME | PARTITION_METHOD | PARTITION_EXPRESSION | PARTITION_DESCRIPTION | TABLE_ROWS |
+----------------+------------------+----------------------+-----------------------+------------+
| pCentral | LIST | `id` | 10,11,12 | 3 |
| pEast | LIST | `id` | 5,6,7 | 3 |
| pNorth | LIST | `id` | 1,2,3 | 3 |
| pWest | LIST | `id` | 4,8,9 | 3 |
+----------------+------------------+----------------------+-----------------------+------------+

key分区(和hash分区类似,其分区函数是由mysql内部确定的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
drop table hashTest;
create table hashTest(
id int(7) primary key,
name varchar(20),
age int(7)
)
PARTITION BY KEY()
PARTITIONS 2;
insert into hashTest values(1,'tom',20),(2,'cca',22),(3,'kkp',21),(4,'see',null),(5,'tom',20),(6,'cca',22),(7,'kkp',21),(8,'see',null),(9,'tom',20),(10,'cca',22),(11,'kkp',21),(12,'see',null);

--分区结果
+----------------+------------------+----------------------+-----------------------+------------+
| PARTITION_NAME | PARTITION_METHOD | PARTITION_EXPRESSION | PARTITION_DESCRIPTION | TABLE_ROWS |
+----------------+------------------+----------------------+-----------------------+------------+
| p0 | KEY | NULL | NULL | 6 |
| p1 | KEY | NULL | NULL | 6 |
+----------------+------------------+----------------------+-----------------------+------------+