Skip to content

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

Closed
@qw4990

Description

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)?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

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