Creating and using MySql table partition

First, create a partition table

MySql is the default partition table support, you can check whether the statement by the turn table partitioning: show plugins;

 

Create table partitions only need to add partitions statement can create a table in the back of the statement, for example:

create table user(id int(11) not null,name varchar(32) not null)  --正常的创建语句
partition by range(id)   --根据表字段id来创建分区
(
  partition p0 values less than(10),       --第一个分区p0,范围~-9
  partition p1 values less than(20),       --第二个分区p1,范围10-19
  partition p2 values less than(30),       --第三个分区p2,范围20-29
  partition p3 values less than maxvalue   --第四个分区p2,范围30-~
) --需要注意的是分区字段“id”的取值范围等于分区取值范围

 

The data storage files will be split into multiple partitions:. * Ibd, .frm file is a table-formatted document:

After several new data query can see the data that has been dispersed in different partitions:

Second, the type of partition table 

1, RANGE table partition: range partitioning table, according to a certain range of values to determine the data contained in each partition, as is the use range table partitions;

partition by range(id)          partition p0 values less than()

Field partitioning function used must be an integer type (bit, int, tinyint, bigint, etc.), the definition of the partition must be continuous and can not overlap, using values less than () to define the zone range from small to large definition.

When a partition to partition field assignment can not exceed the range of field values ​​less than () value range. Use values ​​less than maxvalue to the value of future uncertainty into the table partition.

Press time types (datetime) do partition table can be used in the RANGE () function to do the conversion, for example: partition by range (year (create_time )), timestamp may be used unix_timestamp ( '2019-11-20 00:00:00 ') transformation.

2, LIST table partition: list partition table, according to a determination of a value determined for each partition contains data

partition by list(id)         partition p0 values in(1,2,3)

分区字段必须是整数类型或者分区函数返回整数,取值范围通过values in()来定义。不能使用maxvalue。

 create table user4(sex int(1)) partition by list(sex) (partition p0 values in(1),partition p1 values in(2));

3、HASH表分区:哈希表分区,按照一个自定义的函数返回值来确定每个分区包含的数据

partition by hash(id)        partitions 4

根据hash算法来分配到分区中,以上设置四个分区,并根据id%4进行取模运算,根据余数插入到指定的分区中。

create table user7(id int)  partition by hash(id) partitions 3;

4、KEY表分区:key表分区,与哈希表分区类似,只是用MySql自己的HASH函数来确定每个分区包含的数据;

partition by key()        partitions 4

key()括号里面可以包含0个或多个字段(不必是整数类型,可以是普通字段),如果表中有主键或者唯一键,所引用的字段必须是主键或者主键的一部分。如果没有写字段,默认使用主键,如果表中没有主键,则使用唯一键,但唯一键必须设置为not null。

5、多字段分区(range、list):可以指定多个字段作为分区字段;以下以多字段range作为讲解:

例如:partition by range columns(id,name) 

多字段分区可以使用非整数类型来作为分区字段。使用这个特性可以用来创建单字段的非整数类型的表分区。

对比方式从左到右,第一个字段值小于第一个字段分区值则放在第一个分区,等于或大于第一个字段分区值则对比第二个字段值与第一个字段分区值的大小,以此类推。

create table user6(sex varchar(10),name varchar(10),age varchar(10)) partition by range columns(sex,name,age) (partition p0 values less than('d','f','h'),partition p1 values less than('l','n','x'));

先比较sex字段,再比较name字段,最后比较age字段。使用select ('a','b')<('b','c')来验证

三、创建表分区的约束条件

1、如果表有主键(primary key)或者唯一键(unique key),分区字段必须被包含于主键和唯一键字段的交集部分。

错误的例子: 

正确的例子:

 

 

三、表分区的使用

1、使用分区字段"id"做查询的时候只查询id所处的分区,否则查询所有分区:

四、表分区的主要优点 

          1、可以允许在一个表里存储更多的数据,突破磁盘限制和文件系统限制。

          2、对于从表里删除过期的历史数据比较容易,只需要移除对应的分区。

          3、对于某些查询或修改语句,可以自动将数据范围缩小到一个至几个分区上,优化语句执行效率。

          

 

发布了65 篇原创文章 · 获赞 28 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_26900081/article/details/103159660
Recommended