> ## 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_and_compare_chain_* 会话设置

> ClickHouse 中由 optimize_and_compare_chain_* 生成的组的会话设置。

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_and_compare_chain">
  ## optimize\_and\_compare\_chain
</div>

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

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

在 AND 链中推导并补全与常量的比较条件，以增强过滤能力。支持 `<`、`<=`、`>`、`>=`、`=` 运算符及其混合使用。例如，`(a < b) AND (b < c) AND (c < 5)` 会变为 `(a < b) AND (b < c) AND (c < 5) AND (b < 5) AND (a < 5)`。

<div id="optimize_and_compare_chain_max_hash_work">
  ## optimize\_and\_compare\_chain\_max\_hash\_work
</div>

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

<VersionHistory rows={[{"id": "row-1","items": [{"label": "26.7"},{"label": "5000000"},{"label": "`optimize_and_compare_chain` 优化的新增设置，用于限制其工作量（以哈希的查询树节点数衡量），从而避免它在包含大量或超长比较 `AND` 链的查询中占用过多查询分析时间。此前的值 `0`（无限制）会复现 26.7 之前的行为，即该优化不设上限，因此将 `compatibility` 设为更早版本时，仍会在没有预算限制的情况下推导传递谓词。设为 `0` 可禁用此预算。"}]}]} />

在查询分析期间，`optimize_and_compare_chain` 优化的工作预算以 `getTreeHash` 哈希的查询树节点数来衡量 (这是该优化的主要开销) 。一旦某个查询在应用该优化时哈希的节点数超过此值，就会在该查询的剩余部分停止应用该优化。这可以限制包含大量或超长比较 `AND` 链的查询的分析时间；否则，该优化可能在没有折叠任何内容的情况下占用大部分分析时间。提前停止始终是安全的：它只会放弃一次优化机会，绝不会改变结果。设为 `0` 可禁用此预算 (无限制) 。
