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

Query optimization: rewrite GROUP BY with LIMIT to DISTINCT. #8777

Open
alexey-milovidov opened this issue Jan 22, 2020 · 0 comments
Open

Query optimization: rewrite GROUP BY with LIMIT to DISTINCT. #8777

alexey-milovidov opened this issue Jan 22, 2020 · 0 comments

Comments

@alexey-milovidov
Copy link
Member

Use case
If query contains GROUP BY but no aggregate functions and also contains LIMIT, remove GROUP BY and add DISTINCT.

SELECT a, b, c FROM t GROUP BY a, b, c LIMIT 10

->

SELECT DISTINCT a, b, c FROM t LIMIT 10

This optimization is viable, because DISTINCT can output records without waiting for full result and it can stop earlier in presence of LIMIT.

But if LIMIT is not present, DISTINCT is less efficient than GROUP BY due to lack of some specializations.

PS. Advanced version:

SELECT a, b, c, any(x), any(y) FROM t GROUP BY a, b, c LIMIT 10

->

SELECT a, b, c, x, y FROM t LIMIT 1 BY a, b, c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants