基本配置:1.
服务器配置(/etc/clickhouse-server/config.xml): -
<listen_host>:要监听的 IP 地址(0.0.0.0 表示所有地址)
-
<http_port>:HTTP 接口端口(默认 8123)
-
<tcp_port>:原生协议端口(默认 9000)
-
<max_connections>:最大并发连接数
-
<max_concurrent_queries>:限制并行查询数
2.
内存与性能调优:<max_server_memory_usage>80000000000</max_server_memory_usage> <!-- 80GB -->
<max_memory_usage>40000000000</max_memory_usage> <!-- 40GB per query -->
<max_bytes_before_external_group_by>20000000000</max_bytes_before_external_group_by>
<max_threads>8</max_threads> <!-- Parallel query threads -->
3.
存储配置:<storage_configuration>
<disks>
<default>
<path>/var/lib/clickhouse/</path>
</default>
<fast_ssd>
<path>/mnt/fast_ssd/clickhouse/</path>
</fast_ssd>
<slow_hdd>
<path>/mnt/slow_hdd/clickhouse/</path>
</slow_hdd>
</disks>
<policies>
<hot_and_cold>
<volumes>
<hot>
<disk>fast_ssd</disk>
</hot>
<cold>
<disk>slow_hdd</disk>
</cold>
</volumes>
<move_factor>0.2</move_factor>
</hot_and_cold>
</policies>
</storage_configuration>
4.
建表示例:-- MergeTree (basic)
CREATE TABLE events (
event_date Date,
event_time DateTime,
user_id UInt32,
event_type String,
url String
) ENGINE = MergeTree()
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_date, user_id)
SETTINGS index_granularity = 8192;
-- ReplicatedMergeTree (with replication)
CREATE TABLE events_replicated (
event_date Date,
event_time DateTime,
user_id UInt32,
event_type String
) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/events', '{replica}')
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_date, user_id);
-- Distributed table (cluster)
CREATE TABLE events_distributed AS events_replicated
ENGINE = Distributed(cluster_name, database_name, events_replicated, rand());
5.
数据类型与压缩:CREATE TABLE metrics (
timestamp DateTime CODEC(Delta, ZSTD),
metric_name LowCardinality(String),
value Float64 CODEC(Gorilla, ZSTD),
tags Array(String),
metadata Map(String, String)
) ENGINE = MergeTree()
ORDER BY (metric_name, timestamp);
6.
查询优化:-- Use PREWHERE for filtering (faster than WHERE)
SELECT * FROM events
PREWHERE event_date >= today() - 7
WHERE user_id IN (SELECT id FROM users WHERE premium = 1);
-- Materialized views for pre-aggregation
CREATE MATERIALIZED VIEW events_hourly
ENGINE = SummingMergeTree()
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_date, event_hour, event_type)
AS SELECT
toDate(event_time) AS event_date,
toHour(event_time) AS event_hour,
event_type,
count() AS event_count
FROM events
GROUP BY event_date, event_hour, event_type;
7.
数据保留与 TTL:-- Automatic data expiration
ALTER TABLE events
MODIFY TTL event_date + INTERVAL 90 DAY;
-- Move old data to cold storage
ALTER TABLE events
MODIFY TTL
event_date + INTERVAL 7 DAY TO VOLUME 'hot',
event_date + INTERVAL 30 DAY TO VOLUME 'cold',
event_date + INTERVAL 90 DAY DELETE;
8.
监控查询:-- System metrics
SELECT * FROM system.metrics;
-- Current queries
SELECT query_id, user, query, elapsed FROM system.processes;
-- Query log analysis
SELECT
query_duration_ms,
read_rows,
read_bytes,
memory_usage,
query
FROM system.query_log
WHERE type = 'QueryFinish'
ORDER BY query_duration_ms DESC
LIMIT 10;
-- Table sizes
SELECT
database,
table,
formatReadableSize(sum(bytes)) AS size,
sum(rows) AS rows
FROM system.parts
WHERE active
GROUP BY database, table
ORDER BY sum(bytes) DESC;
最佳实践:- 生产环境的表请使用 MergeTree 引擎系列
- 谨慎选择 ORDER BY,它会影响查询性能
- 时间序列数据按日期分区(便于启用 TTL 并高效删除)
- 对于唯一值少于 10K 的字符串列,使用 LowCardinality
- 为特定数据类型启用压缩编解码器(Delta、Gorilla、ZSTD)
- 为频繁使用的聚合创建物化视图
- 多节点部署请使用分布式表
- 通过 system.query_log 监控查询性能
- 设置合适的 max_memory_usage,以防止内存溢出(OOM)
- 定期执行 OPTIMIZE TABLE 进行合并操作(但应谨慎使用)