> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 关于 Profile-Guided Optimization 的文档

# Profile-Guided Optimization

Profile-Guided Optimization (PGO) 是一种编译器优化技术，即根据程序的运行时 profile 对其进行优化。

根据测试，PGO 有助于提升 ClickHouse 的性能。在 ClickBench 测试套件中，我们观察到 QPS 最高可提升 15%。更详细的结果可在[此处](https://pastebin.com/xbue3HMU)查看。性能收益取决于你的典型 工作负载，因此实际效果可能更好，也可能更差。

有关 ClickHouse 中 PGO 的更多信息，请参阅对应的 GitHub [issue](https://github.com/ClickHouse/ClickHouse/issues/44567)。

<div id="how-to-build-clickhouse-with-pgo">
  ## 如何使用 PGO 构建 ClickHouse？
</div>

PGO 主要分为两大类：[插桩式](https://clang.llvm.org/docs/UsersManual.html#using-sampling-profilers) 和 [采样式](https://clang.llvm.org/docs/UsersManual.html#using-sampling-profilers) (也称为 AutoFDO) 。本指南介绍的是 ClickHouse 的插桩式 PGO。

1. 以插桩模式构建 ClickHouse。在 Clang 中，可以通过向 `CXXFLAGS` 传递 `-fprofile-generate` 选项来实现。
2. 在样本工作负载上运行插桩版 ClickHouse。这里需要使用你平时的工作负载。一种做法是使用 [ClickBench](https://github.com/ClickHouse/ClickBench) 作为样本工作负载。插桩模式下的 ClickHouse 可能运行较慢，因此请提前做好准备，并且不要在对性能敏感的环境中运行插桩版 ClickHouse。
3. 使用 `-fprofile-use` 编译器标志以及上一步收集到的 profile，再次编译 ClickHouse。

有关如何应用 PGO 的更详细说明，请参阅 Clang [文档](https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization)。

如果你打算直接从生产环境收集样本工作负载，我们建议尝试使用采样式 PGO。
