> ## 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.

# prefer_* 会话设置

> ClickHouse 的 prefer_* 分组会话设置。

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>;
};

export const CloudOnlyBadge = () => {
  return <div className="cloudBadge">
            <div className="cloudIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path fillRule="evenodd" clipRule="evenodd" d="M5.33395 12.6667H12.3739C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00004 12.3739 8.00004H12.0839V7.33337C12.0839 5.12671 10.2906 3.33337 8.08395 3.33337C6.09928 3.33337 4.45395 4.78537 4.14195 6.68204C2.55728 6.76271 1.29395 8.06204 1.29395 9.66671C1.29395 11.3234 2.63728 12.6667 4.29395 12.6667H5.33395Z" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
            </svg>
        </div>
            {'仅限 ClickHouse Cloud'}
        </div>;
};

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

<div id="prefer_column_name_to_alias">
  ## prefer\_column\_name\_to\_alias
</div>

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

启用或禁用在查询表达式和子句中使用原始列名而非别名。在别名与列名相同时，这一点尤为重要，参见 [Expression Aliases](/docs/zh/reference/syntax#notes-on-usage)。启用此设置可使 ClickHouse 中的别名语法规则与大多数其他数据库引擎更兼容。

可能的值：

* 0 — 列名会被替换为别名。
* 1 — 列名不会被替换为别名。

**示例**

启用与禁用时的区别：

查询：

```sql theme={null}
SET prefer_column_name_to_alias = 0;
SELECT avg(number) AS number, max(number) FROM numbers(10);
```

结果：

```text theme={null}
Received exception from server (version 21.5.1):
Code: 184. DB::Exception: Received from localhost:9000. DB::Exception: Aggregate function avg(number) is found inside another aggregate function in query: While processing avg(number) AS number.
```

查询：

```sql theme={null}
SET prefer_column_name_to_alias = 1;
SELECT avg(number) AS number, max(number) FROM numbers(10);
```

结果：

```text theme={null}
┌─number─┬─max(number)─┐
│    4.5 │           9 │
└────────┴─────────────┘
```

<div id="prefer_external_sort_block_bytes">
  ## prefer\_external\_sort\_block\_bytes
</div>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "24.5"},{"label": "16744704"},{"label": "外部排序时优先使用最大块字节数，以减少合并期间的内存使用量。"}]}]} />

外部排序时优先使用最大块字节数，以减少合并期间的内存使用量。

<div id="prefer_global_in_and_join">
  ## prefer\_global\_in\_and\_join
</div>

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

启用将 `IN`/`JOIN` 运算符替换为 `GLOBAL IN`/`GLOBAL JOIN`。

可能的值：

* 0 — 已禁用。`IN`/`JOIN` 运算符不会被替换为 `GLOBAL IN`/`GLOBAL JOIN`。
* 1 — 已启用。`IN`/`JOIN` 运算符会被替换为 `GLOBAL IN`/`GLOBAL JOIN`。

**用法**

虽然 `SET distributed_product_mode=global` 可以改变分布式表的查询行为，但它不适用于本地表或来自外部资源的表。这正是 `prefer_global_in_and_join` 设置发挥作用的时候。

例如，我们有一些提供查询服务的节点，其中包含本地表，而这些表不适合做分布式处理。我们需要在分布式处理中使用 `GLOBAL` 关键字——`GLOBAL IN`/`GLOBAL JOIN`——动态地将这些数据分发出去。

`prefer_global_in_and_join` 的另一个用例是访问由 external engine 创建的表。此设置有助于在连接这类表时减少对外部数据源的调用次数：每个查询只调用一次。

**另请参阅：**

* [分布式子查询](/docs/zh/reference/statements/in#distributed-subqueries)，了解有关如何使用 `GLOBAL IN`/`GLOBAL JOIN` 的更多信息

<div id="prefer_localhost_replica">
  ## prefer\_localhost\_replica
</div>

<SettingsInfoBlock type="Bool" default_value="1" />

启用/禁用在处理分布式查询时优先使用 localhost 副本。

可能的值：

* 1 — 如果存在 localhost 副本，ClickHouse 始终将查询发送到该副本。
* 0 — ClickHouse 使用 [load\_balancing](/docs/zh/reference/settings/session-settings/load-balancing#load_balancing) 设置指定的负载均衡策略。

<Note>
  如果你使用 [max\_parallel\_replicas](/docs/zh/reference/settings/session-settings/max#max_parallel_replicas) 而未使用 [parallel\_replicas\_custom\_key](/docs/zh/reference/settings/session-settings/parallel-replicas#parallel_replicas_custom_key)，请禁用此设置。
  如果设置了 [parallel\_replicas\_custom\_key](/docs/zh/reference/settings/session-settings/parallel-replicas#parallel_replicas_custom_key)，则仅当它用于包含多个 сегмент 且每个 сегмент 都包含多个副本的集群时，才应禁用此设置。
  如果它用于只有单个 сегмент 但包含多个副本的集群，禁用此设置会带来负面影响。
</Note>

<div id="prefer_warmed_unmerged_parts_seconds">
  ## prefer\_warmed\_unmerged\_parts\_seconds
</div>

<CloudOnlyBadge />

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

仅在 ClickHouse Cloud 中生效。如果某个 merged 分片 距合并完成还不到这么多秒，且尚未预热 (参见 [cache\_populated\_by\_fetch](/docs/zh/reference/settings/merge-tree-settings#cache_populated_by_fetch)) ，但它的所有 source 分片 都可用且已预热，则 SELECT 查询会改为从这些 分片 读取。仅适用于 Replicated-/SharedMergeTree。请注意，这里只检查 CacheWarmer 是否处理过该分片；如果该分片是通过其他方式被拉取到缓存中的，那么在 CacheWarmer 处理到它之前，仍会被视为冷分片；如果它曾被预热，之后又从缓存中被驱逐，仍会被视为热分片。
