planner: prefix index can lose to table full scan
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
```
create table tx(
a text collate utf8mb4_general_ci,
index ia3(a(3)),
index ia10(a(10)),
index ia(a(100))
);
insert into tx values('aaAAaaaAAAabbc'), ('AaAaAaAaAaAbBC'), ('AAAaabbBBbbb'), ('AAAaabbBBbbbccc');
insert into tx values('b'), ('bBb'), ('Bb'), ('bA'), ('BBBB'), ('BBBBBDDDDDdd'), ('bbbbBBBBbbBBR'), ('BBbbBBbbBBbbBBRRR');
```
### 2. What did you expect to see? (Required)
If you don't collect statistics (do not run ANALYZE) - the index will be chosen:
```
tidb> explain select * from tx where a = 'aaa';
+-------------------------------+---------+-----------+------------------------+-----------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------------+---------+-----------+------------------------+-----------------------------------------------------------------------------+
| IndexLookUp_9 | 10.00 | root | | |
| ├─IndexRangeScan_6(Build) | 10.00 | cop[tikv] | table:tx, index:ia3(a) | range:["\x00A\x00A\x00A","\x00A\x00A\x00A"], keep order:false, stats:pseudo |
| └─Selection_8(Probe) | 10.00 | cop[tikv] | | eq(test.tx.a, "aaa") |
| └─TableRowIDScan_7 | 10.00 | cop[tikv] | table:tx | keep order:false, stats:pseudo |
+-------------------------------+---------+-----------+------------------------+-----------------------------------------------------------------------------+
```
This is the desired plan.
### 3. What did you see instead (Required)
If you run ANALYZE - TableFullScan wins:
```
tidb> analyze table tx;
Query OK, 0 rows affected, 1 warning (0.039 sec)
tidb> explain select * from tx where a = 'aaa';
+-------------------------+---------+-----------+---------------+----------------------+
| id | estRows | task | access object | operator info |
+-------------------------+---------+-----------+---------------+----------------------+
| TableReader_8 | 1.00 | root | | data:Selection_7 |
| └─Selection_7 | 1.00 | cop[tikv] | | eq(test.tx.a, "aaa") |
| └─TableFullScan_6 | 16.00 | cop[tikv] | table:tx | keep order:false |
+-------------------------+---------+-----------+---------------+----------------------+
3 rows in set (0.011 sec)
```
### 4. What is your TiDB version? (Required)
master branch
Contributor guide
Assessment
This issue has not been assessed yet.