pingcap / pingcap/tidb

support using ordering property to get better join order

Open
#65,208 3 comments 0 reactions 1 assignee Claimed by @guo-shaoge View on GitHub
report/customer sig/planner type/enhancement
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Feature Request

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

Query:
```
EXPLAIN format='verbose'
SELECT
x.c1,
x.c2
FROM x
INNER JOIN y
ON y.c3 = x.c4
INNER JOIN x AS x1
ON x1.c4 = y.c5
WHERE x.c6 = 'STR_VAL_1'
AND x1.c6 = 'STR_VAL_1'
AND x.c7 IN (
'STR_1',
'STR_2',
'STR_3',
'STR_4',
'STR_5'
)
AND x1.c7 IN (
'STR_1',
'STR_2',
'STR_3',
'STR_4',
'STR_5'
)
AND x1.c8 = 'STR_VAL_2'
ORDER BY x.c2 ASC
LIMIT 1000 OFFSET 0;
```
### 2. What did you expect to see? (Required)
TiDB choose join order of `(x1 inner join y) inner join x` and use `TopN + HashJoin`, which means we cannot leverage the ordering property.

Image

### 3. What did you see instead (Required)
The better plan: choose join order of `(x inner join y) inner join x1` and use `Limit + IndexJoin(keep order: true)`, so we can leverage the ordering property and only need to get 1000 rows from Join output.

Image

### 4. What is your TiDB version? (Required)
```
TiDB root@127.0.0.1:jsm_assets> select tidb_version();
+-----------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------+
| Release Version: v8.5.4-97-g06e6c12236-dirty |
| Edition: Community |
| Git Commit Hash: 06e6c12236e2eeefb1a6d3fe7cc02a48bdf13131 |
| Git Branch: release-8.5 |
| UTC Build Time: 2025-12-24 06:48:08 |
| GoVersion: go1.25.5 |
| Race Enabled: false |
| Check Table Before Drop: false |
| Store: unistore |
+-----------------------------------------------------------+
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.