定义
每一个分区仅包含在指定范围内的数据列。这样的分区方式就是范围分区。在mysql的范围分区表定义中,分区范围需要连续并且不会有覆盖。定义范围分区表时,使用values less than操作符。在partition by range语法中,建立分区表指定分区时,每一个分区都是按顺序定义。使用时类似c语言和java中的if...elseif...表达式。
应用
本文的几个举例,都假设用户为一家具有20个门店的音响公司创建员工记录表。这20家门店的编号store_id是1到20.
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null,
store_id int not null
);使用store_id列进行分区
使用store_id列,将员工表建立四个分区
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null,
store_id int not null
) partition by range (store_id) (
partition p0 values less than (6),
partition p1 values less than (11),
partition p2 values less than (16),
partition p3 values less than (21)
);在这个新建的分区表中,门店1-5的员工数据,保存在第一个分区p0中;门店6-10的员工数据,保存在第二个分区p1中....
按照上面的分区表定义,mysql很容易将数据(72, 'mitchell', 'wilson', '1998-06-25', default, 7, 13) 插入第三个分区p2当中。但这家公司开了一家新的门店,第21个门店时,会出现什么样的问题。在这个分区表的定义中,并未指定门店编码超过20的处理方式,因此插入第21个门店的员工数据时,mysql会报错。mysql给出来一个数值maxvalue来解决这个问题。maxvalue是mysql中最大的一个数值,所有数字都会比maxvalue小。则构建该分区表的语句变成下面的形式
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null,
store_id int not null
) partition by range (store_id) (
partition p0 values less than (6),
partition p1 values less than (11),
partition p2 values less than (16),
partition p3 values less than maxvalue
);现在,所有门店编号大于等于16的数据,都会插入到第四个分区p3当中。而当后续门店增加时,数据库管理员和开发人员,可以为表建立新的分区。如为编号21-25门店添加新的分区 ,为26-30的门店添加另一个分区 。
使用员工编号分区
使用员工编号分区的方式,是一种更加时髦的分区方法。在上面的例子中,数据库管理员和开发人员可以使用字段job_code进行分区。如,两位编码用于保存店长的信息。三位编码分配给办公室和后勤人员。而为销售经理分配四位编码。则可以按照下面的方法来创建分区表
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null,
store_id int not null
) partition by range (job_code) (
partition p0 values less than (100),
partition p1 values less than (1000),
partition p2 values less than (10000)
);使用日期时间字段建立分区
除了使用门店编号,和员工代码分区外,还可以使用时间列来建立分区表。假设现在需要使用员工的离职年份建立分区。即使用year(seperated)表达式返回的值来确定分区范围。则使用下面的语句建立分区表
create table employees (
id int not null,
fname varchar(30),
lname varchar(30),
hired date not null default '1970-01-01',
separated date not null default '9999-12-31',
job_code int not null,
store_id int not null
) partition by range (year(separated)) (
partition p0 values less than (1991),
partition p1 values less than (1996),
partition p2 values less than (2001),
partition p4 values less than maxvalue
);在上面的分区表中,1991年前离职的员工在分区p0中,而在1991-1995年离职的员工,在分区p1中...
当然,也可以基于timestamp字段,按照时间范围来建立分区表,使用unix_timestamp()方法,建立分区表如下。
create table quarterly_report_status (
report_id int not null,
report_status varchar(20) not null,
report_updated timestamp not null default current_timestamp on update current_timestamp
)
partition by range ( unix_timestamp(report_updated) ) (
partition p0 values less than ( unix_timestamp('2008-01-01 00:00:00') ),
partition p1 values less than ( unix_timestamp('2008-04-01 00:00:00') ),
partition p2 values less than ( unix_timestamp('2008-07-01 00:00:00') ),
partition p3 values less than ( unix_timestamp('2008-10-01 00:00:00') ),
partition p4 values less than ( unix_timestamp('2009-01-01 00:00:00') ),
partition p5 values less than ( unix_timestamp('2009-04-01 00:00:00') ),
partition p6 values less than ( unix_timestamp('2009-07-01 00:00:00') ),
partition p7 values less than ( unix_timestamp('2009-10-01 00:00:00') ),
partition p8 values less than ( unix_timestamp('2010-01-01 00:00:00') ),
partition p9 values less than (maxvalue)
);使用时间段分区使用下面的场景
- 当开发人员或数据库管理员想要删除旧数据的时候,如前面的employee表,用户可以简单的使用alter table employees drop partition p0,来删除1991年前离职员工的所有数据。当有很多数据需要删除时,使用这种删除方法比使用delete方法删除数据效率会高很多。
- 用户想要使用时间或其他序列的字段来操作数据表
- 用户经常使用表分区字段来进行查询。如当执行下面这条语句.mysql能够快速的使用where查询条件找到第三个分区p2, 而不需要扫描其他分区的数据。
explain select count(*) from employees where seperated between '2000-01-01' and '2000-12-31' group by store_id
范围分区列
与范围分区相似的用法就是分区列。使用range columns方法构建分区表,允许用户使用多个列来建立分区表。这些列可以共同用于查询时对扫描数据的修剪和过滤。
使用范围分区列来创建分区表。
create table members (
firstname varchar(25) not null,
lastname varchar(25) not null,
username varchar(16) not null,
email varchar(35),
joined date not null
)
partition by range columns(joined) (
partition p0 values less than ('1960-01-01'),
partition p1 values less than ('1970-01-01'),
partition p2 values less than ('1980-01-01'),
partition p3 values less than ('1990-01-01'),
partition p4 values less than maxvalue
);到此这篇关于mysql按照范围区间创建分区表的文章就介绍到这了,更多相关mysql创建分区表内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论