> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# prefer_* 会话设置

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

export const CloudOnlyBadge = ({supported = ['cloud', 'private', 'BYOC']}) => {
  const platforms = supported.map(platform => ({
    cloud: 'ClickHouse Cloud',
    private: 'ClickHouse Private',
    BYOC: 'BYOC'
  })[platform] || platform);
  const label = platforms.length === 1 ? platforms[0] : platforms.length === 2 ? platforms.join(' and ') : platforms.slice(0, -1).join(', ') + ', and ' + platforms[platforms.length - 1];
  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>
            {'Available in ' + label}
        </div>;
};

export const VersionHistory = ({rows = []}) => {
  if (rows.length === 0) {
    return null;
  }
  const headers = ["版本", "默认值", "注释"];
  const border = "1px solid rgba(128, 128, 128, 0.3)";
  const cell = {
    border,
    padding: "0.25rem 0.5rem",
    textAlign: "start",
    verticalAlign: "top"
  };
  return <details className="not-prose" style={{
    border,
    borderRadius: "0.5rem",
    margin: "0.5rem 0",
    padding: "0.5rem 0.75rem",
    fontSize: "0.8125rem",
    lineHeight: "1.125rem"
  }}>
      <summary style={{
    cursor: "pointer",
    fontWeight: 600,
    opacity: 0.72
  }}>
        版本历史
      </summary>
      <table style={{
    borderCollapse: "collapse",
    width: "100%",
    margin: "0.5rem 0 0"
  }}>
        <thead>
          <tr>
            {headers.map(header => <th key={header} style={{
    ...cell,
    fontWeight: 600,
    opacity: 0.72
  }}>
                {header}
              </th>)}
          </tr>
        </thead>
        <tbody>
          {rows.map((row, row_index) => <tr key={row.id ?? row_index}>
              {(row.items ?? []).map((item, item_index) => <td key={item_index} style={{
    ...cell,
    overflowWrap: "anywhere"
  }}>
                  {item?.label}
                </td>)}
            </tr>)}
        </tbody>
      </table>
    </details>;
};

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](/zh/reference/system-tables/settings) 中找到，并由 [源代码](https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/Settings.cpp) 自动生成。

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

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

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

可能的值：

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

如果列名在被连接的表之间存在歧义，且存在同名的别名，则使用该别名：

```sql theme={null}
SET prefer_column_name_to_alias = 1;
SELECT t1.id + 10 AS id, id AS x
FROM (SELECT 1 AS id) AS t1, (SELECT 1 AS k) AS t2, (SELECT 2 AS id) AS t3;
```

```text theme={null}
┌─id─┬──x─┐
│ 11 │ 11 │
└────┴────┘
```

这里 `id AS x` 中的 `id` 同时是 `t1` 和 `t3` 的列，因此它会解析为别名 `t1.id + 10`。

**示例**

启用与禁用时的区别：

查询：

```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 │
└────────┴─────────────┘
```

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

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

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

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

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

<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 创建的表。此设置有助于在连接这类表时减少对外部数据源的调用次数：每个查询只调用一次。

**另请参阅：**

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

<h2 id="prefer_localhost_replica">
  prefer\_localhost\_replica
</h2>

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

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

可能的值：

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

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

<h2 id="prefer_optimize_projection">
  prefer\_optimize\_projection
</h2>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "26.10"},{"label": "0"},{"label": "新增设置：与 `force_optimize_projection` 类似，无论预估代价如何都选用可用的 projection，但在未使用任何 projection 时不会导致查询失败。"}]}]} />

在启用 projection 优化的情况下 (参见 [optimize\_use\_projections](/zh/reference/settings/session-settings/optimize-use#optimize_use_projections) 设置) ，让 projection 优化在 `SELECT` 查询中优先使用 [projections](/zh/reference/engines/table-engines/mergetree-family/mergetree#projections) 而非表本身。

启用后，只要某个 projection 能够满足该查询，即使读取的 marks 比表本身更多，也会被使用；这一点与 [force\_optimize\_projection](/zh/reference/settings/session-settings/force-optimize#force_optimize_projection) 相同，但在无可用 projection 时查询不会失败。

可能的取值：

* 0 — 按预估代价选择 projection。
* 1 — 无论预估代价如何，都选择可用的 projection。

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

<CloudOnlyBadge />

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

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