mysql> SHOW PLUGINS ;
+----------------------------+----------+--------------------+---------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+---------+---------+
| partition | ACTIVE | STORAGE ENGINE | NULL | GPL |
+----------------------------+----------+--------------------+---------+---------+
或使用
mysql> SELECT PLUGIN_NAME as Name, PLUGIN_VERSION as Version, PLUGIN_STATUS as Status
-> FROM INFORMATION_SCHEMA.PLUGINS
-> WHERE PLUGIN_TYPE='STORAGE ENGINE';
mysql> ALTER TABLE t1 PARTITION BY RANGE (YEAR(atime))
-> (
-> PARTITION p0 VALUES LESS THAN (2016),
-> PARTITION p1 VALUES LESS THAN (2017),
-> PARTITION p2 VALUES LESS THAN (2018),
-> PARTITION p3 VALUES LESS THAN MAXVALUE );
Query OK, 4194304 rows affected (1 min 8.32 sec)
mysql> EXPLAIN PARTITIONS SELECT * FROM `t1`; #查看分区情况
+----+-------------+-------+-------------+------+---------------+------+---------+------+---------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+-------------+------+---------------+------+---------+------+---------+----------+-------+
| 1 | SIMPLE | t1 | p0,p1,p2,p3 | ALL | NULL | NULL | NULL | NULL | 4180974 | 100.00 | NULL |
+----+-------------+-------+-------------+------+---------------+------+---------+------+---------+----------+-------+
1 row in set, 2 warnings (0.00 sec)
同样用上面的查询测试结果
mysql> SELECT * FROM `t1` WHERE `uid`=89757 AND `atime`< CURRENT_TIMESTAMP();
1048576 rows in set (4.46 sec) #与上面没有分区查询执行的时间相比少了接近1s
mysql> EXPLAIN PARTITIONS SELECT * FROM `t1` WHERE `uid`=89757 AND `atime`< CURRENT_TIMESTAMP(); #查看查询使用的分区情况
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
| 1 | SIMPLE | t1 | p0,p1,p2 | ALL | NULL | NULL | NULL | NULL | 3135804 | 3.33 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+---------+----------+-------------+
1 row in set, 2 warnings (0.00 sec)
同时也要注意,进行表分区以后,mysql存放的数据文件夹中该表的存放文件也被拆分为多个
-rw-r----- 1 mysql mysql 8.7K 2月 14 14:49 t1.frm
-rw-r----- 1 mysql mysql 36M 2月 14 14:50 t1#P#p0.ibd
-rw-r----- 1 mysql mysql 64M 2月 14 14:50 t1#P#p1.ibd
-rw-r----- 1 mysql mysql 92M 2月 14 14:50 t1#P#p2.ibd
-rw-r----- 1 mysql mysql 64M 2月 14 14:50 t1#P#p3.ibd
mysql> CREATE TABLE `t2` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
`pid` int(10) unsigned NOT NULL COMMENT '产品ID',
`price` decimal(15,2) NOT NULL COMMENT '单价',
`num` int(11) NOT NULL COMMENT '购买数量',
`uid` int(10) unsigned NOT NULL COMMENT '客户ID',
`atime` datetime NOT NULL COMMENT '下单时间',
`utime` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '修改时间',
`isdel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '软删除标识',
PRIMARY KEY (`id`,`atime`)
)
PARTITION BY RANGE COLUMNS(atime) (
PARTITION p0 VALUES LESS THAN ('2016-01-01'),
PARTITION p1 VALUES LESS THAN ('2016-02-01'),
PARTITION p2 VALUES LESS THAN ('2016-03-01'),
PARTITION p3 VALUES LESS THAN ('2016-04-01'),
PARTITION p4 VALUES LESS THAN ('2016-05-01'),
PARTITION p5 VALUES LESS THAN ('2016-06-01'),
PARTITION p6 VALUES LESS THAN ('2016-07-01'),
PARTITION p7 VALUES LESS THAN ('2016-08-01'),
PARTITION p8 VALUES LESS THAN ('2016-09-01'),
PARTITION p9 VALUES LESS THAN ('2016-10-01'),
PARTITION p10 VALUES LESS THAN ('2016-11-01'),
PARTITION p11 VALUES LESS THAN ('2016-12-01'),
PARTITION p12 VALUES LESS THAN ('2017-01-01'),
PARTITION p13 VALUES LESS THAN ('2017-02-01'),
PARTITION p14 VALUES LESS THAN ('2017-03-01'),
PARTITION p15 VALUES LESS THAN ('2017-04-01'),
PARTITION p16 VALUES LESS THAN ('2017-05-01'),
PARTITION p17 VALUES LESS THAN ('2017-06-01'),
PARTITION p18 VALUES LESS THAN ('2017-07-01'),
PARTITION p19 VALUES LESS THAN ('2017-08-01'),
PARTITION p20 VALUES LESS THAN ('2017-09-01'),
PARTITION p21 VALUES LESS THAN ('2017-10-01'),
PARTITION p22 VALUES LESS THAN ('2017-11-01'),
PARTITION p23 VALUES LESS THAN ('2017-12-01'),
PARTITION p24 VALUES LESS THAN ('2018-01-01'),
PARTITION p25 VALUES LESS THAN ('2018-02-01'),
PARTITION p26 VALUES LESS THAN ('2018-03-01'),
PARTITION p27 VALUES LESS THAN ('2018-04-01'),
PARTITION p28 VALUES LESS THAN ('2018-05-01'),
PARTITION p29 VALUES LESS THAN ('2018-06-01'),
PARTITION p30 VALUES LESS THAN ('2018-07-01'),
PARTITION p31 VALUES LESS THAN ('2018-08-01'),
PARTITION p32 VALUES LESS THAN ('2018-09-01'),
PARTITION p33 VALUES LESS THAN ('2018-10-01'),
PARTITION p34 VALUES LESS THAN ('2018-11-01'),
PARTITION p35 VALUES LESS THAN ('2018-12-01'),
PARTITION p36 VALUES LESS THAN MAXVALUE
);
mysql> CREATE TABLE `tb01` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
`pid` int(10) unsigned NOT NULL COMMENT '产品ID',
`price` decimal(15,2) NOT NULL COMMENT '单价',
`num` int(11) NOT NULL COMMENT '购买数量',
`uid` int(10) unsigned NOT NULL COMMENT '客户ID',
`atime` datetime NOT NULL COMMENT '下单时间',
`utime` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '修改时间',
`isdel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '软删除标识',
PRIMARY KEY (`id`,`num`)
);
*****************************插入测试数据******************************************************
INSERT INTO `tb01`(`pid`,`price`,`num`,`uid`,`atime`) SELECT `pid`,`price`,`num`,`uid`,`atime` FROM `tb`;
Query OK, 3145728 rows affected (46.26 sec)
Records: 3145728 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE tb01 PARTITION BY LIST(num)
(
PARTITION pl01 VALUES IN (1,3),
PARTITION pl02 VALUES IN (2,4),
PARTITION pl03 VALUES IN (5,7),
PARTITION pl04 VALUES IN (6,8),
PARTITION pl05 VALUES IN (9,10)
);
Query OK, 3145728 rows affected (48.86 sec)
Records: 3145728 Duplicates: 0 Warnings: 0
存放mysql数据文件中生成,以下文件
-rw-r----- 1 mysql mysql 8.7K 2月 15 11:35 tb01.frm
-rw-r----- 1 mysql mysql 56M 2月 15 11:36 tb01#P#pl01.ibd
-rw-r----- 1 mysql mysql 32M 2月 15 11:36 tb01#P#pl02.ibd
-rw-r----- 1 mysql mysql 36M 2月 15 11:36 tb01#P#pl03.ibd
-rw-r----- 1 mysql mysql 36M 2月 15 11:36 tb01#P#pl04.ibd
-rw-r----- 1 mysql mysql 52M 2月 15 11:36 tb01#P#pl05.ibd
mysql> ALTER TABLE t1 ADD PARTITION (PARTITION P4 VALUES LESS THAN (2018) ) ;
ERROR 1481 (HY000): MAXVALUE can only be used in last partition definition
示例:把tb01上面删除的pl05分区添加
mysql> ALTER TABLE tb01 ADD PARTITION(PARTITION pl05 VALUES IN (9,10));
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE SmsSend EXCHANGE PARTITION py01 WITH TABLE smssendbak;
Query OK, 0 rows affected (0.13 sec)
mysql> select count(1) from SmsSend partition(py01); #对比上面原SmsSend表分区的记录
+----------+
| count(1) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT COUNT(1) FROM smssendbak; #查看新smssendbak备份表转移记录
+----------+
| COUNT(1) |
+----------+
| 10 |
+----------+
1 row in set (0.00 sec)
*****************测试使用的表***********************************************************************
创建一个基础测试表:
CREATE TABLE `tb` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '表主键',
`pid` int(10) unsigned NOT NULL COMMENT '产品ID',
`price` decimal(15,2) NOT NULL COMMENT '单价',
`num` int(11) NOT NULL COMMENT '购买数量',
`uid` int(10) unsigned NOT NULL COMMENT '客户ID',
`atime` datetime NOT NULL COMMENT '下单时间',
`utime` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '修改时间',
`isdel` tinyint(4) NOT NULL DEFAULT '0' COMMENT '软删除标识',
) ;
插入数据:
INSERT INTO tb(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,CURRENT_TIMESTAMP());
INSERT INTO tb(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2016-05-01 00:00:00');
INSERT INTO tb(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2017-05-01 00:00:00');
INSERT INTO tb(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89757,'2018-05-01 00:00:00');
INSERT INTO tb(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2015-05-01 00:00:00');
INSERT INTO tb(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2016-05-01 00:00:00');
INSERT INTO tb(`pid`,`price`,`num`,`uid`,`atime`) VALUES(1,12.23,1,89756,'2017-05-01 00:00:00');
************************************插入大量的数据(建议百万以上)*************************************
INSERT INTO `tb`(`pid`,`price`,`num`,`uid`,`atime`) SELECT `pid`,`price`,`num`,`uid`,`atime` FROM `tb`;
****注意,如果要删除自增长的主键id(修改过程中,建议该库改为只读),如下操作:
Alter table tb change id id int(10); #先删除自增长
Alter table tb drop primary key;#删除主建
Alter table tb change id id int not null auto_increment; #如果想重新设置为自增字段
Alter table tb auto_increment=1; #自增起始值