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

LIMIT pushdown to UNION ALL queries #23239

Open
alexey-milovidov opened this issue Apr 17, 2021 · 0 comments
Open

LIMIT pushdown to UNION ALL queries #23239

alexey-milovidov opened this issue Apr 17, 2021 · 0 comments

Comments

@alexey-milovidov
Copy link
Member

alexey-milovidov commented Apr 17, 2021

The first two queries below are slow due to missing optimization.
The last query is fast - this is how them are supposed to be optimized.

milovidov-desktop :) SELECT * FROM (SELECT * FROM numbers(1000000000) ORDER BY number UNION ALL SELECT * FROM numbers(1000000000) ORDER BY number) ORDER BY number LIMIT 10

SELECT *
FROM 
(
    SELECT *
    FROM numbers(1000000000)
    ORDER BY number ASC
    UNION ALL
    SELECT *
    FROM numbers(1000000000)
    ORDER BY number ASC
)
ORDER BY number ASC
LIMIT 10

Query id: ad5e3c17-6840-480a-ae12-27fe90d60e71

Cancelling query.
Ok.
Query was cancelled.

0 rows in set. Elapsed: 2.809 sec. Processed 203.20 million rows, 1.63 GB (72.34 million rows/s., 578.76 MB/s.) 

milovidov-desktop :) SELECT * FROM (SELECT * FROM numbers(1000000000) ORDER BY number UNION ALL SELECT * FROM numbers(1000000000) ORDER BY number) LIMIT 10

SELECT *
FROM 
(
    SELECT *
    FROM numbers(1000000000)
    ORDER BY number ASC
    UNION ALL
    SELECT *
    FROM numbers(1000000000)
    ORDER BY number ASC
)
LIMIT 10

Query id: 98d7340d-4efb-4c13-aa4a-2dddb3aa0602

→ Progress: 241.58 million rows, 1.93 GB (73.05 million rows/s., 584.37 MB/s.) ███████████████████▌                                                                                                                                               11%^Cancelling query.
Ok.
Query was cancelled.

0 rows in set. Elapsed: 3.509 sec. Processed 256.45 million rows, 2.05 GB (73.09 million rows/s., 584.70 MB/s.) 

milovidov-desktop :) SELECT * FROM (SELECT * FROM numbers(1000000000) ORDER BY number LIMIT 10 UNION ALL SELECT * FROM numbers(1000000000) ORDER BY number LIMIT 10) ORDER BY number LIMIT 10

SELECT *
FROM 
(
    SELECT *
    FROM numbers(1000000000)
    ORDER BY number ASC
    LIMIT 10
    UNION ALL
    SELECT *
    FROM numbers(1000000000)
    ORDER BY number ASC
    LIMIT 10
)
ORDER BY number ASC
LIMIT 10

Query id: a550a49f-a792-4084-8294-e6e6304281ce

┌─number─┐
│      0 │
│      0 │
│      1 │
│      1 │
│      2 │
│      2 │
│      3 │
│      3 │
│      4 │
│      4 │
└────────┘

10 rows in set. Elapsed: 0.794 sec. Processed 2.00 billion rows, 16.00 GB (2.52 billion rows/s., 20.16 GB/s.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant