Repository navigation
Closed
Labels
comp-distributedDistributed table engine & query routing across shards (sharding/load balancing).comp-joinsJOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla...feature
Description
I experience troubles in joining data locally at every shard.
Setup
I have cluster with 3 shards
<ha1>
<shard>
<weight>1</weight>
<internal_replication>true</internal_replication>
<replica>
<host>localhost</host>
<port>9000</port>
<default_database>db0</default_database>
</replica>
</shard>
<shard>
<weight>1</weight>
<internal_replication>true</internal_replication>
<replica>
<host>localhost</host>
<port>9001</port>
<default_database>db0</default_database>
</replica>
</shard>
<shard>
<weight>1</weight>
<internal_replication>true</internal_replication>
<replica>
<host>localhost</host>
<port>9002</port>
<default_database>db0</default_database>
</replica>
</shard>
</ha1>
and next tables
CREATE DATABASE db0 ENGINE = Ordinary
CREATE TABLE db0.a1_replicated (
day Date,
id UInt32
)
ENGINE = MergeTree() PARTITION BY day ORDER BY id
CREATE TABLE a1 (
day Date,
id UInt32
)
ENGINE = Distributed('ha1', 'db0', a1_replicated, id)
CREATE TABLE db0.b1_replicated (
day Date,
id UInt32
)
ENGINE = MergeTree() PARTITION BY day ORDER BY id
CREATE TABLE b1 (
day Date,
id UInt32
)
ENGINE = Distributed('ha1', 'db0', b1_replicated, id)
filled with data
INSERT INTO a1 (day, id) VALUES
('2019-01-01', 9),
('2019-01-01', 10), ('2019-01-01', 13),
('2019-01-01', 11), ('2019-01-01', 14), ('2019-01-01', 17)
INSERT INTO b1 (day, id) VALUES
('2019-01-01', 9),
('2019-01-01', 10), ('2019-01-01', 13),
('2019-01-01', 11), ('2019-01-01', 14), ('2019-01-01', 17)
Expected behavior
Because of id being sharding key for both tables, I expect the query
SELECT id, count()
FROM a1 AS a1
LEFT JOIN b1 b1 ON a1.id = b1.id
GROUP BY id
to be
- sent from initiator to all of the shards
- computed at every shard
- results from all shards combined at initiator
Actual behavior
I tried different approaches.
Query 1
SELECT id, count()
FROM a1 AS a1
LEFT JOIN b1 AS b1 ON a1.id = b1.id
GROUP BY id
The error is DB::Exception: Table db0.b1 doesn't exist..
Query 2
SELECT id, count()
FROM a1 a1
LEFT JOIN (SELECT id FROM b1 b1) b1 ON a1.id = b1.id
GROUP BY id
The error is DB::Exception: Table default.b1_replicated doesn't exist
Query 3
SELECT id, count()
FROM (SELECT id FROM a1) a1
LEFT JOIN (SELECT id FROM b1) b1 ON a1.id = b1.id
GROUP BY id
Initial query is not sent to all of the shards. Instead, both subqueries are sent to all of the shards and the results of subqueries are then joined at initiator (that may not be efficient).
Questions
- How to explain different behaviors of Query 1, Query 2 and Query 3?
- How to JOIN data locally at every shard using this topology and schema?
Reproduce
The version of Clickhouse is latest 19.17.6.
The only non-default setting was <distributed_product_mode>local</distributed_product_mode>
Metadata
Metadata
Assignees
Labels
comp-distributedDistributed table engine & query routing across shards (sharding/load balancing).comp-joinsJOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla...feature
Activity
den-crane commentedon Jan 6, 2020
Your expectations are wrong.
CH does not create tables at other nodes automatically. All CH nodes are independent and are not aware about cluster. You need to execute all 4
create tableat all 3 nodes manually. Or useon clusterhelper https://clickhouse.yandex/docs/en/query_language/create/#create-table-query but it requires Zookeeper.CH does not trust to a Distributed table sharding key. Because you can insert data to b1_replicated locally or you can change sharding key in Distributed table.
So you should do a proper join manually.
options:
mrtarsa commentedon Jan 6, 2020
Yes, I executed CREATE TABLE statements at all 3 nodes. Sorry, forgot to mention that.
And distributed_product_mode was set to local on config level at every node.
den-crane commentedon Jan 6, 2020
probably you've missed a database qualifier
db0and created Distributed tables in adefaultdatabase.check
desc db0.a1vsdesc default.a1mrtarsa commentedon Jan 7, 2020
In my case tables
a1andb1are Distributed indefaultdatabase. That was intended and I cannot move the tables into another database.I simplified it and just for example explained the problem with single replica for every shard. In such case topology is like
where there is only one replica for every shard in database
db0.In real case topology is cross replicated and may be like
where
s2r1is second replica of third shard.Here all replicas
r0are located in databasedb0. All replicasr1are located in databasedb1.Consider query
SELECT * FROM a1.For every shard from the cluster
h1, where Distributed tablea1is defined, the replica will be selected. Imagine that selected replicas ares0r0,s1r0ands2r1, that are located in databasesdb0,db0anddb1. Since databases may be differentI expect that
default_databasefrom config will be used for every selected replica.I tried next queries based on your suggestions
The error is
DB::Exception: Table default.b1_replicated doesn't exist..The error is
DB::Exception: Table default.b1_replicated doesn't exist..In my case the only way to execute the query with JOIN without error is
But with such approach, instead of sending the whole query to every shard, initiator sends only the subqueries
SELECT * FROM a1andSELECT * FROM b1. Results are then collected from every shard, joined and grouped by at initiator node. This is not efficient, since GROUP BY may reduce the result set significantly. That's why I find a way to perform the whole query at every shard and then just combine the results at initiator. I wonder whether Distributed engine is capable of that, or I should take the cluster topology into account on application level and execute multiple queries in parallel from there.4ertus2 commentedon May 13, 2020
Probably we need rethink Distributed logic with different databases.
Related #10471 #6571
4ertus2 commentedon May 13, 2020
As workaround you may try to make views in same db that look at original tables and make Distributed table over them. It's not well defined zone when you combine tables from different databases in one query. Something could go wrong.
[-]Join distributed tables with same sharding key[/-][+]Distributed Join over tables in different databases with distributed_product_mode=local[/+]4ertus2 commentedon May 13, 2020
Your main problem is most probably cause of #6571
Close: #8547