跳到主要内容
跳到主要内容

clusterAllReplicas 表函数

允许访问集群中所有分片(在 remote_servers 部分配置)而无需创建 Distributed 表。只查询每个分片的一个副本。

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.tabledbtable数据库和表的名称。
sharding_key分片键。可选。如果集群有多个分片,则需要指定该键。

返回值

来自集群的数据集。

使用宏

cluster_name 可以包含宏 — 用大括号包围的替换。替换值来自服务器配置文件的 macros 部分。

示例:

SELECT * FROM cluster('{cluster}', default.example_table);

使用和建议

使用 clusterclusterAllReplicas 表函数的效率低于创建 Distributed 表,因为这样每个请求都会重新建立服务器连接。在处理大量查询时,请始终提前创建 Distributed 表,并且不要使用 clusterclusterAllReplicas 表函数。

clusterclusterAllReplicas 表函数在以下情况下可能有用:

  • 访问特定集群进行数据比较、调试和测试。
  • 查询多个 ClickHouse 集群和副本以进行研究。
  • 不频繁的手动分布式请求。

连接设置如 hostportuserpasswordcompressionsecure 来自 <remote_servers> 配置部分。有关详细信息,请参见 Distributed engine