pingcap/tidb

planner: Limit cannot be pushed down through IndexLookUp when the keep-order is true and it has a table-side Selection

Open

#21,250 opened on Nov 24, 2020

View on GitHub
 (10 comments) (0 reactions) (2 assignees)Go (40,090 stars) (6,186 forks)batch import
help wantedreport/customersig/plannertype/enhancement

Description

Development Task

mysql> create table t (a int, b int, key(a));
mysql> explain select * from t where b>10 order by a limit 1;
+-----------------------------------+---------+-----------+---------------------+--------------------------------+
| id                                | estRows | task      | access object       | operator info                  |
+-----------------------------------+---------+-----------+---------------------+--------------------------------+
| Limit_12                          | 1.00    | root      |                     | offset:0, count:1              |
| └─Projection_25                   | 1.00    | root      |                     | test.t.a, test.t.b             |
|   └─IndexLookUp_24                | 1.00    | root      |                     |                                |
|     ├─IndexFullScan_21(Build)     | 3.00    | cop[tikv] | table:t, index:a(a) | keep order:true, stats:pseudo  |
|     └─Selection_23(Probe)         | 1.00    | cop[tikv] |                     | gt(test.t.b, 10)               |
|       └─TableRowIDScan_22         | 3.00    | cop[tikv] | table:t             | keep order:false, stats:pseudo |
+-----------------------------------+---------+-----------+---------------------+--------------------------------+

In the case above, we can push Limit down to the probe side to get a better plan:

Limit                           root
  Projection                    root
    IndexLookUp                 root
      IndexFullScan             cop
      Limit                     cop
        Selection               cop
           TableRowIDScan       cop

Contributor guide