【 TiDB 使用环境】生产环境
【 TiDB 版本】6.5.7,
【遇到的问题】
最基本的sql。优化器选择的执行计划有问题。导致查询慢。
sql语句:
select * from t1 where create_time >= ‘2024-03-01 00:00:00’ limit 2;
这个表create_time是有索引的。但查询时默认并不走索引。
mysql> explain select * from t1 where create_time >=‘2024-03-01 00:00:00’ limit 2;
±-----------------------------±--------±----------±------------------±-----------------------------------------------------------------+
| id | estRows | task | access object | operator info |
±-----------------------------±--------±----------±------------------±-----------------------------------------------------------------+
| Limit_8 | 2.00 | root | | offset:0, count:2 |
| └─TableReader_13 | 2.00 | root | | data:Limit_12 |
| └─Limit_12 | 2.00 | cop[tikv] | | offset:0, count:2 |
| └─Selection_11 | 2.00 | cop[tikv] | | ge(t1.weplay_user.create_time, 2024-03-01 00:00:00.000000) |
| └─TableFullScan_10 | 21.50 | cop[tikv] | table:weplay_user | keep order:false |
±-----------------------------±--------±----------±------------------±-----------------------------------------------------------------+
5 rows in set (0.00 sec)
mysql> explain select * from t1 use index(idx_create_time) where create_time >=‘2024-03-01 00:00:00’ limit 2;
±-------------------------------±--------±----------±------------------------------------------------------±---------------------------------------------------+
| id | estRows | task | access object | operator info |
±-------------------------------±--------±----------±------------------------------------------------------±---------------------------------------------------+
| IndexLookUp_13 | 2.00 | root | | limit embedded(offset:0, count:2) |
| ├─Limit_12(Build) | 2.00 | cop[tikv] | | offset:0, count:2 |
| │ └─IndexRangeScan_10 | 2.00 | cop[tikv] | table:t1, index:idx_create_time(create_time) | range:[2024-03-01 00:00:00,+inf], keep order:false |
| └─TableRowIDScan_11(Probe) | 2.00 | cop[tikv] | table:t1 | keep order:false |
±-------------------------------±--------±----------±------------------------------------------------------±---------------------------------------------------+