用户登录
用户注册

分享至

mysql分区功能详细介绍,以及实例

  • 作者: 爱吃水果沙拉的秋天
  • 来源: 51数据库
  • 2022-04-06

一,什么是分区

前段时间写过一篇关于mysql分表的的文章,下面来说一下什么是数据库分区,以mysql为例。my中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面(可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件,一个是frm存放表结构的,一个是myd存放表数据的,一个是myi存表索引的。如果一张表的数据量太大的话,那么myd,myi就会变的很大,查找数据就会变的很慢,这个时候我们可以利用mysql的分区功能,在物理上将这一张表对应的三个文件,分割成许多个小块,这样呢,我们查找一条数据时,就不用全部查找了,只要知道这条数据在哪一块,然后在那一块找就行了。如果表的数据太大,可能一个磁盘放不下,这个时候,我们可以把数据分配到不同的磁盘里面去。

分区的二种方式

1,横向分区

什么是横向分区呢?就是横着来分区了,举例来说明一下,假如有100w条数据,分成十份,前10w条数据放到第一个分区,第二个10w条数据放到第二个分区,依此类推。也就是把表分成了十分,根用merge来分表,有点像哦。取出一条数据的时候,这条数据包含了表结构中的所有字段,也就是说横向分区,并没有改变表的结构。

2,纵向分区

什么是纵向分区呢?就是竖来分区了,举例来说明,在设计用户表的时候,开始的时候没有考虑好,而把个人的所有信息都放到了一张表里面去,这样这个表里面就会有比较大的字段,如个人简介,而这些简介呢,也许不会有好多人去看,所以等到有人要看的时候,在去查找,分表的时候,可以把这样的大字段,分开来。

感觉数据库的分区好像是切苹果,到底是横着切呢,还是竖着切,根据个人喜好了,mysql提供的分区属于第一种,横向分区,并且细分成很多种方式。下面将举例说明一下。

二,mysql的分区

我觉着吧,mysql的分区只有一种方式,只不过运用不同的算法,規则将数据分配到不同的区块中而已。

1,mysql5.1及以上支持分区功能

安装安装的时候,我们就可以查看一下

查看复制打印?

[root@blackghost mysql-5.1.50]# ./configure --help |grep -a 3 partition

=== partition support ===

plugin name: partition

description: mysql partitioning support

supports build: static

configurations: max, max-no-ndb [php] view plain copy

[root@blackghost mysql-5.1.50]# ./configure --help |grep -a 3 partition

=== partition support ===

plugin name: partition

description: mysql partitioning support

supports build: static

configurations: max, max-no-ndb

查看一下,如果发现有上面这个东西,说明他是支持分区的,默认是打开的。如果你已经安装过了mysql的话

查看复制打印?

mysql> show variables like "%part%";

+-------------------+-------+

| variable_name | value |

+-------------------+-------+

| have_partitioning | yes |

+-------------------+-------+

1 row in set (0.00 sec) [php] view plain copy

mysql> show variables like "%part%";

+-------------------+-------+

| variable_name | value |

+-------------------+-------+

| have_partitioning | yes |

+-------------------+-------+

1 row in set (0.00 sec)

查看一下变量,如果支持的话,会有上面的提示的。

2,range分区

按照range分区的表是通过如下一种方式进行分区的,每个分区包含那些分区表达式的值位于一个给定的连续区间内的行

查看复制打印?

//创建range分区表

mysql> create table if not exists `user` (

-> `id` int(11) not null auto_increment comment '用户id',

-> `name` varchar(50) not null default '' comment '名称',

-> `sex` int(1) not null default '0' comment '0为男,1为女',

-> primary key (`id`)

-> ) engine=myisam default charset=utf-8 auto_increment=1

-> partition by range (id) (

-> partition p0 values less than (3),

-> partition p1 values less than (6),

-> partition p2 values less than (9),

-> partition p3 values less than (12),

-> partition p4 values less than maxvalue

-> );

query ok, 0 rows affected (0.13 sec)

//插入一些数据

mysql> insert into `test`.`user` (`name` ,`sex`)values ('tank', '0')

-> ,('zhang',1),('ying',1),('张',1),('映',0),('test1',1),('tank2',1)

-> ,('tank1',1),('test2',1),('test3',1),('test4',1),('test5',1),('tank3',1)

-> ,('tank4',1),('tank5',1),('tank6',1),('tank7',1),('tank8',1),('tank9',1)

-> ,('tank10',1),('tank11',1),('tank12',1),('tank13',1),('tank21',1),('tank42',1);

query ok, 25 rows affected (0.05 sec)

records: 25 duplicates: 0 warnings: 0

//到存放数据库表文件的地方看一下,my.cnf里面有配置,datadir后面就是

[root@blackghost test]# ls |grep user |xargs du -sh

4.0k user#p#p0.myd

4.0k user#p#p0.myi

4.0k user#p#p1.myd

4.0k user#p#p1.myi

4.0k user#p#p2.myd

4.0k user#p#p2.myi

4.0k user#p#p3.myd

4.0k user#p#p3.myi

4.0k user#p#p4.myd

4.0k user#p#p4.myi

12k user.frm

4.0k user.par

//取出数据

mysql> select count(id) as count from user;

+-------+

| count |

+-------+

| 25 |

+-------+

1 row in set (0.00 sec)

//删除第四个分区

mysql> alter table user drop partition p4;

query ok, 0 rows affected (0.11 sec)

records: 0 duplicates: 0 warnings: 0

/**存放在分区里面的数据丢失了,第四个分区里面有14条数据,剩下的3个分区

只有11条数据,但是统计出来的文件大小都是4.0k,从这儿我们可以看出分区的

最小区块是4k

*/

mysql> select count(id) as count from user;

+-------+

| count |

+-------+

| 11 |

+-------+

1 row in set (0.00 sec)

//第四个区块已删除

[root@blackghost test]# ls |grep user |xargs du -sh

4.0k user#p#p0.myd

4.0k user#p#p0.myi

4.0k user#p#p1.myd

4.0k user#p#p1.myi

4.0k user#p#p2.myd

4.0k user#p#p2.myi

4.0k user#p#p3.myd

4.0k user#p#p3.myi

12k user.frm

4.0k user.par

/*可以对现有表进行分区,并且会按規则自动的将表中的数据分配相应的分区

中,这样就比较好了,可以省去很多事情,看下面的操作*/

mysql> alter table aa partition by range(id)

-> (partition p1 values less than (1),

-> partition p2 values less than (5),

-> partition p3 values less than maxvalue);

query ok, 15 rows affected (0.21 sec) //对15数据进行分区

records: 15 duplicates: 0 warnings: 0

//总共有15条

mysql> select count(*) from aa;

+----------+

| count(*) |

+----------+

| 15 |

+----------+

1 row in set (0.00 sec)

//删除一个分区

mysql> alter table aa drop partition p2;

query ok, 0 rows affected (0.30 sec)

records: 0 duplicates: 0 warnings: 0

//只有11条了,说明对现有的表分区成功了

mysql> select count(*) from aa;

+----------+

| count(*) |

+----------+

| 11 |

+----------+

1 row in set (0.00 sec) [php] view plain copy

//创建range分区表

mysql> create table if not exists `user` (

-> `id` int(11) not null auto_increment comment '用户id',

-> `name` varchar(50) not null default '' comment '名称',

-> `sex` int(1) not null default '0' comment '0为男,1为女',

-> primary key (`id`)

-> ) engine=myisam default charset=utf-8 auto_increment=1

-> partition by range (id) (

-> partition p0 values less than (3),

-> partition p1 values less than (6),

-> partition p2 values less than (9),

-> partition p3 values less than (12),

-> partition p4 values less than maxvalue

-> );

query ok, 0 rows affected (0.13 sec)

//插入一些数据

mysql> insert into `test`.`user` (`name` ,`sex`)values ('tank', '0')

-> ,('zhang',1),('ying',1),('张',1),('映',0),('test1',1),('tank2',1)

-> ,('tank1',1),('test2',1),('test3',1),('test4',1),('test5',1),('tank3',1)

-> ,('tank4',1),('tank5',1),('tank6',1),('tank7',1),('tank8',1),('tank9',1)

-> ,('tank10',1),('tank11',1),('tank12',1),('tank13',1),('tank21',1),('tank42',1);

query ok, 25 rows affected (0.05 sec)

records: 25 duplicates: 0 warnings: 0

//到存放数据库表文件的地方看一下,my.cnf里面有配置,datadir后面就是

[root@blackghost test]# ls |grep user |xargs du -sh

4.0k user#p#p0.myd

4.0k user#p#p0.myi

4.0k user#p#p1.myd

4.0k user#p#p1.myi

4.0k user#p#p2.myd

4.0k user#p#p2.myi

4.0k user#p#p3.myd

4.0k user#p#p3.myi

4.0k user#p#p4.myd

4.0k user#p#p4.myi

12k user.frm

4.0k user.par

//取出数据

mysql> select count(id) as count from user;

+-------+

| count |

+-------+

| 25 |

+-------+

1 row in set (0.00 sec)

//删除第四个分区

mysql> alter table user drop partition p4;

query ok, 0 rows affected (0.11 sec)

records: 0 duplicates: 0 warnings: 0

/**存放在分区里面的数据丢失了,第四个分区里面有14条数据,剩下的3个分区

只有11条数据,但是统计出来的文件大小都是4.0k,从这儿我们可以看出分区的

最小区块是4k

*/

mysql> select count(id) as count from user;

+-------+

| count |

+-------+

| 11 |

+-------+

1 row in set (0.00 sec)

//第四个区块已删除

[root@blackghost test]# ls |grep user |xargs du -sh

4.0k user#p#p0.myd

4.0k user#p#p0.myi

4.0k user#p#p1.myd

4.0k user#p#p1.myi

4.0k user#p#p2.myd

4.0k user#p#p2.myi

4.0k user#p#p3.myd

4.0k user#p#p3.myi

12k user.frm

4.0k user.par

/*可以对现有表进行分区,并且会按規则自动的将表中的数据分配相应的分区

中,这样就比较好了,可以省去很多事情,看下面的操作*/

mysql> alter table aa partition by range(id)

-> (partition p1 values less than (1),

-> partition p2 values less than (5),

-> partition p3 values less than maxvalue);

query ok, 15 rows affected (0.21 sec) //对15数据进行分区

records: 15 duplicates: 0 warnings: 0

//总共有15条

mysql> select count(*) from aa;

+----------+

| count(*) |

+----------+

| 15 |

+----------+

1 row in set (0.00 sec)

//删除一个分区

mysql> alter table aa drop partition p2;

query ok, 0 rows affected (0.30 sec)

records: 0 duplicates: 0 warnings: 0

//只有11条了,说明对现有的表分区成功了

mysql> select count(*) from aa;

+----------+

| count(*) |

+----------+

| 11 |

+----------+

1 row in set (0.00 sec)

3,list分区

list分区中每个分区的定义和选择是基于某列的值从属于一个值列表集中的一个值,而range分 区是从属于一个连续区间值的集合。

查看复制打印?

//这种方式失败

mysql> create table if not exists `list_part` (

-> `id` int(11) not null auto_increment comment '用户id',

-> `province_id` int(2) not null default 0 comment '省',

-> `name` varchar(50) not null default '' comment '名称',

-> `sex` int(1) not null default '0' comment '0为男,1为女',

-> primary key (`id`)

-> ) engine=innodb default charset=utf-8 auto_increment=1

-> partition by list (province_id) (

-> partition p0 values in (1,2,3,4,5,6,7,8),

-> partition p1 values in (9,10,11,12,16,21),

-> partition p2 values in (13,14,15,19),

-> partition p3 values in (17,18,20,22,23,24)

-> );

error 1503 (hy000): a primary key must include all columns in the table's partitioning function

//这种方式成功

mysql> create table if not exists `list_part` (

-> `id` int(11) not null comment '用户id',

-> `province_id` int(2) not null default 0 comment '省',

-> `name` varchar(50) not null default '' comment '名称',

-> `sex` int(1) not null default '0' comment '0为男,1为女'

-> ) engine=innodb default charset=utf-8

-> partition by list (province_id) (

-> partition p0 values in (1,2,3,4,5,6,7,8),

-> partition p1 values in (9,10,11,12,16,21),

-> partition p2 values in (13,14,15,19),

-> partition p3 values in (17,18,20,22,23,24)

-> );

query ok, 0 rows affected (0.33 sec) [javascript] view plain copy

//这种方式失败

mysql> create table if not exists `list_part` (

-> `id` int(11) not null auto_increment comment '用户id',

-> `province_id` int(2) not null default 0 comment '省',

-> `name` varchar(50) not null default '' comment '名称',

-> `sex` int(1) not null default '0' comment '0为男,1为女',

-> primary key (`id`)

-> ) engine=innodb default charset=utf-8 auto_increment=1

-> partition by list (province_id) (

-> partition p0 values in (1,2,3,4,5,6,7,8),

-> partition p1 values in (9,10,11,12,16,21),

-> partition p2 values in (13,14,15,19),

-> partition p3 values in (17,18,20,22,23,24)

-> );

error 1503 (hy000): a primary key must include all columns in the table's partitioning function

//这种方式成功

mysql> create table if not exists `list_part` (

-> `id` int(11) not null comment '用户id',

-> `province_id` int(2) not null default 0 comment '省',

-> `name` varchar(50) not null default '' comment '名称',

-> `sex` int(1) not null default '0' comment '0为男,1为女'

-> ) engine=innodb default charset=utf-8

-> partition by list (province_id) (

-> partition p0 values in (1,2,3,4,5,6,7,8),

-> partition p1 values in (9,10,11,12,16,21),

-> partition p2 values in (13,14,15,19),

-> partition p3 values in (17,18,20,22,23,24)

-> );

query ok, 0 rows affected (0.33 sec)

上面的这个创建list分区时,如果有主銉的话,分区时主键必须在其中,不然就会报错。如果我不用主键,分区就创建成功了,一般情况下,一个张表肯定会有一个主键,这算是一个分区的局限性吧。

如果对数据进行测试,请参考range分区的测试来操作

4,hash分区

hash分区主要用来确保数据在预先确定数目的分区中平均分布,你所要做的只是基于将要被哈希的列值指定一个列值或表达式,以 及指定被分区的表将要被分割成的分区数量。

查看复制打印?

mysql> create table if not exists `hash_part` (

-> `id` int(11) not null auto_increment comment '评论id',

-> `comment` varchar(1000) not null default '' comment '评论',

-> `ip` varchar(25) not null default '' comment '来源ip',

-> primary key (`id`)

-> ) engine=innodb default charset=utf-8 auto_increment=1

-> partition by hash(id)

-> partitions 3;

query ok, 0 rows affected (0.06 sec)[php]view plain copy

mysql> create table if not exists `hash_part` (

-> `id` int(11) not null auto_increment comment '评论id',

-> `comment` varchar(1000) not null default '' comment '评论',

-> `ip` varchar(25) not null default '' comment '来源ip',

-> primary key (`id`)

-> ) engine=innodb default charset=utf-8 auto_increment=1

-> partition by hash(id)

-> partitions 3;

query ok, 0 rows affected (0.06 sec)

测试请参考range分区的操作

5,key分区

按照key进行分区类似于按照hash分区,除了hash分区使用的用 户定义的表达式,而key分区的 哈希函数是由mysql 服务器提供。

查看复制打印?

mysql> create table if not exists `key_part` (

-> `news_id` int(11) not null comment '新闻id',

-> `content` varchar(1000) not null default '' comment '新闻内容',

-> `u_id` varchar(25) not null default '' comment '来源ip',

-> `create_time` date not null default '0000-00-00 00:00:00' comment '时间'

-> ) engine=innodb default charset=utf-8

-> partition by linear hash(year(create_time))

-> partitions 3;

query ok, 0 rows affected (0.07 sec)[php]view plain copy

mysql> create table if not exists `key_part` (

-> `news_id` int(11) not null comment '新闻id',

-> `content` varchar(1000) not null default '' comment '新闻内容',

-> `u_id` varchar(25) not null default '' comment '来源ip',

-> `create_time` date not null default '0000-00-00 00:00:00' comment '时间'

-> ) engine=innodb default charset=utf-8

-> partition by linear hash(year(create_time))

-> partitions 3;

query ok, 0 rows affected (0.07 sec)

测试请参考range分区的操作

6,子分区

子分区是分区表中每个分区的再次分割,子分区既可以使用hash希分区,也可以使用key分区。这 也被称为复合分区(composite partitioning)。

1,如果一个分区中创建了子分区,其他分区也要有子分区

2,如果创建了了分区,每个分区中的子分区数必有相同

3,同一分区内的子分区,名字不相同,不同分区内的子分区名子可以相同(5.1.50不适用)

查看复制打印?

mysql> create table if not exists `sub_part` (

-> `news_id` int(11) not null comment '新闻id',

-> `content` varchar(1000) not null default '' comment '新闻内容',

-> `u_id` int(11) not null default 0s comment '来源ip',

-> `create_time` date not null default '0000-00-00 00:00:00' comment '时间'

-> ) engine=innodb default charset=utf-8

-> partition by range(year(create_time))

-> subpartition by hash(to_days(create_time))(

-> partition p0 values less than (1990)(subpartition s0,subpartition s1,subpartition s2),

-> partition p1 values less than (2000)(subpartition s3,subpartition s4,subpartition good),

-> partition p2 values less than maxvalue(subpartition tank0,subpartition tank1,subpartition tank3)

-> );

query ok, 0 rows affected (0.07 sec)[php]view plain copy

mysql> create table if not exists `sub_part` (

-> `news_id` int(11) not null comment '新闻id',

-> `content` varchar(1000) not null default '' comment '新闻内容',

-> `u_id` int(11) not null default 0s comment '来源ip',

-> `create_time` date not null default '0000-00-00 00:00:00' comment '时间'

-> ) engine=innodb default charset=utf-8

-> partition by range(year(create_time))

-> subpartition by hash(to_days(create_time))(

-> partition p0 values less than (1990)(subpartition s0,subpartition s1,subpartition s2),

-> partition p1 values less than (2000)(subpartition s3,subpartition s4,subpartition good),

-> partition p2 values less than maxvalue(subpartition tank0,subpartition tank1,subpartition tank3)

-> );

query ok, 0 rows affected (0.07 sec)

官方网站说不同分区内的子分区可以有相同的名字,但是mysql5.1.50却不行会提示以下错误

error 1517 (hy000): duplicate partition name s1

三,分区管理

1,删除分区

mysql> alter table user drop partition p4;[php]view plain copy

mysql> alter table user drop partition p4;

2,新增分区

查看复制打印?

//range添加新分区

mysql> alter table user add partition(partition p4 values less than maxvalue);

query ok, 0 rows affected (0.06 sec)

records: 0 duplicates: 0 warnings: 0

//list添加新分区

mysql> alter table list_part add partition(partition p4 values in (25,26,28));

query ok, 0 rows affected (0.01 sec)

records: 0 duplicates: 0 warnings: 0

//hash重新分区

mysql> alter table hash_part add partition partitions 4;

query ok, 0 rows affected (0.12 sec)

records: 0 duplicates: 0 warnings: 0

//key重新分区

mysql> alter table key_part add partition partitions 4;

query ok, 1 row affected (0.06 sec) //有数据也会被重新分配

records: 1 duplicates: 0 warnings: 0

//子分区添加新分区,虽然我没有指定子分区,但是会给子分区命名的

mysql> alter table sub1_part add partition(partition p3 values less than maxvalue);

query ok, 0 rows affected (0.02 sec)

records: 0 duplicates: 0 warnings: 0

mysql> show create table sub1_part\g;

*************************** 1. row ***************************

table: sub1_part

create table: create table `sub1_part` (

`news_id` int(11) not null comment '新闻id',

`content` varchar(1000) not null default '' comment '新闻内容',

`u_id` varchar(25) not null default '' comment '来源ip',

`create_time` date not null default '0000-00-00' comment '时间'

) engine=innodb default charset=utf-8

!50100 partition by range (year(create_time))

subpartition by hash (to_days(create_time))

(partition p0 values less than (1990)

(subpartition s0 engine = innodb,

subpartition s1 engine = innodb,

subpartition s2 engine = innodb),

partition p1 values less than (2000)

(subpartition s3 engine = innodb,

subpartition s4 engine = innodb,

subpartition good engine = innodb),

partition p2 values less than (3000)

(subpartition tank0 engine = innodb,

subpartition tank1 engine = innodb,

subpartition tank3 engine = innodb),

partition p3 values less than maxvalue

(subpartition p3sp0 engine = innodb, //子分区的名子是自动生成的

subpartition p3sp1 engine = innodb,

subpartition p3sp2 engine = innodb))

1 row in set (0.00 sec)[javascript]view plain copy

//range添加新分区

mysql> alter table user add partition(partition p4 values less than maxvalue);

query ok, 0 rows affected (0.06 sec)

records: 0 duplicates: 0 warnings: 0

//list添加新分区

mysql> alter table list_part add partition(partition p4 values in (25,26,28));

query ok, 0 rows affected (0.01 sec)

records: 0 duplicates: 0 warnings: 0

//hash重新分区

mysql> alter table hash_part add partition partitions 4;

query ok, 0 rows affected (0.12 sec)

records: 0 duplicates: 0 warnings: 0

//key重新分区

mysql> alter table key_part add partition partitions 4;

query ok, 1 row affected (0.06 sec) //有数据也会被重新分配

records: 1 duplicates: 0 warnings: 0

//子分区添加新分区,虽然我没有指定子分区,但是系统会给子分区命名的

mysql> alter table sub1_part add partition(partition p3 values less than maxvalue);

query ok, 0 rows affected (0.02 sec)

records: 0 duplicates: 0 warnings: 0

mysql> show create table sub1_part\g;

*************************** 1. row ***************************

table: sub1_part

create table: create table `sub1_part` (

`news_id` int(11) not null comment '新闻id',

`content` varchar(1000) not null default '' comment '新闻内容',

`u_id` varchar(25) not null default '' comment '来源ip',

`create_time` date not null default '0000-00-00' comment '时间'

) engine=innodb default charset=utf-8

!50100 partition by range (year(create_time))

subpartition by hash (to_days(create_time))

(partition p0 values less than (1990)

(subpartition s0 engine = innodb,

subpartition s1 engine = innodb,

subpartition s2 engine = innodb),

partition p1 values less than (2000)

(subpartition s3 engine = innodb,

subpartition s4 engine = innodb,

subpartition good engine = innodb),

partition p2 values less than (3000)

(subpartition tank0 engine = innodb,

subpartition tank1 engine = innodb,

subpartition tank3 engine = innodb),

partition p3 values less than maxvalue

(subpartition p3sp0 engine = innodb, //子分区的名子是自动生成的

subpartition p3sp1 engine = innodb,

subpartition p3sp2 engine = innodb))

1 row in set (0.00 sec)

3,重新分区

查看复制打印?

//range重新分区

mysql> alter table user reorganize partition p0,p1,p2,p3,p4 into (partition p0 values less than maxvalue);

query ok, 11 rows affected (0.08 sec)

records: 11 duplicates: 0 warnings: 0

//list重新分区

mysql> alter table list_part reorganize partition p0,p1,p2,p3,p4 into (partition p0 values in (1,2,3,4,5));

query ok, 0 rows affected (0.28 sec)

records: 0 duplicates: 0 warnings: 0

//hash和key分区不能用reorganize,官方网站说的很清楚

mysql> alter table key_part reorganize partition coalesce partition 9;

error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'partition 9' at line 1[php]view plain copy

//range重新分区

mysql> alter table user reorganize partition p0,p1,p2,p3,p4 into (partition p0 values less than maxvalue);

query ok, 11 rows affected (0.08 sec)

records: 11 duplicates: 0 warnings: 0

//list重新分区

mysql> alter table list_part reorganize partition p0,p1,p2,p3,p4 into (partition p0 values in (1,2,3,4,5));

query ok, 0 rows affected (0.28 sec)

records: 0 duplicates: 0 warnings: 0

//hash和key分区不能用reorganize,官方网站说的很清楚

mysql> alter table key_part reorganize partition coalesce partition 9;

error 1064 (42000): you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'partition 9' at line 1

四,分区优点

1,分区可以分在多个磁盘,存储更大一点

2,根据查找条件,也就是where后面的条件,查找只查找相应的分区不用全部查找了

3,进行大数据搜索时可以进行并行处理。

4,跨多个磁盘来分散数据查询,来获得更大的查询吞吐量

软件
前端设计
程序设计
Java相关