Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push down sort property to range partition table #26166

Closed
lysu opened this issue Jul 13, 2021 · 6 comments
Closed

Push down sort property to range partition table #26166

lysu opened this issue Jul 13, 2021 · 6 comments
Assignees
Labels
component/tablepartition This issue is related to Table Partition of TiDB. sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.

Comments

@lysu
Copy link
Contributor

lysu commented Jul 13, 2021

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

mysql> create table t1(id int primary key, v int) partition by range(id) (partition p1 values less than(10000), partition p2 values less than(20000));
Query OK, 0 rows affected (0.01 sec)

mysql> create table t2(id int primary key, v int);
Query OK, 0 rows affected (0.01 sec)

mysql> explain select * from t1 where id > 20001 order by id limit 1;
+-----------------------------+---------+-----------+----------------+----------------------------------------------------+
| id                          | estRows | task      | access object  | operator info                                      |
+-----------------------------+---------+-----------+----------------+----------------------------------------------------+
| TopN_8                      | 1.00    | root      |                | test.t1.id, offset:0, count:1                      |
| └─TableReader_17            | 1.00    | root      | partition:dual | data:TopN_16                                       |
|   └─TopN_16                 | 1.00    | cop[tikv] |                | test.t1.id, offset:0, count:1                      |
|     └─TableRangeScan_15     | 3333.33 | cop[tikv] | table:t1       | range:(20001,+inf], keep order:false, stats:pseudo |
+-----------------------------+---------+-----------+----------------+----------------------------------------------------+
4 rows in set (0.00 sec)

mysql> explain select * from t2 where id > 20001 order by id limit 1;
+-----------------------------+---------+-----------+---------------+---------------------------------------------------+
| id                          | estRows | task      | access object | operator info                                     |
+-----------------------------+---------+-----------+---------------+---------------------------------------------------+
| Limit_12                    | 1.00    | root      |               | offset:0, count:1                                 |
| └─TableReader_22            | 1.00    | root      |               | data:Limit_21                                     |
|   └─Limit_21                | 1.00    | cop[tikv] |               | offset:0, count:1                                 |
|     └─TableRangeScan_20     | 1.00    | cop[tikv] | table:t2      | range:(20001,+inf], keep order:true, stats:pseudo |
+-----------------------------+---------+-----------+---------------+---------------------------------------------------+
4 rows in set (0.01 sec)

mysql> set tidb_partition_prune_mode='dynamic';
Query OK, 0 rows affected (0.00 sec)

mysql> explain select * from t1 where id > 20001 order by id limit 1;
+-----------------------------+---------+-----------+----------------+----------------------------------------------------+
| id                          | estRows | task      | access object  | operator info                                      |
+-----------------------------+---------+-----------+----------------+----------------------------------------------------+
| TopN_8                      | 1.00    | root      |                | test.t1.id, offset:0, count:1                      |
| └─TableReader_17            | 1.00    | root      | partition:dual | data:TopN_16                                       |
|   └─TopN_16                 | 1.00    | cop[tikv] |                | test.t1.id, offset:0, count:1                      |
|     └─TableRangeScan_15     | 3333.33 | cop[tikv] | table:t1       | range:(20001,+inf], keep order:false, stats:pseudo |
+-----------------------------+---------+-----------+----------------+----------------------------------------------------+
4 rows in set (0.00 sec)

mysql> explain select * from t2 where id > 20001 order by id limit 1;
+-----------------------------+---------+-----------+---------------+---------------------------------------------------+
| id                          | estRows | task      | access object | operator info                                     |
+-----------------------------+---------+-----------+---------------+---------------------------------------------------+
| Limit_12                    | 1.00    | root      |               | offset:0, count:1                                 |
| └─TableReader_22            | 1.00    | root      |               | data:Limit_21                                     |
|   └─Limit_21                | 1.00    | cop[tikv] |               | offset:0, count:1                                 |
|     └─TableRangeScan_20     | 1.00    | cop[tikv] | table:t2      | range:(20001,+inf], keep order:true, stats:pseudo |
+-----------------------------+---------+-----------+---------------+---------------------------------------------------+
4 rows in set (0.00 sec)

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

t1, t2 be same

3. What did you see instead (Required)

the partition table can not be eliminated as limitN (even with dynamic prune)

4. What is your TiDB version? (Required)

newest master

@lysu lysu added the type/bug The issue is confirmed as a bug. label Jul 13, 2021
@yudongusa yudongusa added the sig/sql-infra SIG: SQL Infra label Aug 23, 2021
@bb7133 bb7133 removed the sig/sql-infra SIG: SQL Infra label Aug 24, 2021
@winoros
Copy link
Member

winoros commented Nov 23, 2021

This needs a MergeSort operator.
It's not a thing that can be solved as a bug.

@time-and-fate time-and-fate added type/enhancement The issue or PR belongs to an enhancement. and removed type/bug The issue is confirmed as a bug. severity/major labels Nov 23, 2021
@time-and-fate
Copy link
Member

As stated above, we don't support keep order on partitions like we do on a single table. We need to implement this functionality.
So converting it to an enhancement.

@time-and-fate time-and-fate changed the title TopN on range partition pk couldn't be eliminated as limitN Push down sort property to range partition table Nov 23, 2021
@mjonss
Copy link
Contributor

mjonss commented Feb 14, 2022

/component tablepartition

@ti-chi-bot ti-chi-bot added the component/tablepartition This issue is related to Table Partition of TiDB. label Feb 14, 2022
@qw4990
Copy link
Contributor

qw4990 commented May 27, 2022

image

@qw4990
Copy link
Contributor

qw4990 commented May 27, 2022

Or for this case, we can generate a plan like this:

TopN
  IndexReader
    Limit
      IndexScan

@winoros
Copy link
Member

winoros commented Apr 26, 2023

Now the normal partition table cases can have a clean order property push down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/tablepartition This issue is related to Table Partition of TiDB. sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

No branches or pull requests

9 participants