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

UnionAll should be able to reserve the order property comming from its children #59869

Open
winoros opened this issue Mar 3, 2025 · 1 comment
Labels
sig/execution SIG execution sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.

Comments

@winoros
Copy link
Member

winoros commented Mar 3, 2025

Enhancement

suppose that we have table

create table t(a int, index idx(a));

and a SQL select * from (select a from t union all select a from t) t order by a limit 1.

The best plan is that we keep the order property by reading index idx of table t and the union can also use the order property to perform a merge sort to output the data. In this way, we don't need any extra sort to sort the data can directly perform limit upon the union all.

current TiDB lacks the functionality and will do a sort after the union all. no rewrite can achieve the best possible plan until the union all can do the merge sort.

@winoros winoros added sig/execution SIG execution sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement. labels Mar 3, 2025
@winoros
Copy link
Member Author

winoros commented Mar 5, 2025

Image
the problem will become clearer if we attach a lot of children to the union all.

The SQL just wants 10k rows, but we read 1 million rows in total. the read amplification is 10x(the number of its children)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution sig/planner SIG: Planner type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

No branches or pull requests

1 participant