时间:2021-07-01 10:21:17 帮助过:2人阅读

对于mysql来讲,现阶段支持分区操作的版本有5.1和5.5.如图显示为YES则表明该数据库支持分区操作。
俗称:范围分区。根据表的字段的值,依据给定某段连续的区间来分区。
直接创建表时分区
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
create table teacher
(id varchar(20) not null ,
name varchar(20),
age varchar(20),
birthdate date not null,
salary int
)
partition by range(year(birthdate))
(
partition p1 values less than (1970),
partition p2 values less than (1990),
partition p3 values less than maxvalue
);
|
Ps:创建teacher表,并在创建teacher表同时根据birthdate字段将表划分为p1、p2、p3三个分区。
在创建表后分区
| 1 2 3 4 5 6 7 |
ALTER TABLE teacher
partition by range(year(birthdate))
(
partition p1 values less than (1970),
partition p2 values less than (1990),
partition p3 values less than maxvalue
);
|
Ps:给已经创建了的表分区,分为p1、p2、p3.
俗名:列表分区。其实list分区和range分区应该说都是一样的,不同的是range分区在分区是的依据是一段连续的区间;而list分区针对的分区依据是一组分布的散列值。
| 1 2 3 4 5 6 7 8 9 10 11 12 |
create table student
(id varchar(20) not null ,
studentno int(20) not null,
name varchar(20),
age varchar(20)
)
partition by list(studentno)
(
partition p1 values in (1,2,3,4),
partition p2 values in (5,6,7,8),
partition p3 values in (9,10,11)
);
|
Ps:如上创建表student,并将student表分为p1、p2、p3三个分区。需要注意的是一般情况下,针对表的分区字段为int等数值类型。
小名:哈希分区。哈希分区主要是依据表的某个字段以及指定分区的数量。
| 1 2 3 4 5 6 7 |
create table user (
id int(20) not null,
role varchar(20) not null,
description varchar(50)
)
partition by hash(id)
partitions 10;
|
Ps:如上创建user表,并将user表平均分为十个分区。比较有限制的就是需要知道表的数据有多少才能更好平均分配分区。
类似于按HASH分区,区别在于KEY分区只支持计算一列或多列,且MySQL服务器提供其自身的哈希函数。必须有一列或多列包含整数值。
| 1 2 3 |
create table role( id int(20) not null,name varchar(20) not null)
partition by linear key(id)
partitions 10;
|
对指定表添加分区
| 1 |
alter table user add partition(partition p4 values less than MAXVALUE);
|
删除指定表指定分区
alter table student drop partition p1;
创建子分区
| 1 2 3 4 5 6 7 8 |
create table role_subp(id int(20) not null,name int(20) not null)
partition by list(id)
subpartition by hash(name)
subpartitions 3
(
partition p1 values in(10),
partition p2 values in(20)
)
|
复合分区
?
| 1 2 3 |
alter table user
reorganize partition p1,p3 into
(partition p1 values less than (1000));
|
mysql 分区
标签: