pingcap / pingcap/tidb

The estRows in explain plan is wrong

Open
#39,512 0 comments 0 reactions 1 assignee Claimed by @time-and-fate View on GitHub
severity/minor sig/planner type/bug
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 t2 (
a INT NOT NULL,
b int NOT NULL,
c INT NOT NULL,
d INT NOT NULL,
primary key(a) NONCLUSTERED
)

PARTITION BY HASH(a)
PARTITIONS 4;

insert into t2 values(1,1,1,1), (2,2,2,2), (3,1,1,1), (4,2,2,2), (5,1,1,1), (6,2,2,2), (7,1,1,1), (8,2,2,2);
analyze table t2;

explain select /*+ use_index(t2, primary) */ b from t2 where a < 10 order by a desc limit 1;
```

### 2. What did you expect to see? (Required)
The `estRows` for `IndexRangeScan_14` should equal to the value of `limit`.

If the table is normal table, the plan seems like below
```
+--------------------------------------+---------+-----------+----------------------------+----------------------------------------+
| id | estRows | task | access object | operator info |
+--------------------------------------+---------+-----------+----------------------------+----------------------------------------+
| Projection_8 | 1.00 | root | | test.t3.b |
| └─Limit_13 | 1.00 | root | | offset:0, count:1 |
| └─Projection_22 | 1.00 | root | | test.t3.a, test.t3.b |
| └─IndexLookUp_21 | 1.00 | root | | |
| ├─Limit_20(Build) | 1.00 | cop[tikv] | | offset:0, count:1 |
| │ └─IndexRangeScan_18 | 1.00 | cop[tikv] | table:t3, index:PRIMARY(a) | range:[-inf,10), keep order:true, desc |
| └─TableRowIDScan_19(Probe) | 1.00 | cop[tikv] | table:t3 | keep order:false |
+--------------------------------------+---------+-----------+----------------------------+----------------------------------------+
7 rows in set (0.01 sec)
```

### 3. What did you see instead (Required)
```
mysql> explain select /*+ use_index(t2, primary) */ b from t2 where a < 10 order by a desc limit 1;
+------------------------------------+---------+-----------+----------------------------+-----------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------------------+---------+-----------+----------------------------+-----------------------------------------+
| Projection_8 | 1.00 | root | | test.t2.b |
| └─TopN_10 | 1.00 | root | | test.t2.a:desc, offset:0, count:1 |
| └─IndexLookUp_17 | 1.00 | root | partition:all | |
| ├─Limit_16(Build) | 1.00 | cop[tikv] | | offset:0, count:1 |
| │ └─IndexRangeScan_14 | 16.00 | cop[tikv] | table:t2, index:PRIMARY(a) | range:[-inf,10), keep order:false, desc |
| └─TableRowIDScan_15(Probe) | 1.00 | cop[tikv] | table:t2 | keep order:false |
+------------------------------------+---------+-----------+----------------------------+-----------------------------------------+
6 rows in set (0.01 sec)
```

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

latest master

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.