跳转到主要内容
允许在不创建 Distributed 表的情况下访问集群的所有分片 (这些分片在 remote_servers 部分中配置) 。每个分片只会查询一个副本。 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 or db, table数据库和表的名称。
sharding_key分片键。可选。如果集群包含多个分片,则需要指定。

返回值

来自集群的数据。

使用宏

cluster_name 可以包含宏,即 {} 中的替换内容。替换后的值取自服务器配置文件中的 macros 部分。 示例:
SELECT * FROM cluster('{cluster}', default.example_table);

使用与建议

与创建 Distributed 表相比,使用 clusterclusterAllReplicas 表函数的效率较低,因为在这种情况下,每次请求都会重新建立服务器连接。处理大量查询时,请务必预先创建 Distributed 表,而不要使用 clusterclusterAllReplicas 表函数。 在以下情况下,clusterclusterAllReplicas 表函数会比较有用:
  • 访问特定集群以进行数据对比、调试和测试。
  • 出于研究目的,查询不同的 ClickHouse 集群和副本。
  • 手动发起的不频繁分布式请求。
hostportuserpasswordcompressionsecure 等连接设置取自 <remote_servers> 配置部分。详见 Distributed 引擎
最后修改于 2026年6月10日