Skip to content

Performance regression in 21.2 - query analysis time explosion with several WITH and JOINs #21067

Closed

Description

@adrian17

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 date

Example 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.date

Results:
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.

Activity

  1. adrian17 commented on Feb 22, 2021

    @adrian17
    Author

    Reproducible. A tiny little bit better, but still orders of magnitude worse than in 21.1.x.

    WITHs in outer query WITHs in inner query time in 21.1.5.4 time in 21.2.4.6 time in 21.3.1.6083
    2 2 0.080s 0.8s 0.65s
    3 2 0.080s 2.5s 2.25s
    3 3 0.081s 7.4s 6.6s
  2. filimonov commented on Mar 3, 2021

    @filimonov
    Contributor

    @amosbird can you check it? Looks related to WITH changes.

  3. adrian17 commented on Mar 3, 2021

    @adrian17
    Author

    For the record, after some experimenting I found out that even without WITH, adding JOINs 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 adding WITH significantly speeds up the quadratic time increase, to the point where it's visible on my real queries with single-digit JOINs.

    (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 :) )

  4. added
    comp-query-analyzer
    comp-joins
    on Mar 3, 2021
  5. filimonov commented on Apr 23, 2021

    @filimonov
    Contributor

    Any update there?

  6. self-assigned this
    on Apr 25, 2021
  7. vdimir commented on Apr 27, 2021

    @vdimir
    Member

    Another example, maybe connected
    #21557

  8. adrian17 commented on Feb 21, 2024

    @adrian17
    Author

    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):

    WITHs in outer query WITHs in inner query time in 24.1.5.6
    2 2 1.937s
    3 2 9.103s
    3 3 45.637s

    The issue #21557 linked above appears to be at least partially connected - the time for EXPLAIN SYNTAX alone for the biggest (45s) query, is 9s.

  9. Algunenano commented on Feb 21, 2024

    @Algunenano
    Member

    Tackled as part of the new analyzer (allow_experimental_analyzer).

  10. adrian17 commented on Feb 21, 2024

    @adrian17
    Author

    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?

  11. Algunenano commented on Feb 21, 2024

    @Algunenano
    Member

    OK, what's the status/expected stability of it?

    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.

    I just tried it and it fails for a bunch of our queries - do you accept issues for it?

    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.

  12. Algunenano commented on May 6, 2024

    @Algunenano
    Member

    The analyzer is now enabled by default

  13. self-assigned this
    on May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp-joinscomp-query-analyzerperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions