Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

mysql - Mysql8 partition by month, partition pruning not working

I have a large table that I have partitioned by month

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);

I have inserted 3 months of data and can see that the rows have been put into their respective partitions.

When I query specifying the partition, the correct data is returned and the explain shows that it is selecting from the correct partition

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;

However when not specifying the partition no partition pruning is occurring for the below.

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;

According to mysql-8 documentation https://dev.mysql.com/doc/refman/8.0/en/partitioning-range.html

Partitioning schemes based on time intervals. If you wish to implement a partitioning scheme based on ranges or intervals of time in MySQL 8.0, you have two options:

  1. Partition the table by RANGE, and for the partitioning expression, employ a function operating on a DATE, TIME, or DATETIME column and returning an integer value - as shown here in my code
  1. Partition the table by RANGE COLUMNS, using a DATE or DATETIME column as the partitioning column.

What am I missing?

question from:https://stackoverflow.com/questions/65895219/mysql8-partition-by-month-partition-pruning-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...