前言
在系列文 MySQL 系列文 - Partition Table(2) - 在既有的 TABLE 上建立 partition 中有举了一个範例是用日来做分区。 当然相信有很多人都有试过要用日来做切割,但是可能会遇到一些状况。本篇文章就举几个我遇到的问题分享给大家,或许在建置过程中有遇到一样的问题可以参考。
日期 type 使用 datetime
测试资料
create table pt1(id int ,ctime datetime,primary key(ctime))partition by range(TO_DAYS(ctime))(partition p0 values less than (TO_DAYS('2020-01-01')), # 资料在 < 2020-01-01partition p1 values less than (TO_DAYS('2020-02-01')) # 资料在 >= 2020-01-01 and < 2020-02-01 );insert into pt1 values(1,'2019-10-05');insert into pt1 values(1,'2020-01-02');insert into pt1 values(1,'2020-01-05');
查一下分区状况
看起来没问题,p0: 有一笔资料 p1: 有两笔资料select table_schema, table_name, partition_name,partition_method, partition_description, table_rowsfrom information_schema.partitionswhere table_schema = 'test2' and table_name = 'pt1'
图1
这时候来搜寻看看是不是真的如我们所愿
下面的时间範围应该只要 scan p1 这个 partition 就好。但是看下图2,却 p1,p2 两个 partition 都去找了。
explainselect * from pt1where ctime<'2020-02-01' and ctime > '2020-01-01'
图2
日期 type 使用 timestamp (确保可以用正确的 partition)
测试资料
create table pt2(id int ,ctime TIMESTAMP,primary key(ctime)) ENGINE=InnoDBpartition by range(unix_timestamp(ctime))(partition p0 values less than (unix_timestamp('2020-01-01 00:00:00')), # 资料在 < 2020-01-01partition p1 values less than (unix_timestamp('2020-02-01 00:00:00')) # 资料在 >= 2020-01-01 and < 2020-02-01);insert into pt2 values(1,'2019-10-05');insert into pt2 values(1,'2020-01-02');insert into pt2 values(1,'2020-01-15');
查一下分区状况
看起来没问题,p0: 有一笔资料 p1: 有两笔资料select table_schema, table_name, partition_name,partition_method, partition_description, table_rowsfrom information_schema.partitionswhere table_schema = 'test2' and table_name = 'pt2'
图3
这时候来搜寻看看是不是真的如我们所愿
下面的时间範围应该只要 scan p1 这个 partition 就好。看下图4, 的确只找了 p1 这个 partition 。
explainselect * from pt2where ctime<'2020-02-01' and ctime > '2020-01-01'
图4
小结
以上两种方式虽然都可以写到正确的 partition ,但是用 datetime 的时候在 select 时会用不到指定的 partition。目前是没有找到原因,只知道改用 timestamp 是可以成功的,知道的也麻烦留言给我啦!
当然 datetime 和 timestamp 这两个时间类型各有各的优缺点,大家可以自己 google 了解一下,我这里只是提供我的一个解法给大家参考,如果有更好的解法也请各位分享给我。
资料库知识相当广泛,文中若有不正确的地方,也烦请各位大神不吝指教,谢谢