mysql 按时间分区的方法 PARTITION
在操作之前,你仍然有必要执行如下命令,查看你的MySQL版本是否支持partition功能:
Sql代码
show variables like '%partition%'
如果系统输出如下结果,那么恭喜你,MySQL支持分区!
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('2011-01-01 00:00:00') ),
PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2011-02-01 00:00:00') )
);
----------------------------
SQL测试,看看是否有效:
Sql代码
explain partitions select * FROM quarterly_report_status q where q.report_updated<'2011-02-01 00:00:00';
输出:
| id | select_type | table | partitions | type | possible_keys | key | key_len| ref | rows | Extra |
| 1 | SIMPLE | q | p0,p1 | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
1 row in set (0.00 sec)