我的Clickhouse群集如下:
┌─cluster──────┬─shard_num─┬─shard_weight─┬─replica_num─┬─host_name─┬─host_address─┬─port─┬─is_local─┬─user─┬─default_database─┐
│ test_cluster │ 1 │ 1 │ 1 │ 127.0.0.1 │ 127.0.0.1 │ 9000 │ 1 │ test │ │
└──────────────┴───────────┴──────────────┴─────────────┴───────────┴──────────────┴──────┴──────────┴──────┴──────────────────┘
创建数据库和表:
create database test;
use test;
CREATE TABLE local_table_1 ( datatime UInt32, datadate Date, number1 UInt32) ENGINE = MergeTree(datadate, datatime, 8192);
CREATE TABLE local_table_2 ( datatime UInt32, datadate Date, number1 UInt32) ENGINE = MergeTree(datadate, datatime, 8192);
CREATE TABLE distributed_table_1 AS local_table_1 ENGINE = Distributed('test_cluster', 'test', local_table_1, rand());
CREATE TABLE distributed_table_2 AS local_table_2 ENGINE = Distributed('test_cluster', 'test', local_table_2, rand());
插入一些测试数据:
insert into local_table_1 (datatime,datadate,number1) values (1533091943,'2018-08-01',100);
insert into local_table_2 (datatime,datadate,number1) values (1533091943,'2018-08-01',200);
在那之后,如果我们使用分布式表查询,则结果不是我们期望的,并且本地表还可以!
select sum(number1) from merge('test','distributed_table_2|distributed_table_1');
SELECT sum(number1) FROM merge('test','distributed_table_2 | distributed_table_1')
┌─_table──────────────┬─sum(number1)─┐
│ distributed_table_1 │ 100 │
└─────────────────────┴──────────────┘
┌─_table──────────────┬─sum(number1)─┐
│ distributed_table_2 │ 200 │
└─────────────────────┴──────────────┘
select sum(number1) from merge('test','local_table_1|local_table_2');
SELECT sum(number1) FROM merge('test','local_table_1 | local_table_2')
┌─sum(number1)─┐
│ 300 │
└──────────────┘