执行计划有错误

【 TiDB 使用环境】生产环境 /测试/ Poc
【 TiDB 版本】7.5
【复现路径】做过哪些操作出现的问题
【遇到的问题:问题现象及影响】

CREATE TABLE `win_ticket` (
  `draw_id` int(11) NOT NULL ,
  `ticket_no` varchar(30) NOT NULL ,
  `win_prz_lvl` int(11) NOT NULL ,
  `clerk_id` int(11) unsigned DEFAULT NULL ,
  `ticket_pwd` varchar(40) DEFAULT NULL,
  `sale_time` datetime DEFAULT NULL ,
  `chances` int(11) unsigned DEFAULT NULL ,
  `selection` varchar(4000) DEFAULT NULL ,
  `multiple` int(11) unsigned DEFAULT NULL ,
  `transaction_id` int(11) DEFAULT NULL ,
  `term_id` int(11) unsigned DEFAULT NULL ,
  `prz_cnt` decimal(50,12) DEFAULT NULL ,
  `prz_amt` decimal(50,2) DEFAULT NULL ,
  `tax_amt` decimal(50,2) DEFAULT NULL ,
  `paid_type` int(11) DEFAULT NULL ,
  `paid_time` datetime DEFAULT NULL ,
  `paid_operator_id` int(11) DEFAULT NULL,
  `withdraw_amt` int(11) DEFAULT NULL ,
  `bno` int(11) DEFAULT NULL ,
  `eno` int(11) DEFAULT NULL ,
  `win_time` datetime NOT NULL ,
  `paid_term_id` int(11) DEFAULT NULL ,
  `end_paid_time` datetime DEFAULT NULL ,
  `win_selection_chances` int(11) DEFAULT NULL ,
  `payment_type` int(11) DEFAULT NULL ,
  PRIMARY KEY (`draw_id`,`ticket_no`,`win_prz_lvl`) /*T![clustered_index] CLUSTERED */,
  KEY `ind_paid_time` (`paid_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

这表在paid_time列上加了索引 KEY ind_paid_time (paid_time),这里不知道执行计划为什么出问题了

导出统计信息
win_ticket.json (1.1 MB)

https://docs.pingcap.com/zh/tidb/stable/statistics#导出统计信息
按这个把统计信息导出来下

已经上传

区间问题

具体说说?

隐式转换???也不应该啊

加个 hint 试试,统计信息收集下试试。

统计重新收集后正常了

1 个赞

你如果是分区表 建议锁定统计信息

这个是普通表

show stats_buckets where table_name=‘win_ticket’ and Column_name=‘paid_time’;
看下现在的统计信息

感觉还是评估的问题

1701941416490

这意思是说ind_paid_time这个索引只有一部分,所以不能用?

你的这个db怎么老是遇到这种统计信息的问题。
好几次了。要么是某个分区统计信息收集了以后global的不行,要么是某个字段的统计信息不行。
这类问题我感觉你碰上好几次了。已经到了 不能当作偶然对待的程度了。

explain方式看评估的index scan比全表成本要低,不知道这个评估咋算的,但看直方图和distinct值 都不应该有60多万行

// FormatForExplain format the content in the format expected to be printed in the execution plan.
// case 1: if stats version is 0, print stats:pseudo.
// case 2: if stats version is not 0, and there are column/index stats that are not full loaded,
// print stats:partial, then print status of 3 column/index status at most. For the rest, only
// the count will be printed, in the format like (more: 1 onlyCmsEvicted, 2 onlyHistRemained).

https://github.com/pingcap/tidb/blob/5db7c6aab54809a2e15a9387e48e125fe1dfe4ca/pkg/sessionctx/stmtctx/stmtctx.go#L1416C19-L1416C19

代码中有一段注释解释了stats:partial意思。确实是索引的统计信息没有完全加载造成的。

有256条

你肯定记错了,单纯说执行计划选择不对的就发过这一个

是想看看符合你SQL条件的buckets是什么情况

统计没完全加载我记得tidb是有看到过有人发过几个相关的问题,比如什么一个tidb server分析了另外一个tidb server看不到