pingcap / pingcap/tidb

planner: cardinality estimation results of the same predicate on different index-stats are not consistent

Open
#44,376 2 comments 0 reactions 2 assignees Claimed by @time-and-fate View on GitHub
epic/cardinality-estimation sig/planner type/enhancement
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)

Run SQLs below:
```
drop database if exists test;
create database test;
use test;
create table t (a int, b int, c int, d int , e int);
create index a on test.t (a);
explain format = 'verbose' select * from t where a = 1 and c < 1;
drop index a on test.t;
create index ac on test.t (a, c);
explain format = 'verbose' select * from t where a = 1 and c < 1;
drop index ac on test.t;
```

```
...
mysql> explain format = 'verbose' select * from t where a = 1 and c < 1;
+-------------------------------+---------+----------+-----------+---------------------+---------------------------------------------+
| id | estRows | estCost | task | access object | operator info |
+-------------------------------+---------+----------+-----------+---------------------+---------------------------------------------+
| IndexLookUp_11 | 3.32 | 19542.20 | root | | |
| ├─IndexRangeScan_8(Build) | 10.00 | 2035.00 | cop[tikv] | table:t, index:a(a) | range:[1,1], keep order:false, stats:pseudo |
| └─Selection_10(Probe) | 3.32 | 3179.08 | cop[tikv] | | lt(test.t.c, 1) |
| └─TableRowIDScan_9 | 10.00 | 2680.08 | cop[tikv] | table:t | keep order:false, stats:pseudo |
+-------------------------------+---------+----------+-----------+---------------------+---------------------------------------------+
4 rows in set (0.00 sec)

...

mysql> explain format = 'verbose' select * from t where a = 1 and c < 1;
+-------------------------------+---------+----------+-----------+-------------------------+----------------------------------------------------+
| id | estRows | estCost | task | access object | operator info |
+-------------------------------+---------+----------+-----------+-------------------------+----------------------------------------------------+
| IndexLookUp_10 | 33.23 | 65047.18 | root | | |
| ├─IndexRangeScan_8(Build) | 33.23 | 7554.20 | cop[tikv] | table:t, index:ac(a, c) | range:[1 -inf,1 1), keep order:false, stats:pseudo |
| └─TableRowIDScan_9(Probe) | 33.23 | 8906.80 | cop[tikv] | table:t | keep order:false, stats:pseudo |
+-------------------------------+---------+----------+-----------+-------------------------+----------------------------------------------------+
3 rows in set (0.00 sec)
```

### 2. What did you expect to see? (Required)

The estimation result of `a=1 and c<1` on `ac` should be less than it on `a`.

### 3. What did you see instead (Required)

### 4. What is your TiDB version? (Required)

```
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v7.2.0-alpha-316-gcf4dedf35d
Edition: Community
Git Commit Hash: cf4dedf35dd7492b6648b6b9f45f3d69fe20f803
Git Branch: master
UTC Build Time: 2023-06-02 06:31:19
GoVersion: go1.20.2
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: unistore |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.