필수 구성: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로 쿼리 성능을 모니터링하세요
- OOM을 방지하도록 적절한 max_memory_usage를 설정하세요
- 병합 작업을 위해 OPTIMIZE TABLE을 정기적으로 실행하세요(다만 꼭 필요한 경우에만 신중하게 사용하세요)