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

# optimize_rewrite_* 会话设置

> ClickHouse 中 optimize_rewrite_* 分组下的会话设置。

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="optimize_rewrite_aggregate_function_with_if">
  ## optimize\_rewrite\_aggregate\_function\_with\_if
</div>

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

当逻辑上等价时，将以 if 表达式作为参数的聚合函数重写为等效形式。
例如，`avg(if(cond, col, null))` 可以重写为 `avgOrNullIf(cond, col)`。这可能会提升性能。

<Note>
  仅在启用 analyzer 时受支持 (`enable_analyzer = 1`) 。
</Note>

<div id="optimize_rewrite_array_exists_to_has">
  ## optimize\_rewrite\_array\_exists\_to\_has
</div>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "26.4"},{"label": "1"},{"label": "默认启用将 arrayExists 重写为 has 的优化，因为现在会在重写前检查类型兼容性。"}]}]} />

当逻辑上等价时，将 arrayExists() 函数重写为 has()。例如，arrayExists(x -> x = 1, arr) 可重写为 has(arr, 1)

<div id="optimize_rewrite_has_to_in">
  ## optimize\_rewrite\_has\_to\_in
</div>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "26.6"},{"label": "1"},{"label": "新增设置"}]}]} />

当第一个参数为常量数组时，将 `has` 函数重写为 `IN`。例如，`has([1, 2, 3], x)` 可重写为 `x IN [1, 2, 3]`，从而在处理常量数组时获得更好的性能

<div id="optimize_rewrite_like_perfect_affix">
  ## optimize\_rewrite\_like\_perfect\_affix
</div>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "25.10"},{"label": "1"},{"label": "新增设置"}]}]} />

将具有精确前缀或后缀的 LIKE 表达式 (例如 `col LIKE 'ClickHouse%'`) 重写为 startsWith 或 endsWith 函数 (例如 `startsWith(col, 'ClickHouse')`) 。

<div id="optimize_rewrite_regexp_functions">
  ## optimize\_rewrite\_regexp\_functions
</div>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "25.8"},{"label": "1"},{"label": "新增设置"}]}]} />

将与正则表达式相关的函数重写为更简单、更高效的形式

<div id="optimize_rewrite_sum_if_to_count_if">
  ## optimize\_rewrite\_sum\_if\_to\_count\_if
</div>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "24.4"},{"label": "1"},{"label": "仅 analyzer 可用，且仅在该组件中能正确工作"}]}]} />

当逻辑上等价时，将 sumIf() 和 sum(if()) 函数重写为 countIf() 函数
