ClickHouse冷热分层与S3存储
ClickHouse是如何实现冷热数据分层的?请说明TTL-based分层策略、磁盘类型配置(Local SSD + S3)、以及数据在冷热层之间的自动迁移机制。如何将S3作为ClickHouse的冷存并保证查询性能?
回答
我还是少年
1. 冷热分层架构
ClickHouse通过 Storage Policy(存储策略) 实现冷热分层:
<!-- config.xml -->
<storage_configuration>
<disks>
<hot>
<type>local</type>
<path>/data/hot/</path>
</hot>
<cold>
<type>s3</type>
<endpoint>https://s3.amazonaws.com/bucket/</endpoint>
<access_key_id>...</access_key_id>
<secret_access_key>...</secret_access_key>
</cold>
</disks>
<policies>
<tiered>
<volumes>
<hot_volume>
<disk>hot</disk>
<max_data_part_size>1000000000</max_data_part_size>
</hot_volume>
<cold_volume>
<disk>cold</disk>
</cold_volume>
</volumes>
</tiered>
</policies>
</storage_configuration>
2. TTL自动迁移
-- 表创建时指定TTL
CREATE TABLE logs (
ts DateTime,
message String,
level String
) ENGINE = MergeTree()
ORDER BY ts
TTL ts + INTERVAL 30 DAY TO VOLUME 'cold_volume',
ts + INTERVAL 90 DAY DELETE
SETTINGS storage_policy = 'tiered';
迁移规则:
ts + 30d→ 自动从Local SSD迁移到S3ts + 90d→ 删除过期数据- 基于Part粒度迁移,合并时自动触发
3. S3查询性能优化
<!-- S3配置优化 -->
<s3>
<max_single_read_retries>4</max_single_read_retries>
<min_upload_part_size>33554432</min_upload_part_size>
<max_inflight_parts_for_one_file>20</max_inflight_parts_for_one_file>
<request_timeout_ms>30000</request_timeout_ms>
<headers>
<X-Request-Id>clickhouse</X-Request-Id>
</headers>
</s3>
本地缓存
<!-- config.xml -->
<cache>
<max_cache_size>10737418240</max_cache_size> <!-- 10GB -->
<max_file_segment_size>33554432</max_file_segment_size>
</cache>
4. 查询优化
-- 优先命中热数据
SELECT * FROM logs
WHERE ts > now() - INTERVAL 7 DAY -- 热数据查询
ORDER BY ts;
5. 最佳实践
- 热盘SSD占20-30%总容量
- S3冷层建议使用标准存储
- 设置合适的
max_data_part_size(避免大Part迁移慢) - 监控
system.detached_parts防止迁移失败