> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# load_balancing_* 会话设置

> ClickHouse 在 load_balancing_* 自动生成分组中的会话设置。

export const SettingsInfoBlock = ({type, default_value, changeable_without_restart}) => {
  return <div className="not-prose" style={{
    display: "flex",
    flexWrap: "wrap",
    alignItems: "baseline",
    columnGap: "0.5rem",
    rowGap: "0.125rem",
    margin: "0.375rem 0",
    fontSize: "0.8125rem",
    lineHeight: "1.125rem"
  }}>
      <div style={{
    fontWeight: 600,
    opacity: 0.72
  }}>类型</div>
      <div style={{
    overflowWrap: "anywhere"
  }}>{type}</div>
      <div style={{
    fontWeight: 600,
    opacity: 0.72,
    marginInlineStart: "0.5rem"
  }}>默认值</div>
      <div style={{
    overflowWrap: "anywhere"
  }}>{default_value}</div>
      {changeable_without_restart && <div style={{
    fontWeight: 600,
    opacity: 0.72,
    marginInlineStart: "0.5rem"
  }}>
          无需重启即可更改
        </div>}
      {changeable_without_restart && <div style={{
    overflowWrap: "anywhere"
  }}>
          {changeable_without_restart}
        </div>}
    </div>;
};

这些设置可在 [system.settings](/docs/zh/reference/system-tables/settings) 中查看，并根据 [源码](https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/Settings.cpp) 自动生成。

<div id="load_balancing">
  ## load\_balancing
</div>

<SettingsInfoBlock type="LoadBalancing" default_value="random" />

指定在分布式查询处理中使用的副本选择算法。

ClickHouse 支持以下副本选择算法：

* [Random](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-random) (默认)
* [最近的主机名](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-nearest_hostname)
* [主机名 Levenshtein 距离](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-hostname_levenshtein_distance)
* [主机名最长公共前缀](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-hostname_longest_common_prefix)
* [主机名最长公共后缀](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-hostname_longest_common_suffix)
* [按顺序](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-in_order)
* [第一个或随机](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-first_or_random)
* [轮询](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing-round_robin)

另请参阅：

* [distributed\_replica\_max\_ignored\_errors](/docs/zh/reference/settings/session-settings/distributed-replica#distributed_replica_max_ignored_errors)

<div id="load_balancing-random">
  ### Random (默认)
</div>

```sql theme={null}
load_balancing = random
```

会统计每个副本的错误次数。查询会发送到错误最少的副本；如果有多个副本都满足这一条件，则会发送到其中任意一个。
缺点：不考虑服务器的距离；如果各副本中的数据不同，得到的数据也会不同。

<div id="load_balancing-nearest_hostname">
  ### 最近的主机名
</div>

```sql theme={null}
load_balancing = nearest_hostname
```

每个副本都会统计错误数。每 5 分钟，错误数都会整除 2。这样一来，近期一段时间内的错误数就会以指数平滑平均值的方式计算出来。如果某个副本的错误数最少 (即其他副本最近出现过错误) ，则查询会发送到该副本。如果有多个副本的最少错误数相同，则查询会发送到这样一个副本：其主机名与配置文件中服务器的主机名最为相似 (依据是两个主机名在相同位置上不同字符的数量，比较长度不超过两者中的较小值) 。

例如，example01-01-1 和 example01-01-2 只有 1 个位置不同，而 example01-01-1 和 example01-02-2 则有 2 个位置不同。
这种方法看起来可能比较简单，但它不需要关于网络拓扑的外部数据，也不比较 IP 地址；而对于我们的 IPv6 地址来说，比较 IP 地址会很复杂。

因此，如果存在等效的副本，则会优先选择名称最接近的那个。
我们还可以认为，在没有发生故障的情况下，如果将查询发送到同一台服务器，那么分布式查询也会发送到相同的服务器。因此，即使不同的数据存放在各个副本上，查询返回的结果通常也会基本相同。

<div id="load_balancing-hostname_levenshtein_distance">
  ### 主机名的 Levenshtein 距离
</div>

```sql theme={null}
load_balancing = hostname_levenshtein_distance
```

与 `nearest_hostname` 类似，但它会根据 [Levenshtein 距离](https://en.wikipedia.org/wiki/Levenshtein_distance) 来比较主机名。例如：

```text theme={null}
example-clickhouse-0-0 ample-clickhouse-0-0
1

example-clickhouse-0-0 example-clickhouse-1-10
2

example-clickhouse-0-0 example-clickhouse-12-0
3
```

<div id="load_balancing-hostname_longest_common_prefix">
  ### 主机名最长公共前缀
</div>

```sql theme={null}
load_balancing = hostname_longest_common_prefix
```

与 `nearest_hostname` 类似，但会优先选择主机名与本地主机名具有最长公共前缀的副本 (公共前缀越长，优先级越高) 。不同于 `nearest_hostname` 按字符位置逐一统计差异，这种策略不会因主机名中数字分段的长度不同而受到干扰。例如，对于本地主机名 `sfe301`：

```text theme={null}
sfe301 sde301
1

sfe301 sfe10101
3

sfe301 sde505
1
```

这里会优先选择 `sfe10101`，因为它与 `sfe301` 具有最长的公共前缀 (`sfe`，长度为 3) 。

公共前缀长度相同的副本会被随机选中。特别是，当没有任何副本与本地主机名共享前缀时 (即所有公共前缀长度都为 0) ，此策略的行为与 `random` 完全相同。

<div id="load_balancing-hostname_longest_common_suffix">
  ### 主机名最长公共后缀
</div>

```sql theme={null}
load_balancing = hostname_longest_common_suffix
```

与 `hostname_longest_common_prefix` 类似，但这里比较的是最长公共 *后缀*，而不是前缀。当数据中心标识被编码到主机名的后缀中时，这一点会很有用。例如，对于本地主机名 `et46gtghn.qc.localdomain`：

```text theme={null}
et46gtghn.qc.localdomain tr676ddgh.td.localdomain
12

et46gtghn.qc.localdomain ab999.qc.localdomain
15
```

这里会优先选择 `ab999.qc.localdomain`，因为它与 `et46gtghn.qc.localdomain` 共享最长的公共后缀 (`.qc.localdomain`，长度为 15) 。

如果副本的公共后缀长度相同，则会随机选择。特别是，当没有任何副本与本地主机名共享任何后缀时 (即所有公共后缀长度都为 0) ，此策略的行为与 `random` 完全一致。

<div id="load_balancing-in_order">
  ### 按顺序
</div>

```sql theme={null}
load_balancing = in_order
```

错误数量相同的副本将按照它们在配置中声明的顺序依次访问。
当你明确知道哪个副本更优先时，这种方法比较合适。

<div id="load_balancing-first_or_random">
  ### 第一个或随机
</div>

```sql theme={null}
load_balancing = first_or_random
```

该算法会选择集合中的第一个副本；如果第一个不可用，则随机选择一个副本。它在交叉复制拓扑中效果很好，但在其他配置中则没有什么作用。

`first_or_random` 算法解决了 `in_order` 算法存在的问题。使用 `in_order` 时，如果某个副本宕机，下一个副本会承受双倍负载，而其余副本仍处理平常的流量。使用 `first_or_random` 算法时，负载会在仍然可用的副本之间均匀分配。

可以通过设置 `load_balancing_first_offset` 来显式指定哪个副本是第一个副本。这样可以更好地控制在各副本之间重新平衡查询工作负载。

<div id="load_balancing-round_robin">
  ### 轮询
</div>

```sql theme={null}
load_balancing = round_robin
```

该算法会在错误数相同的副本之间采用轮询策略 (仅将采用 `round_robin` 策略的查询计入统计) 。

<div id="load_balancing_first_offset">
  ## load\_balancing\_first\_offset
</div>

<SettingsInfoBlock type="UInt64" default_value="0" />

在使用 FIRST\_OR\_RANDOM 负载均衡策略时，优先将查询发送到哪个副本。
