我有一个按月分区的大表
CREATE TABLE `log` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`logdate` date NOT NULL,
...
,
PRIMARY KEY (`id`,`logdate`)
) PARTITION BY RANGE (month(`logdate`))
(PARTITION part0 VALUES LESS THAN (2),
PARTITION part1 VALUES LESS THAN (3),
PARTITION part2 VALUES LESS THAN (4),
PARTITION part3 VALUES LESS THAN (5),
PARTITION part4 VALUES LESS THAN (6),
PARTITION part5 VALUES LESS THAN (7),
PARTITION part6 VALUES LESS THAN (8),
PARTITION part7 VALUES LESS THAN (9),
PARTITION part8 VALUES LESS THAN (10),
PARTITION part9 VALUES LESS THAN (11),
PARTITION part10 VALUES LESS THAN (12),
PARTITION part11 VALUES LESS THAN MAXVALUE);
我已经插入了3个月的数据,可以看到这些行已经被放到了各自的分区中。
当我查询指定分区时,返回了正确的数据,并且explain
显示它正在从正确的分区中进行选择
select logdate, sum(total) from log partition(part10)
where logdate between '2020-11-01' and '2020-11-30' group by 1 order by 1 desc;
但是,如果未指定分区,则不会对以下分区进行分区修剪。
select logdate, sum(total) from log
where logdate between '2020-11-01' and '2020-11-30' group by 1 order by 1 desc;
select logdate, sum(total) from log
where month(logdate) = 11 group by 1 order by 1 desc;
根据mysql-8文档https://dev.mysql.com/doc/refman/8.0/en/partitioning-range.html
基于时间间隔的分区方案如果您希望在MySQL 8.0中实现基于时间范围或时间间隔的分区方案,您有两种选择:
1.按RANGE对表进行分区,对于分区表达式,使用一个函数对DATE、TIME或DATETIME列进行操作,并返回一个整数值**-如我的代码所示**
1.使用DATE或DATETIME列作为分区列,按RANGE COLUMNS对表进行分区。
我错过了什么?
1条答案
按热度按时间weylhg0b1#
根据24.6.3 Partitioning Limitations Relating to Functions:
在MySQL 8.0中,TO_DAYS()、TO_SECONDS()、YEAR()和UNIX_TIMESTAMP()函数支持分区修剪。有关详细信息,请参见第24.4节"Partition Pruning"。
因为您是按MONTH()进行分区的,所以分区修剪不起作用。