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

# 容量规划与硬件建议

> 本指南介绍了我们面向开源用户在硬件、算力、内存和磁盘配置方面的一般建议。

本指南介绍了我们面向开源用户在硬件、算力、内存和磁盘配置方面的一般建议。如果您希望简化部署，我们建议使用 [ClickHouse Cloud](https://clickhouse.com/cloud)：它能够自动扩缩容，适配您的工作负载，同时尽可能降低基础设施管理成本。

您的 ClickHouse 集群配置在很大程度上取决于应用场景和工作负载模式。规划架构时，必须考虑以下因素：

* 并发度 (每秒请求数)
* 吞吐量 (每秒处理的行数)
* 数据量
* 数据保留策略
* 硬件成本
* 维护成本

<div id="disk">
  ## 磁盘
</div>

应为 ClickHouse 选用哪种磁盘类型，取决于数据量、延迟或吞吐量要求。

<div id="optimizing-for-performance">
  ### 性能优化
</div>

为最大限度提升性能，我们建议直接挂载 AWS 的[预置 IOPS SSD 卷](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/provisioned-iops.html)，或使用您的云提供商提供的同类产品，以优化 IO 性能。

<div id="optimizing-for-storage-costs">
  ### 优化存储成本
</div>

为了进一步降低成本，您可以使用[通用型 SSD EBS 卷](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/general-purpose.html)。

您还可以在[热/温/冷架构](/docs/zh/concepts/features/operations/delete/ttl#implementing-a-hotwarmcold-architecture)中结合 SSD 和 HDD 实现分层存储。或者，也可以使用 [AWS S3](https://aws.amazon.com/s3/) 作为存储后端，以实现计算与存储分离。有关在开源 ClickHouse 中实现计算与存储分离的指南，请参阅[此处](/docs/zh/guides/oss/deployment-and-scaling/separation-storage-compute)。ClickHouse Cloud 默认支持计算与存储分离。

<div id="cpu">
  ## CPU
</div>

<div id="which-cpu-should-i-use">
  ### 我应该使用哪种 CPU？
</div>

应使用哪种 CPU 取决于你的使用模式。不过一般来说，如果应用存在大量高频并发查询、需要处理更多数据，或者使用计算密集型 UDF，就需要更多 CPU 核心。

**低延迟或面向客户的应用**

对于延迟要求在数十毫秒级的工作负载 (例如面向客户的工作负载) ，我们建议使用 AWS 的 EC2 [i3 系列](https://aws.amazon.com/ec2/instance-types/i3/) 或 [i4i 系列](https://aws.amazon.com/ec2/instance-types/i4i/)，或你的云提供商提供的同类产品，这类产品针对 IO 进行了优化。

**高并发应用**

对于需要针对并发进行优化的工作负载 (每秒 100+ 查询) ，我们建议使用 AWS 的[计算优化型 C 系列](https://aws.amazon.com/ec2/instance-types/#Compute_Optimized)，或你的云提供商提供的同类产品。

**数据仓库使用场景**

对于数据仓库工作负载和临时分析查询，我们建议使用 AWS 的 [R 系列](https://aws.amazon.com/ec2/instance-types/#Memory_Optimized) 或你的云提供商提供的同类产品，因为它们针对内存进行了优化。

***

<div id="what-should-cpu-utilization-be">
  ### CPU 利用率应保持在多少？
</div>

ClickHouse 并没有统一的 CPU 利用率目标。请使用 [iostat](https://linux.die.net/man/1/iostat) 等工具测量平均 CPU 使用率，并据此调整服务器规模，以应对突发流量高峰。不过，对于包含即席查询的分析型或数据仓库场景，建议将 CPU 利用率控制在 10-20%。

<div id="how-many-cpu-cores-should-i-use">
  ### 我应该使用多少个 CPU 核心？
</div>

应使用多少 CPU 核心取决于你的工作负载。不过，我们通常建议根据 CPU 类型采用以下内存与 CPU 核心配比：

* **[M-type](https://aws.amazon.com/ec2/instance-types/) (通用场景) ：** 4 GB:1 的内存与 CPU 核心配比
* **[R-type](https://aws.amazon.com/ec2/instance-types/#Memory_Optimized) (数据仓库场景) ：** 8 GB:1 的内存与 CPU 核心配比
* **[C-type](https://aws.amazon.com/ec2/instance-types/#Compute_Optimized) (计算优化场景) ：** 2 GB:1 的内存与 CPU 核心配比

例如，使用 M-type CPU 时，我们建议每 25 个 CPU 核心配置 100GB 内存。要确定适合你的应用程序的内存容量，必须分析内存使用情况。你可以阅读[这篇关于调试内存问题的指南](/docs/zh/concepts/features/performance/troubleshoot/debugging-memory-issues)，或使用[内置的可观测性仪表板](/docs/zh/guides/oss/deployment-and-scaling/monitoring/monitoring)监控 ClickHouse。

<div id="memory">
  ## 内存
</div>

与 CPU 的选择类似，内存与存储比以及内存与 CPU 比的选择取决于您的使用场景。

所需的 RAM 容量通常取决于：

* 查询的复杂程度。
* 查询中处理的数据量。

不过，总体而言，内存越多，查询运行速度通常越快。
如果您的使用场景对价格较为敏感，较低的内存容量也可以满足需求，因为可以启用相关设置 ([`max_bytes_before_external_group_by`](/docs/zh/reference/settings/session-settings#max_bytes_before_external_group_by) 和 [`max_bytes_before_external_sort`](/docs/zh/reference/settings/session-settings#max_bytes_before_external_sort)) ，允许将数据落盘到磁盘；但请注意，这可能会显著影响查询性能。

<div id="what-should-the-memory-to-storage-ratio-be">
  ### 内存与存储比例应为多少？
</div>

对于数据量较小的场景，1:1 的内存与存储比例是可以接受的，但总内存不应低于 8GB。

对于数据保留周期较长或数据量较大的场景，我们建议采用 1:100 到 1:130 的内存与存储比例。例如，如果要存储 10TB 数据，则每个副本建议配置 100GB RAM。

对于需要频繁访问的场景，例如面向客户的工作负载，我们建议增加内存，采用 1:30 到 1:50 的内存与存储比例。

<div id="replicas">
  ## 副本
</div>

我们建议每个分片至少配置三个副本 (如果使用 [亚马逊 EBS](https://aws.amazon.com/ebs/) ，则可配置两个副本) 。此外，我们建议在添加更多副本 (横向扩缩容) 之前，先对所有副本进行纵向扩容。

ClickHouse 不会自动进行分片，而对数据集重新分片需要消耗大量计算资源。因此，我们通常建议使用可用的最大规格服务器，以避免将来不得不重新分片数据。

你也可以考虑使用 [ClickHouse Cloud](https://clickhouse.com/cloud)，它支持自动扩缩容，并允许你根据自身使用场景轻松控制副本数量。

<div id="example-configurations-for-large-workloads">
  ## 大型工作负载示例配置
</div>

ClickHouse 的配置高度依赖具体应用的需求。如果您希望我们帮助优化架构，在成本与性能之间取得更好平衡，请[联系销售](https://clickhouse.com/company/contact?loc=docs-sizing-and-hardware-recommendations)。

以下列出的是生产环境中 ClickHouse 用户的示例配置，旨在提供参考而非建议：

<div id="fortune-500-b2b-saas">
  ### 《财富》500 强 B2B SaaS
</div>

<table>
  <tr>
    <td col="2"><strong><em>存储</em></strong></td>
  </tr>

  <tr>
    <td><strong>每月新增数据量</strong></td>
    <td>30TB</td>
  </tr>

  <tr>
    <td><strong>总存储 (压缩后) </strong></td>
    <td>540TB</td>
  </tr>

  <tr>
    <td><strong>数据保留期</strong></td>
    <td>18 个月</td>
  </tr>

  <tr>
    <td><strong>每个节点磁盘容量</strong></td>
    <td>25TB</td>
  </tr>

  <tr>
    <td col="2"><strong><em>CPU</em></strong></td>
  </tr>

  <tr>
    <td><strong>并发度</strong></td>
    <td>200+ 并发查询</td>
  </tr>

  <tr>
    <td><strong>副本数 (包括高可用对) </strong></td>
    <td>44</td>
  </tr>

  <tr>
    <td><strong>每个节点 vCPU</strong></td>
    <td>62</td>
  </tr>

  <tr>
    <td><strong>总 vCPU</strong></td>
    <td>2700</td>
  </tr>

  <tr>
    <td col="2"><strong><em>内存</em></strong></td>
  </tr>

  <tr>
    <td><strong>总 RAM</strong></td>
    <td>11TB</td>
  </tr>

  <tr>
    <td><strong>每个副本的 RAM</strong></td>
    <td>256GB</td>
  </tr>

  <tr>
    <td><strong>RAM 与 vCPU 比例</strong></td>
    <td>4 GB:1</td>
  </tr>

  <tr>
    <td><strong>RAM 与磁盘比例</strong></td>
    <td>1:50</td>
  </tr>
</table>

<div id="fortune-500-telecom-operator-for-a-logging-use-case">
  ### 《财富》500 强电信运营商日志场景
</div>

<table>
  <tr>
    <td col="2"><strong><em>存储</em></strong></td>
  </tr>

  <tr>
    <td><strong>每月日志数据量</strong></td>
    <td>4860TB</td>
  </tr>

  <tr>
    <td><strong>总存储 (压缩后) </strong></td>
    <td>608TB</td>
  </tr>

  <tr>
    <td><strong>数据保留期</strong></td>
    <td>30 天</td>
  </tr>

  <tr>
    <td><strong>每个节点的磁盘容量</strong></td>
    <td>13TB</td>
  </tr>

  <tr>
    <td col="2"><strong><em>CPU</em></strong></td>
  </tr>

  <tr>
    <td><strong>副本数 (含 高可用对) </strong></td>
    <td>38</td>
  </tr>

  <tr>
    <td><strong>每个节点 vCPU</strong></td>
    <td>42</td>
  </tr>

  <tr>
    <td><strong>总 vCPU</strong></td>
    <td>1600</td>
  </tr>

  <tr>
    <td col="2"><strong><em>内存</em></strong></td>
  </tr>

  <tr>
    <td><strong>总 RAM</strong></td>
    <td>10TB</td>
  </tr>

  <tr>
    <td><strong>每个副本的 RAM</strong></td>
    <td>256GB</td>
  </tr>

  <tr>
    <td><strong>RAM 与 vCPU 比例</strong></td>
    <td>6 GB:1</td>
  </tr>

  <tr>
    <td><strong>RAM 与磁盘比例</strong></td>
    <td>1:60</td>
  </tr>
</table>

<div id="further-reading">
  ## 延伸阅读
</div>

以下是一些使用开源 ClickHouse 的公司发布的架构博客文章：

* [Cloudflare](https://blog.cloudflare.com/http-analytics-for-6m-requests-per-second-using-clickhouse/?utm_source=linkedin\&utm_medium=social\&utm_campaign=blog)
* [eBay](https://innovation.ebayinc.com/tech/engineering/ou-online-analytical-processing/)
* [GitLab](https://handbook.gitlab.com/handbook/engineering/architecture/design-documents/clickhouse_usage/)
* [Lyft](https://eng.lyft.com/druid-deprecation-and-clickhouse-adoption-at-lyft-120af37651fd)
* [MessageBird](https://clickhouse.com/blog/how-messagebird-uses-clickhouse-to-monitor-the-delivery-of-billions-of-messages)
* [Microsoft](https://clickhouse.com/blog/self-service-data-analytics-for-microsofts-biggest-web-properties)
* [Uber](https://www.uber.com/en-ES/blog/logging/)
* [Zomato](https://blog.zomato.com/building-a-cost-effective-logging-platform-using-clickhouse-for-petabyte-scale)
