Repository navigation
Closed
Labels
comp-joinsJOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla...comp-query-analyzerSemantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules).performance
Description
Describe the situation
In 21.2, several complex test queries became visibly slower - regressing from <1s to 2-3s despite single-digit row counts. Further increasing query complexity results in combinatorial explosion of query analysis time.
This might be related to #20388 .
How to reproduce
Tables (can be empty):
CREATE TABLE hits (`date` Date, `row_type` Enum8('A' = 1, 'B' = 2, 'C' = 3), `user` UInt32)
ENGINE = MergeTree PARTITION BY toYYYYMM(date) ORDER BY date
CREATE TABLE users_list (`date` Date, `user` UInt32)
ENGINE = MergeTree PARTITION BY toYYYYMM(date) ORDER BY dateExample simplified query:
WITH
(SELECT count(*) AS users_cnt FROM (SELECT distinct user FROM users_list WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03')))
AS with1,
(SELECT count(*) AS users_cnt FROM (SELECT distinct user FROM users_list WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03')))
AS with2
SELECT main.date, averages.users AS average_daily_users, main.users AS users
FROM (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS main
LEFT JOIN (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS join1 ON main.date = join1.date
LEFT JOIN (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS join2 ON main.date = join2.date
LEFT JOIN (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS join3 ON main.date = join3.date
LEFT JOIN
(
SELECT inner.date as date, avg(users) AS users
FROM
(
WITH
(SELECT count(*) AS users_cnt FROM (SELECT distinct user FROM users_list WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03')))
AS with1,
(SELECT count(*) AS users_cnt FROM (SELECT distinct user FROM users_list WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03')))
AS with2
SELECT main.date as date, main.users AS users
FROM (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS main
LEFT JOIN (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS join1 ON main.date = join1.date
LEFT JOIN (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS join2 ON main.date = join2.date
LEFT JOIN (SELECT date, uniqExact(filtered.user) AS users FROM ( SELECT date, user FROM hits WHERE (1 = 1) AND (date >= '2020-06-01') AND (date <= '2020-06-03') AND (row_type = 'A') ) AS filtered GROUP BY date)
AS join3 ON main.date = join3.date
) AS inner
WHERE inner.date != '1970-01-01'
GROUP BY date
) AS averages ON main.date = averages.dateResults:
First row is the query above, further rows are with extra added WITH entries.
| WITHs in outer query | WITHs in inner query | time in 21.1.5.4 | time in 21.2.4.6 |
|---|---|---|---|
| 2 | 2 | 0.080s | 0.8s |
| 3 | 2 | 0.080s | 2.5s |
| 3 | 3 | 0.081s | 7.4s |
Interestingly, in the last row, even dropping (1 = 1) AND from all WHEREs reduces execution time by 1s.
Metadata
Metadata
Assignees
Labels
comp-joinsJOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla...comp-query-analyzerSemantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules).performance
Activity
den-crane commentedon Feb 22, 2021
Check with version 21.3.1.6083 https://repo.yandex.ru/clickhouse/deb/testing/main/
adrian17 commentedon Feb 22, 2021
Reproducible. A tiny little bit better, but still orders of magnitude worse than in 21.1.x.
filimonov commentedon Mar 3, 2021
@amosbird can you check it? Looks related to WITH changes.
adrian17 commentedon Mar 3, 2021
For the record, after some experimenting I found out that even without
WITH, addingJOINs appears to slowly but quadratically increase query planning time - with 12 identical nontrivial subquery JOINs I got planning time ~1s, with 20 JOINs it reached >4s. But while this was an artificial edge case, apparently addingWITHsignificantly speeds up the quadratic time increase, to the point where it's visible on my real queries with single-digitJOINs.(and in case anyone asks, my
JOINs only join final results with <100 rows, I'm not trying to use them for the "hot path" metric calculation :) )filimonov commentedon Apr 23, 2021
Any update there?
vdimir commentedon Apr 27, 2021
Another example, maybe connected
#21557
adrian17 commentedon Feb 21, 2024
This is still reproducible, in fact appears to be several times worse now. This appears to have slowly degraded over time, as for example 22.6 is almost as bad. Same queries, same empty tables (on a faster machine too):
The issue #21557 linked above appears to be at least partially connected - the time for
EXPLAIN SYNTAXalone for the biggest (45s) query, is 9s.Algunenano commentedon Feb 21, 2024
Tackled as part of the new analyzer (
allow_experimental_analyzer).adrian17 commentedon Feb 21, 2024
OK, what's the status/expected stability of it? I've been seeing it in changelogs since 2022, but it never appeared usable. I just tried it and it fails for a bunch of our queries - do you accept issues for it?
Algunenano commentedon Feb 21, 2024
Experimental and not recommended for production. There is still a few tests that don't pass with the new analyzer as well as some reported problems.
Yes, please. If they aren't reported now they'll need to be reported once it becomes enabled by default and it will be more painful for everyone.
Algunenano commentedon May 6, 2024
The analyzer is now enabled by default