一个自己项目中用到的范围分区:
DROP TABLE IF EXISTS T1;
CREATE TABLE T1
(
ID INT NOT NULL,
C1 CHAR(15) NOT NULL,
DATATIME DATETIME NOT NULL,
PRIMARY KEY (ID, C1, DATATIME)
) ENGINE=INNODB DEFAULT CHARSET=utf8
PARTITION BY RANGE (TO_DAYS(DATATIME)) (
PARTITION p201701 VALUES LESS THAN (TO_DAYS('2017-02-01')),
PARTITION p201702 VALUES LESS THAN (TO_DAYS('2017-03-01')),
PARTITION p201703 VALUES LESS THAN (TO_DAYS('2017-04-01')),
PARTITION p201704 VALUES LESS THAN (TO_DAYS('2017-05-01')),
PARTITION p201705 VALUES LESS THAN (TO_DAYS('2017-06-01')),
PARTITION p201706 VALUES LESS THAN (TO_DAYS('2017-07-01')),
PARTITION p201707 VALUES LESS THAN (TO_DAYS('2017-08-01')),
PARTITION p201708 VALUES LESS THAN (TO_DAYS('2017-09-01')),
PARTITION p201709 VALUES LESS THAN (TO_DAYS('2017-10-01')),
PARTITION p201710 VALUES LESS THAN (TO_DAYS('2017-11-01')),
PARTITION p201711 VALUES LESS THAN (TO_DAYS('2017-12-01')),
PARTITION p201712 VALUES LESS THAN (TO_DAYS('2018-01-01')));
INSERT INTO T1(ID, C1, DATATIME) VALUES(1,'00001','2017-03-15 09:00:00'),(1,'00001','2017-02-15 09:00:00');
SELECT * FROM T1 PARTITION(p201702);
SELECT * FROM T1 PARTITION(p201702,p201703)
-- 添加分区语句
ALTER TABLE T1 ADD PARTITION (PARTITION p20180103 VALUES LESS THAN(TO_DAYS('2018-01-03')));
-- 如果是整形的
CREATE TABLE IF NOT EXISTS T1(
ID INT NOT NULL,
C1 CHAR(15) NOT NULL,
DATATIME BIGINT NOT NULL,
PRIMARY KEY (ID, C1, DATATIME)
) ENGINE=INNODB DEFAULT CHARSET=utf8
PARTITION BY RANGE(TSTAMP)
(
PARTITION p1 VALUES LESS THAN (UNIX_TIMESTAMP('2017-02-01')) ENGINE=INNODB,
PARTITION p2 VALUES LESS THAN (UNIX_TIMESTAMP('2017-03-01')) ENGINE=INNODB,
PARTITION p3 VALUES LESS THAN (UNIX_TIMESTAMP('2017-04-01')) ENGINE=INNODB,
PARTITION p4 VALUES LESS THAN (UNIX_TIMESTAMP('2017-05-01')) ENGINE=INNODB,
PARTITION p5 VALUES LESS THAN (UNIX_TIMESTAMP('2017-06-01')) ENGINE=INNODB,
PARTITION p6 VALUES LESS THAN (UNIX_TIMESTAMP('2017-07-01')) ENGINE=INNODB,
PARTITION p7 VALUES LESS THAN (UNIX_TIMESTAMP('2017-08-01')) ENGINE=INNODB,
PARTITION p8 VALUES LESS THAN (UNIX_TIMESTAMP('2017-09-01')) ENGINE=INNODB,
PARTITION p9 VALUES LESS THAN (UNIX_TIMESTAMP('2017-10-01')) ENGINE=INNODB,
PARTITION p10 VALUES LESS THAN (UNIX_TIMESTAMP('2017-11-01')) ENGINE=INNODB,
PARTITION p11 VALUES LESS THAN (UNIX_TIMESTAMP('2017-12-01')) ENGINE=INNODB,
PARTITION p12 VALUES LESS THAN (UNIX_TIMESTAMP('2018-01-01')) ENGINE=INNODB
);
新版本也支持另一形式
CREATE TABLE T1 (
id INT,
dt DATE NOT NULL
)
PARTITION BY RANGE COLUMNS(dt) (
PARTITION p0 VALUES LESS THAN ('2014-01-01'),
PARTITION p1 VALUES LESS THAN ('2015-01-01'),
PARTITION p2 VALUES LESS THAN ('2016-01-01'),
PARTITION p3 VALUES LESS THAN ('2017-01-01')
);
ALTER TABLE T1 ADD PARTITION (PARTITION p4 VALUES LESS THAN('2018-01-01'))
子分区表操作:
CREATE TABLE T1 (
pid INT,
joined DATETIME NOT NULL
)
PARTITION BY RANGE COLUMNS(joined)
SUBPARTITION BY HASH(pid) (
PARTITION p20180802 VALUES LESS THAN ('2018-01-02')
(
SUBPARTITION s0,
SUBPARTITION s1
)
);
ALTER TABLE T1
ADD PARTITION
(
PARTITION p20180103 VALUES LESS THAN ('2018-01-03')
(SUBPARTITION s3,
SUBPARTITION s4
)
);
分区表自动创建过程:
DELIMITER $$
USE `t`$$
DROP PROCEDURE IF EXISTS `P_BAS_ADDPARTITION`$$
CREATE PROCEDURE P_BAS_ADDPARTITION()
BEGIN
DECLARE v_sysdate DATE;
DECLARE v_mindate DATE;
DECLARE v_maxdate DATE;
DECLARE v_pt VARCHAR(20);
DECLARE v_maxval VARCHAR(20);
DECLARE i INT;
/*增加新分区*/
SELECT SUBSTRING(MAX(partition_description),2,10)+INTERVAL 1 DAY AS val
INTO v_maxdate
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'T_ETL_DATA' AND table_schema='t';
SET v_sysdate = SYSDATE();
SELECT v_maxdate;
-- WHILE v_maxdate <= (v_sysdate + INTERVAL 2 DAY) DO
WHILE v_maxdate <= ('2017-01-10') DO
BEGIN
SET v_pt = DATE_FORMAT(v_maxdate,'%Y%m%d');
SET v_maxval = DATE_FORMAT(v_maxdate, '%Y-%m-%d');
SET @sql = CONCAT('alter table T_ETL_DATA add partition (partition p', v_pt, ' values less than(''', v_maxval, '''))');
-- SELECT @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET v_maxdate = v_maxdate + INTERVAL 1 DAY;
END;
END WHILE;
/*删除旧分区
SELECT FROM_UNIXTIME(min(partition_description)) AS val
INTO v_mindate
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 'XXX';
WHILE v_mindate <= (v_sysdate - INTERVAL 3 DAY) DO
SET v_pt = date_format(v_mindate,'%Y%m%d');
SET @sql = concat('alter table files drop partition p', v_pt);
-- SELECT @sql;
-- PREPARE stmt FROM @sql;
-- EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET v_mindate = v_mindate + INTERVAL 2 DAY;
END WHILE;*/
END$$
DELIMITER ;
以下转自:http://mysql.taobao.org/monthly/2018/09/
随着MySQL越来越流行,Mysql里面的保存的数据也越来越大。在日常的工作中,我们经常遇到一张表里面保存了上亿甚至过十亿的记录。这些表里面保存了大量的历史记录。 对于这些历史数据的清理是一个非常头疼事情,由于所有的数据都一个普通的表里。所以只能是启用一个或多个带where条件的delete语句去删除(一般where条件是时间)。 这对数据库的造成了很大压力。即使我们把这些删除了,但底层的数据文件并没有变小。面对这类问题,最有效的方法就是在使用分区表。最常见的分区方法就是按照时间进行分区。 分区一个最大的优点就是可以非常高效的进行历史数据的清理。
目前MySQL支持范围分区(RANGE),列表分区(LIST),哈希分区(HASH)以及KEY分区四种。下面我们逐一介绍每种分区:
基于属于一个给定连续区间的列值,把多行分配给分区。最常见的是基于时间字段. 基于分区的列最好是整型,如果日期型的可以使用函数转换为整型。本例中使用to_days函数
CREATE TABLE my_range_datetime(
id INT,
hiredate DATETIME
)
PARTITION BY RANGE (TO_DAYS(hiredate) ) (
PARTITION p1 VALUES LESS THAN ( TO_DAYS('20171202') ),
PARTITION p2 VALUES LESS THAN ( TO_DAYS('20171203') ),
PARTITION p3 VALUES LESS THAN ( TO_DAYS('20171204') ),
PARTITION p4 VALUES LESS THAN ( TO_DAYS('20171205') ),
PARTITION p5 VALUES LESS THAN ( TO_DAYS('20171206') ),
PARTITION p6 VALUES LESS THAN ( TO_DAYS('20171207') ),
PARTITION p7 VALUES LESS THAN ( TO_DAYS('20171208') ),
PARTITION p8 VALUES LESS THAN ( TO_DAYS('20171209') ),
PARTITION p9 VALUES LESS THAN ( TO_DAYS('20171210') ),
PARTITION p10 VALUES LESS THAN ( TO_DAYS('20171211') ),
PARTITION p11 VALUES LESS THAN (MAXVALUE)
);
p11是一个默认分区,所有大于20171211的记录都会在这个分区。MAXVALUE是一个无穷大的值。p11是一个可选分区。如果在定义表的没有指定的这个分区,当我们插入大于20171211的数据的时候,会收到一个错误。
我们在执行查询的时候,必须带上分区字段。这样可以使用分区剪裁功能
mysql> insert into my_range_datetime select * from test;
Query OK, 1000000 rows affected (8.15 sec)
Records: 1000000 Duplicates: 0 Warnings: 0
mysql> explain partitions select * from my_range_datetime where hiredate >= '20171207124503' and hiredate<='20171210111230';
+----+-------------+-------------------+--------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+--------------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | my_range_datetime | p7,p8,p9,p10 | ALL | NULL | NULL | NULL | NULL | 400061 | Using where |
+----+-------------+-------------------+--------------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.03 sec)
注意执行计划中的partitions的内容,只查询了p7,p8,p9,p10三个分区,由此来看,使用to_days函数确实可以实现分区裁剪。
上面是基于datetime的,如果是timestamp类型,我们遇到上面问题呢?
事实上,MySQL提供了一种基于UNIX_TIMESTAMP函数的RANGE分区方案,而且,只能使用UNIX_TIMESTAMP函数,如果使用其它函数,譬如to_days,会报如下错误:“ERROR 1486 (HY000): Constant, random or timezone-dependent expressions in (sub)partitioning function are not allowed”。
而且官方文档中也提到“Any other expressions involving TIMESTAMP values are not permitted. (See Bug #42849.)”。
下面来测试一下基于UNIX_TIMESTAMP函数的RANGE分区方案,看其能否实现分区裁剪。
针对TIMESTAMP的分区方案
创表语句如下:
CREATE TABLE my_range_timestamp (
id INT,
hiredate TIMESTAMP
)
PARTITION BY RANGE ( UNIX_TIMESTAMP(hiredate) ) (
PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-02 00:00:00') ),
PARTITION p2 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-03 00:00:00') ),
PARTITION p3 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-04 00:00:00') ),
PARTITION p4 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-05 00:00:00') ),
PARTITION p5 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-06 00:00:00') ),
PARTITION p6 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-07 00:00:00') ),
PARTITION p7 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-08 00:00:00') ),
PARTITION p8 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-09 00:00:00') ),
PARTITION p9 VALUES LESS THAN ( UNIX_TIMESTAMP('2017-12-10 00:00:00') ),
PARTITION p10 VALUES LESS THAN (UNIX_TIMESTAMP('2017-12-11 00:00:00') )
);
插入数据并查看上述查询的执行计划
mysql> insert into my_range_timestamp select * from test;
Query OK, 1000000 rows affected (13.25 sec)
Records: 1000000 Duplicates: 0 Warnings: 0
mysql> explain partitions select * from my_range_timestamp where hiredate >= '20171207124503' and hiredate<='20171210111230';
+----+-------------+-------------------+--------------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+--------------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | my_range_timestamp | p7,p8,p9,p10 | ALL | NULL | NULL | NULL | NULL | 400448 | Using where |
+----+-------------+-------------------+--------------+------+---------------+------+---------+------+--------+-------------+
1 row in set (0.00 sec)
同样也能实现分区裁剪。
在5.7版本之前,对于DATA和DATETIME类型的列,如果要实现分区裁剪,只能使用YEAR() 和TO_DAYS()函数,在5.7版本中,又新增了TO_SECONDS()函数。
LIST分区
LIST分区和RANGE分区类似,区别在于LIST是枚举值列表的集合,RANGE是连续的区间值的集合。二者在语法方面非常的相似。同样建议LIST分区列是非null列,否则插入null值如果枚举列表里面不存在null值会插入失败,这点和其它的分区不一样,RANGE分区会将其作为最小分区值存储,HASH\KEY分为会将其转换成0存储,主要LIST分区只支持整形,非整形字段需要通过函数转换成整形.
create table t_list(
a int(11),
b int(11)
)(partition by list (b)
partition p0 values in (1,3,5,7,9),
partition p1 values in (2,4,6,8,0)
);
我们在实际工作中经常遇到像会员表的这种表。并没有明显可以分区的特征字段。但表数据有非常庞大。为了把这类的数据进行分区打散mysql 提供了hash分区。基于给定的分区个数,将数据分配到不同的分区,HASH分区只能针对整数进行HASH,对于非整形的字段只能通过表达式将其转换成整数。表达式可以是mysql中任意有效的函数或者表达式,对于非整形的HASH往表插入数据的过程中会多一步表达式的计算操作,所以不建议使用复杂的表达式这样会影响性能。
Hash分区表的基本语句如下:
CREATE TABLE my_member (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
created DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY HASH(id)
PARTITIONS 4;
注意:
CREATE TABLE t1 (col1 INT, col2 CHAR(5), col3 DATE) PARTITION BY HASH( YEAR(col3) ) PARTITIONS 4; 如果你要插入一个col3为“2017-09-15”的记录,则分区的选择是根据以下值决定的:
MOD(YEAR(‘2017-09-01’),4) = MOD(2017,4) = 1
LINEAR HASH分区
LINEAR HASH分区是HASH分区的一种特殊类型,与HASH分区是基于MOD函数不同的是,它基于的是另外一种算法。
格式如下:
CREATE TABLE my_members (
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,
store_id INT
)
PARTITION BY LINEAR HASH( id )
PARTITIONS 4;
说明: 它的优点是在数据量大的场景,譬如TB级,增加、删除、合并和拆分分区会更快,缺点是,相对于HASH分区,它数据分布不均匀的概率更大。
KEY分区其实跟HASH分区差不多,不同点如下:
格式如下:
CREATE TABLE k1 (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20)
)
PARTITION BY KEY()
PARTITIONS 2;
在没有主键或者唯一键的情况下,格式如下:
CREATE TABLE tm1 (
s1 CHAR(32)
)
PARTITION BY KEY(s1)
PARTITIONS 10;