Skip to content

IndexFullScan with higher cost is chosen to avoid sorting for window function #46177

@pcqz

Description

@pcqz

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

mysql> explain format='verbose'  select row_number() over(order by a.k), a.* from (select * from sbtest1 where id<10) a;
+------------------------------------+------------+--------------+-----------+-----------------------------+--------------------------------------------------------------------------------------------------+
| id                                 | estRows    | estCost      | task      | access object               | operator info                                                                                    |
+------------------------------------+------------+--------------+-----------+-----------------------------+--------------------------------------------------------------------------------------------------+
| Projection_8                       | 2.85       | 16898882.30  | root      |                             | Column#6, sbtest1.sbtest1.id, sbtest1.sbtest1.k, sbtest1.sbtest1.c, sbtest1.sbtest1.pad          |
| └─Window_9                         | 2.85       | 16898880.88  | root      |                             | row_number()->Column#6 over(order by sbtest1.sbtest1.k rows between current row and current row) |
|   └─IndexLookUp_13                 | 2.85       | 16898880.88  | root      |                             |                                                                                                  |
|     ├─Selection_12(Build)          | 2.85       | 253400000.00 | cop[tikv] |                             | lt(sbtest1.sbtest1.id, 10)                                                                       |
|     │ └─IndexFullScan_10           | 1000000.00 | 203500000.00 | cop[tikv] | table:sbtest1, index:k_1(k) | keep order:true                                                                                  |
|     └─TableRowIDScan_11(Probe)     | 2.85       | 898.48       | cop[tikv] | table:sbtest1               | keep order:false                                                                                 |
+------------------------------------+------------+--------------+-----------+-----------------------------+--------------------------------------------------------------------------------------------------+
6 rows in set (0.00 sec)

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

mysql> explain format='verbose'  select row_number() over(order by a.k), a.* from (select * from sbtest1 use index(primary) where id<10) a;
+-------------------------------+---------+---------+-----------+---------------+--------------------------------------------------------------------------------------------------+
| id                            | estRows | estCost | task      | access object | operator info                                                                                    |
+-------------------------------+---------+---------+-----------+---------------+--------------------------------------------------------------------------------------------------+
| Projection_8                  | 2.85    | 561.13  | root      |               | Column#6, sbtest1.sbtest1.id, sbtest1.sbtest1.k, sbtest1.sbtest1.c, sbtest1.sbtest1.pad          |
| └─Window_9                    | 2.85    | 559.71  | root      |               | row_number()->Column#6 over(order by sbtest1.sbtest1.k rows between current row and current row) |
|   └─Sort_12                   | 2.85    | 559.71  | root      |               | sbtest1.sbtest1.k                                                                                |
|     └─TableReader_11          | 2.85    | 222.33  | root      |               | data:TableRangeScan_10                                                                           |
|       └─TableRangeScan_10     | 2.85    | 898.48  | cop[tikv] | table:sbtest1 | range:[0,10), keep order:false                                                                   |
+-------------------------------+---------+---------+-----------+---------------+--------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

3. What did you see instead (Required)

IndexFullScan with higher cost is chosen.

4. What is your TiDB version? (Required)

v7.1.0

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.found/gsfound by gsseverity/majorsig/plannerSIG: Plannertype/bugThe issue is confirmed as a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions