Bug #116135 partition prune incorrect
Submitted: 18 Sep 2:38 Modified: 19 Sep 4:52
Reporter: haizhen xue Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version: OS:Any
Assigned to: CPU Architecture:Any

[18 Sep 2:38] haizhen xue
Description:
explain  partitions's result is 'p20240814,p20240901',expect 'p20240901':

mysql> explain select feeid, customerid, relid, businessunit, resourcetag, resourcename, resourceid, substring(convert_tz(createtime,'+00:00','+08:00'),1,19) as createtime, substring(convert_tz(updatetime,'+00:00','+08:00'),1,19) as updatetime, extendparam1, extendparam2, extendparam3, extendparam4, extendparam5, extendparam6 as extendparam6new, extendparam7, extendparam8, extendparam9, pricefactorname, chargemode, promotionid, promotiontype, resourcespeccsize, resourcespecsizemeasure, spot, releasetype, date_format(substring(convert_tz(createtime,'+00:00','+08:00'),1,19),'%Y%m%d') as dtime from t_billing_feeextendinfo where createtime>= '2024-08-31 16:00:00'  and createtime< '20224-09-01 16:00:00';
+----+-------------+-------------------------+---------------------+-------+----------------+----------------+---------+------+------+----------+-------+
| id | select_type | table                   | partitions          | type  | possible_keys  | key            | key_len | ref  | rows | filtered | Extra |
+----+-------------+-------------------------+---------------------+-------+----------------+----------------+---------+------+------+----------+-------+
|  1 | SIMPLE      | t_billing_feeextendinfo | p20240814,p20240901 | range | idx_createTime | idx_createTime | 5       | NULL |    1 |   100.00 | NULL  |
+----+-------------+-------------------------+---------------------+-------+----------------+----------------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

How to repeat:
CREATE TABLE `t_billing_feeextendinfo` (
  `FeeId` varchar(64) NOT NULL COMMENT '主键',
  `CustomerId` varchar(64) NOT NULL COMMENT '客户ID',
  `RelId` varchar(64) DEFAULT NULL COMMENT '关联ID',
  `BusinessUnit` text,
  `ResourceTag` longtext,
  `ResourceName` varchar(256) DEFAULT NULL,
  `ResourceID` varchar(128) DEFAULT NULL,
  `CreateTime` datetime NOT NULL,
  `UpdateTime` datetime NOT NULL,
  `extendParam1` varchar(256) DEFAULT NULL,
  `extendParam2` varchar(256) DEFAULT NULL,
  `extendParam3` varchar(256) DEFAULT NULL,
  `extendParam4` int(11) DEFAULT NULL,
  `extendParam5` int(11) DEFAULT NULL,
  `extendParam6` varchar(256) DEFAULT NULL,
  `extendParam7` text,
  `extendParam8` varchar(256) DEFAULT NULL,
  `extendParam9` varchar(256) DEFAULT NULL,
  `PriceFactorName` varchar(256) DEFAULT NULL COMMENT '计费因子',
  `ChargeMode` varchar(64) DEFAULT NULL COMMENT '计费模式',
  `PromotionId` varchar(128) DEFAULT NULL COMMENT '促销ID,合同ID,商务ID,原扩展继续放值',
  `PromotionType` varchar(64) DEFAULT NULL COMMENT '折扣类型(子费用有),原扩展继续放值',
  `ResourceSpecSize` decimal(25,6) DEFAULT NULL COMMENT '线性大小,原扩展字段不再放值',
  `ResourceSpecSizeMeasure` int(11) DEFAULT NULL COMMENT '线性大小单位,原扩展字段不再放值',
  `Spot` int(3) DEFAULT NULL COMMENT '竞价,原扩展字段不再放值',
  `ReleaseType` int(3) DEFAULT NULL COMMENT '释放类型,原扩展字段不再放值',
  PRIMARY KEY (`FeeId`,`CreateTime`),
  KEY `idx_createTime` (`CreateTime`),
  KEY `idx_resourceId` (`ResourceID`),
  KEY `idx_relId` (`RelId`),
  KEY `idx_priceFactorName` (`PriceFactorName`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50500 PARTITION BY RANGE (TO_SECONDS(CreateTime))
(PARTITION p20240814 VALUES LESS THAN (63890870400) ENGINE = InnoDB,
 PARTITION p20240815 VALUES LESS THAN (63890956800) ENGINE = InnoDB,
 PARTITION p20240816 VALUES LESS THAN (63891043200) ENGINE = InnoDB,
 PARTITION p20240817 VALUES LESS THAN (63891129600) ENGINE = InnoDB,
 PARTITION p20240818 VALUES LESS THAN (63891216000) ENGINE = InnoDB,
 PARTITION p20240819 VALUES LESS THAN (63891302400) ENGINE = InnoDB,
 PARTITION p20240820 VALUES LESS THAN (63891388800) ENGINE = InnoDB,
 PARTITION p20240821 VALUES LESS THAN (63891475200) ENGINE = InnoDB,
 PARTITION p20240822 VALUES LESS THAN (63891561600) ENGINE = InnoDB,
 PARTITION p20240823 VALUES LESS THAN (63891648000) ENGINE = InnoDB,
 PARTITION p20240824 VALUES LESS THAN (63891734400) ENGINE = InnoDB,
 PARTITION p20240825 VALUES LESS THAN (63891820800) ENGINE = InnoDB,
 PARTITION p20240826 VALUES LESS THAN (63891907200) ENGINE = InnoDB,
 PARTITION p20240827 VALUES LESS THAN (63891993600) ENGINE = InnoDB,
 PARTITION p20240828 VALUES LESS THAN (63892080000) ENGINE = InnoDB,
 PARTITION p20240829 VALUES LESS THAN (63892166400) ENGINE = InnoDB,
 PARTITION p20240830 VALUES LESS THAN (63892252800) ENGINE = InnoDB,
 PARTITION p20240831 VALUES LESS THAN (63892339200) ENGINE = InnoDB,
 PARTITION p20240901 VALUES LESS THAN (63892425600) ENGINE = InnoDB,
 PARTITION p20240902 VALUES LESS THAN (63892512000) ENGINE = InnoDB,
 PARTITION p20240903 VALUES LESS THAN (63892598400) ENGINE = InnoDB,
 PARTITION p20240904 VALUES LESS THAN (63892684800) ENGINE = InnoDB,
 PARTITION p20240905 VALUES LESS THAN (63892771200) ENGINE = InnoDB,
 PARTITION p20240906 VALUES LESS THAN (63892857600) ENGINE = InnoDB,
 PARTITION p20240907 VALUES LESS THAN (63892944000) ENGINE = InnoDB,
 PARTITION p20240908 VALUES LESS THAN (63893030400) ENGINE = InnoDB,
 PARTITION p20240909 VALUES LESS THAN (63893116800) ENGINE = InnoDB,
 PARTITION p20240910 VALUES LESS THAN (63893203200) ENGINE = InnoDB,
 PARTITION p20240911 VALUES LESS THAN (63893289600) ENGINE = InnoDB,
 PARTITION p20240912 VALUES LESS THAN (63893376000) ENGINE = InnoDB,
 PARTITION p20240913 VALUES LESS THAN (63893462400) ENGINE = InnoDB,
 PARTITION p20240914 VALUES LESS THAN (63893548800) ENGINE = InnoDB,
 PARTITION p20240915 VALUES LESS THAN (63893635200) ENGINE = InnoDB,
 PARTITION p20240916 VALUES LESS THAN (63893721600) ENGINE = InnoDB,
 PARTITION p20240917 VALUES LESS THAN (63893808000) ENGINE = InnoDB,
 PARTITION p20240918 VALUES LESS THAN (63893894400) ENGINE = InnoDB,
 PARTITION p20240919 VALUES LESS THAN (63893980800) ENGINE = InnoDB,
 PARTITION p20240920 VALUES LESS THAN (63894067200) ENGINE = InnoDB,
 PARTITION p20240921 VALUES LESS THAN (63894153600) ENGINE = InnoDB,
 PARTITION p20240922 VALUES LESS THAN (63894240000) ENGINE = InnoDB,
 PARTITION p20240923 VALUES LESS THAN (63894326400) ENGINE = InnoDB,
 PARTITION p20240924 VALUES LESS THAN (63894412800) ENGINE = InnoDB,
 PARTITION p20240925 VALUES LESS THAN (63894499200) ENGINE = InnoDB,
 PARTITION p20240926 VALUES LESS THAN (63894585600) ENGINE = InnoDB,
 PARTITION p20240927 VALUES LESS THAN (63894672000) ENGINE = InnoDB,
 PARTITION p20240928 VALUES LESS THAN (63894758400) ENGINE = InnoDB,
 PARTITION p20240929 VALUES LESS THAN (63894844800) ENGINE = InnoDB,
 PARTITION p20240930 VALUES LESS THAN (63894931200) ENGINE = InnoDB,
 PARTITION p20241001 VALUES LESS THAN (63895017600) ENGINE = InnoDB,
 PARTITION p20241002 VALUES LESS THAN (63895104000) ENGINE = InnoDB) */;

select feeid, customerid, relid, businessunit, resourcetag, resourcename, resourceid, substring(convert_tz(createtime,'+00:00','+08:00'),1,19) as createtime, substring(convert_tz(updatetime,'+00:00','+08:00'),1,19) as updatetime, extendparam1, extendparam2, extendparam3, extendparam4, extendparam5, extendparam6 as extendparam6new, extendparam7, extendparam8, extendparam9, pricefactorname, chargemode, promotionid, promotiontype, resourcespecsize, resourcespecsizemeasure, spot, releasetype, date_format(substring(convert_tz(createtime,'+00:00','+08:00'),1,19),'%Y%m%d') as dtime from t_billing_feeextendinfo where createtime>= '2024-08-31 16:00:00'  and createtime< '2024-09-01 16:00:00';
[18 Sep 9:33] MySQL Verification Team
Hi Mr. xue,

Thank you very much for your bug report.

We have managed to repeat your test fully:

id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	filtered	Extra
1	SIMPLE	t1	p20240814,p20240901	range	idx_createTime	idx_createTime	5	NULL	1	100.00	Using index condition

However, this is not a bug.

Simply, for these types of queries, first partition always has to be consulted for some index statistics.

Not a bug.
[19 Sep 4:52] haizhen xue
mysql> explain select feeid, customerid, relid, businessunit, resourcetag, resourcename, resourceid, substring(convert_tz(createtime,'+00:00','+08:00'),1,19) as createtime, substring(convert_tz(updatetime,'+00:00','+08:00'),1,19) as updatetime, extendparam1, extendparam2, extendparam3, extendparam4, extendparam5, extendparam6 as extendparam6new, extendparam7, extendparam8, extendparam9, pricefactorname, chargemode, promotionid, promotiontype, resourcespeccsize, resourcespecsizemeasure, spot, releasetype, date_format(substring(convert_tz(createtime,'+00:00','+08:00'),1,19),'%Y%m%d') as dtime from t_billing_feeextendinfo where createtime>= '2024-08-31 16:00:00'  and createtime< '20224-09-01 00:00:00';
+----+-------------+-------------------------+---------------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+
| id | select_type | table                   | partitions          | type  | possible_keys  | key            | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+-------------------------+---------------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | t_billing_feeextendinfo | p20240814,p20240901 | range | idx_createTime | idx_createTime | 5       | NULL |    1 |   100.00 | Using index condition |
+----+-------------+-------------------------+---------------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql>
mysql>
mysql> explain select feeid, customerid, relid, businessunit, resourcetag, resourcename, resourceid, substring(convert_tz(createtime,'+00:00','+08:00'),1,19) as createtime, substring(convert_tz(updatetime,'+00:00','+08:00'),1,19) as updatetime, extendparam1, extendparam2, extendparam3, extendparam4, extendparam5, extendparam6 as extendparam6new, extendparam7, extendparam8, extendparam9, pricefactorname, chargemode, promotionid, promotiontype, resourcespecsize, resourcespecsizemeasure, spot, releasetype, date_format(substring(convert_tz(createtime,'+00:00','+08:00'),1,19),'%Y%m%d') as dtime from t_billing_feeextendinfo where createtime>= '2024-08-31 16:00:00'  and createtime< '2024-08-31 23:59:59';
+----+-------------+-------------------------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+
| id | select_type | table                   | partitions | type  | possible_keys  | key            | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+-------------------------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | t_billing_feeextendinfo | p20240901  | range | idx_createTime | idx_createTime | 5       | NULL |    1 |   100.00 | Using index condition |
+----+-------------+-------------------------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

why "where createtime>= '2024-08-31 16:00:00'  and createtime< '2024-08-31 23:59:59';" patition is p20240901.
[19 Sep 9:08] MySQL Verification Team
Hi Mr. xue,

Because in order to use index condition , the optimiser requires index statistics, which is in the first partition.