You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
trivial view on top of distributed table can significantly impact the performance of some queries #51645
comp-distributedDistributed table engine & query routing across shards (sharding/load balancing).comp-viewLogical (non-materialized) views.unexpected behaviourResult is unexpected, but not entirely wrong at the same time.
createtablex engine = MergeTree ORDER BY tuple() ASSELECT intDiv(number,100000) as a, numberas b FROM numbers(1000000000);
SET prefer_localhost_replica =0;
CREATETABLEx_distAS x ENGINE = Distributed(test_cluster_two_shards_localhost, currentDatabase(), x);
CREATEVIEWx_dist_vASSELECT*FROM x_dist;
-- direct query from distributed tableselect a, sum(b) from x_dist group by a ORDER BYsum(b) DESClimit1;
--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 viewselect a, sum(b) from x_dist_v group by a ORDER BYsum(b) DESClimit1;
-- 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)
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.
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.
Reacted by filimonov, Murat Kabilov, Yudhiesh Ravindranath and Emir Karamehmetoglu
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?
Reacted by Murat Kabilov, Vitaly Orlov and Yudhiesh Ravindranath
+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.
comp-distributedDistributed table engine & query routing across shards (sharding/load balancing).comp-viewLogical (non-materialized) views.unexpected behaviourResult is unexpected, but not entirely wrong at the same time.
Activity
filimonov commentedon Jun 30, 2023
The view query works exactly the same as
emirkmo commentedon Dec 18, 2023
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.
afrancoisNexthink commentedon May 31, 2024
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.
victor-perov commentedon Oct 11, 2024
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?
Jhors2 commentedon Oct 17, 2025
+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.
simonmichal commentedon Mar 28, 2026
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.
simonmichal commentedon Apr 4, 2026
FYI: I have cut following PR: #101791
Please let me know your thoughts!
rienath commentedon Aug 26, 2026
Both are fast now, thanks @simonmichal!