cluster允许访问集群的所有分片,而无需创建分布式表。仅查询每个分片的一个副本。clusterAllReplicas函数 - 与cluster相同,但会查询所有副本。集群中的每个副本都用作单独的分片/连接。
注意
所有可用的集群都列在system.clusters表中。
语法
cluster(['cluster_name', db.table, sharding_key]) cluster(['cluster_name', db, table, sharding_key]) clusterAllReplicas(['cluster_name', db.table, sharding_key]) clusterAllReplicas(['cluster_name', db, table, sharding_key])
参数
cluster_name – 用于构建远程和本地服务器地址和连接参数集的集群名称,如果未指定,则设置为default。db.table 或 db、table - 数据库和表的名称。sharding_key - (可选)分片键。如果集群有多个分片,则需要指定。返回值
来自集群的数据集。
使用宏cluster_name可以包含宏 - 花括号中的替换。替换的值取自服务器配置文件的宏部分。
示例
SELECT * FROM cluster('{cluster}', default.example_table);
用法和建议
使用cluster和clusterAllReplicas表函数的效率低于创建Distributed表,因为在这种情况下,服务器连接会为每个请求重新建立。在处理大量查询时,请始终提前创建Distributed表,并且不要使用cluster和clusterAllReplicas表函数。cluster和clusterAllReplicas表函数在以下情况下可能很有用