Skip to content

planner: zero estimation result if all values are in TopN #47400

@qw4990

Description

@qw4990

Enhancement

create table t (a int, key(a));
insert into t values (1), (3);
analyze table t with 2 topn;
insert into t values (2);

explain select * from t where a=2;
+------------------------+---------+-----------+---------------------+-------------------------------+
| id                     | estRows | task      | access object       | operator info                 |
+------------------------+---------+-----------+---------------------+-------------------------------+
| IndexReader_6          | 0.00    | root      |                     | index:IndexRangeScan_5        |
| └─IndexRangeScan_5     | 0.00    | cop[tikv] | table:t, index:a(a) | range:[2,2], keep order:false |
+------------------------+---------+-----------+---------------------+-------------------------------+
2 rows in set (0.00 sec)

After v6.5, in the case above, the estimation result for a=2 is zero, which seems dangerous.

If no TopN, it'll be 1:

create table t (a int, key(a));
insert into t values (1), (3);
analyze table t with 0 topn;
insert into t values (2);

mysql> explain select * from t where a=2;
+------------------------+---------+-----------+---------------------+-------------------------------+
| id                     | estRows | task      | access object       | operator info                 |
+------------------------+---------+-----------+---------------------+-------------------------------+
| IndexReader_6          | 1.00    | root      |                     | index:IndexRangeScan_5        |
| └─IndexRangeScan_5     | 1.00    | cop[tikv] | table:t, index:a(a) | range:[2,2], keep order:false |
+------------------------+---------+-----------+---------------------+-------------------------------+

Should we avoid using 0 in this case (this value is not in TopN and there is no Histogram)?

Metadata

Metadata

Assignees

Labels

affects-5.4This bug affects the 5.4.x(LTS) versions.affects-6.1This bug affects the 6.1.x(LTS) versions.affects-6.5This bug affects the 6.5.x(LTS) versions.affects-7.1This bug affects the 7.1.x(LTS) versions.affects-7.5This bug affects the 7.5.x(LTS) versions.affects-8.1This bug affects the 8.1.x(LTS) versions.affects-8.5This bug affects the 8.5.x(LTS) versions.epic/cardinality-estimationthe optimizer cardinality estimationreport/customerCustomers have encountered this bug.severity/majorsig/plannerSIG: Plannertype/enhancementThe issue or PR belongs to an enhancement.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions