Parallel apply to keep order
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Enhancement
When parallel apply is enabled, we lose the ability to keep order for a query with ORDER BY (and LIMIT). This can significantly degrade performance.
```
CREATE TABLE `t1` (
`a` int,
`b` int,
`c` int,
Primary KEY `ia` (`a`),
KEY `ib` (`b`),
KEY `ic` (`c`)
);
set @@cte_max_recursion_depth=10000000;
INSERT INTO t1 (a, b, c)
SELECT a, mod(a, 100) AS b, mod(a, 10) AS c
FROM (
WITH RECURSIVE x AS (
SELECT 1 AS a
UNION ALL
SELECT a + 1 AS a
FROM x
WHERE a < 1000000
)
SELECT a
FROM x
) AS subquery;
Analyze table t1;
```
This is the plan that I would like to see if you set "set tidb_enable_parallel_apply=ON;" But instead, you see a TOPN rather than LIMIT - and we cannot keep order.
```
explain select * from t1 t1a where exists (select /*+ NO_DECORRELATE() */ 1 from t1 t1b where t1b.b = t1a.b and t1b.a < t1a.a) order by t1a.a limit 1;
+-----------------------------------+------------+-----------+------------------------+----------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+-----------------------------------+------------+-----------+------------------------+----------------------------------------------------------------+
| Limit_22 | 1.00 | root | | offset:0, count:1 |
| └─Apply_39 | 1.00 | root | | CARTESIAN semi join, left side:TableReader_41 |
| ├─TableReader_41(Build) | 10000.99 | root | | data:TableFullScan_40 |
| │ └─TableFullScan_40 | 10000.99 | cop[tikv] | table:t1a | keep order:true |
| └─Limit_27(Probe) | 10000.99 | root | | offset:0, count:1 |
| └─IndexReader_34 | 10000.99 | root | | index:Limit_33 |
| └─Limit_33 | 10000.99 | cop[tikv] | | offset:0, count:1 |
| └─Selection_32 | 809980.18 | cop[tikv] | | lt(test.t1.a, test.t1.a) |
| └─IndexRangeScan_31 | 1012427.64 | cop[tikv] | table:t1b, index:ib(b) | range: decided by [eq(test.t1.b, test.t1.b)], keep order:false |
+-----------------------------------+------------+-----------+------------------------+----------------------------------------------------------------+
```
Contributor guide
Assessment
This issue has not been assessed yet.