Skip to content

trivial view on top of distributed table can significantly impact the performance of some queries #51645

Closed

Description

@filimonov
create table x engine = MergeTree ORDER BY tuple() AS SELECT intDiv(number,100000) as a, number as b FROM numbers(1000000000);
SET prefer_localhost_replica = 0;
CREATE TABLE x_dist AS x ENGINE = Distributed(test_cluster_two_shards_localhost, currentDatabase(), x);
CREATE VIEW x_dist_v AS SELECT * FROM x_dist;

-- direct query from distributed table
select a, sum(b) from x_dist group by a ORDER BY sum(b) DESC limit 1;
--Stats: 1 row in set. Elapsed: 5.816 sec. Processed 2.00 billion rows, 32.00 GB (343.89 million rows/s., 5.50 GB/s.)


-- query from view
select a, sum(b) from x_dist_v group by a ORDER BY sum(b) DESC limit 1;
-- Stats: 1 row in set. Elapsed: 17.093 sec. Processed 2.00 billion rows, 32.00 GB (117.01 million rows/s., 1.87 GB/s.)

The differernce is caused by different local queries.

In case of the trivial view it pushes down the innermost subquery (i.e. view):

... executeQuery: ... SELECT `x`.`a`, `x`.`b` FROM `default`.`x` (stage: Complete)

In case of direct query from the distributed table - it constructs the subquery from the main one:

...  executeQuery: ...  SELECT `a`, sum(`b`) FROM `default`.`x` GROUP BY `a` ORDER BY sum(`b`) DESC LIMIT 1 (stage: WithMergeableState)

Activity

  1. added
    unexpected behaviour
    comp-distributed
    comp-view
    on Jun 30, 2023
  2. filimonov commented on Jun 30, 2023

    @filimonov
    ContributorAuthor

    The view query works exactly the same as

    select a, sum(b) from (select * from x_dist) group by a ORDER BY sum(b) DESC limit 1
    
  3. emirkmo commented on Dec 18, 2023

    @emirkmo

    When you add aggregation and joins on top, we see a 30x reduction in performance for a simple two shard cluster setup.

    Any workarounds you could recommend? Could we use some combination of cluster macro and view function to somehow get a similar result as querying the distributed table directly?

    The views are dbt created syntactic sugar for further tables for data usage ergonomics so we control them but have not really thought of a good solution.

  4. afrancoisNexthink commented on May 31, 2024

    @afrancoisNexthink

    We were affected strongly by this and finally found a workaround.

    We used to have this structure:
    local mergetree table <- distributed table <- view with low cost data transformations, used for queries

    By changing to
    local mergetree table <- local view for data transformations <- distributed table for queries

    We can bypass the issue when we query the distributed table and get back to good performance, while still allowing us to have a flexible layer between what is stored in the mergetree tables and what is exposed for queries.

  5. victor-perov commented on Oct 11, 2024

    @victor-perov

    I'm wondering if the workaround is the way-to-go and this behaviour is expected. Could you, @alexey-milovidov, please shed some light on this issue, or please tag someone who can help understanding if this is going to be fixed?

  6. Jhors2 commented on Oct 17, 2025

    @Jhors2

    +1. I hit this very issue today, and unfortunately the workaround won't be usable due to my desire to use parametrized views and not just regular views. I suspect the issue here is that the pushdown doesn't happen in the call to the underlying nodes in the distributed table.

  7. simonmichal commented on Mar 28, 2026

    @simonmichal
    Contributor

    I would be happy to give it a try and work on this. I’ll try to put together a PR to start the discussion.

  8. simonmichal commented on Apr 4, 2026

    @simonmichal
    Contributor

    FYI: I have cut following PR: #101791

    Please let me know your thoughts!

  9. rienath commented on Aug 26, 2026

    @rienath
    Member

    Both are fast now, thanks @simonmichal!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

comp-distributedcomp-viewunexpected behaviour

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions