ClickHouse Performance Tuning
Overview
Diagnose and fix ClickHouse performance issues using query analysis, proper indexing,
projections, materialized views, and server settings tuning. Work top-down: measure
first with system.query_log, then apply the single highest-leverage fix (usually the
ORDER BY key), then re-measure to confirm.
Prerequisites
- ClickHouse tables with data (see
clickhouse-core-workflow-a)
- Access to
system.query_log and system.parts
Instructions
The tuning workflow is seven independent steps. Diagnose first, then reach for the fix
that matches the bottleneck. Each step's full SQL lives in
references/implementation.md — start there for the
complete, copy-paste commands.
- Diagnose slow queries — rank the last 24h of
system.query_log by
query_duration_ms, then inspect a suspect query with EXPLAIN PLAN /
EXPLAIN PIPELINE.
- ORDER BY key optimization — the primary lever. Filtering on the ORDER BY prefix
skips whole granules; a mismatched key forces a full scan.
- Data skipping indexes —
bloom_filter for high-cardinality lookups, set for
low-cardinality columns, minmax for range filters on non-key columns.
- Projections — automatic pre-aggregation ClickHouse picks transparently when a
query matches the projection's shape.
- Server settings —
max_threads, external sort/group-by spill, async_insert,
and friends, set per-query or per-session.
- Materialized views — pre-aggregate on INSERT into an
AggregatingMergeTree so
dashboard reads hit milliseconds, not seconds.
- Query patterns —
PREWHERE, LIMIT BY, and avoiding FINAL.
The essential first move — find the slowest queries:
SELECT event_time, query_duration_ms, read_rows, read_bytes,
substring(query, 1, 300) AS query_preview
FROM system.query_log
WHERE type = 'QueryFinish'
AND event_time >= now() - INTERVAL 24 HOUR
AND query_duration_ms > 1000 -- > 1 second
ORDER BY query_duration_ms DESC
LIMIT 20;
Output
Applying this workflow produces:
- A ranked list of the slowest queries with their
read_rows / read_bytes cost.
- One or more concrete schema/query changes: a corrected
ORDER BY key, added data
skipping indexes, a projection, a materialized view, or tuned session settings.
- A before/after measurement from
system.query_log proving the change reduced
read_rows, read_bytes, query_duration_ms, or memory_usage.
Error Handling
| Issue |
Indicator |
Solution |
| Full table scan |
read_rows = total rows |
Fix ORDER BY to match filters |
| Memory exceeded |
Error 241 |
Add LIMIT, use streaming, increase limit |
| Slow GROUP BY |
High read_bytes |
Add materialized view or projection |
| Merge backlog |
Parts > 300 |
Reduce insert frequency, increase merge threads |
Examples
Worked before/after scenarios — full-scan → ORDER BY fix, slow GROUP BY → projection,
confirming a skipping index fires, and the query-cost measurement query — are in
references/examples.md. The core measurement, run right after
any query you are tuning:
SELECT query_duration_ms, read_rows,
formatReadableSize(read_bytes) AS read_size,
formatReadableSize(memory_usage) AS memory
FROM system.query_log
WHERE query_id = currentQueryId() AND type = 'QueryFinish';
Resources
Next Steps
For cost optimization, see clickhouse-cost-tuning.
Source: jeremylongshore/claude-code-plugins-plus-skills → plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md
1---2name: clickhouse-performance-tuning3description: "| Optimize ClickHouse query performance with indexing, projections, settings tuning, and query analysis using system tables. Use when queries are slow, investigating performance bottlenecks, or tuning ClickHouse server settings. Trigger with \"clickhouse performance\", \"optimize clickhouse query\", \"clickhouse slow query\", \"clickhouse indexing\", \"clickhouse tuning\", \"clickhouse projections\"."4allowed-tools: "Read, Write, Edit"5category: engineering-core6source_repo: jeremylongshore/claude-code-plugins-plus-skills7source_path: "plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md"8source_url: https://github.com/jeremylongshore/claude-code-plugins-plus-skills/blob/HEAD/plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md9metadata:10 source: https://skillmd.com/skills/thedixitjain/clickhouse-performance-tuning11---1213# ClickHouse Performance Tuning1415## Overview1617Diagnose and fix ClickHouse performance issues using query analysis, proper indexing,18projections, materialized views, and server settings tuning. Work top-down: measure19first with `system.query_log`, then apply the single highest-leverage fix (usually the20ORDER BY key), then re-measure to confirm.2122## Prerequisites2324- ClickHouse tables with data (see `clickhouse-core-workflow-a`)25- Access to `system.query_log` and `system.parts`2627## Instructions2829The tuning workflow is seven independent steps. Diagnose first, then reach for the fix30that matches the bottleneck. Each step's full SQL lives in31[references/implementation.md](references/implementation.md) — start there for the32complete, copy-paste commands.33341. **Diagnose slow queries** — rank the last 24h of `system.query_log` by35 `query_duration_ms`, then inspect a suspect query with `EXPLAIN PLAN` /36 `EXPLAIN PIPELINE`.372. **ORDER BY key optimization** — the primary lever. Filtering on the ORDER BY prefix38 skips whole granules; a mismatched key forces a full scan.393. **Data skipping indexes** — `bloom_filter` for high-cardinality lookups, `set` for40 low-cardinality columns, `minmax` for range filters on non-key columns.414. **Projections** — automatic pre-aggregation ClickHouse picks transparently when a42 query matches the projection's shape.435. **Server settings** — `max_threads`, external sort/group-by spill, `async_insert`,44 and friends, set per-query or per-session.456. **Materialized views** — pre-aggregate on INSERT into an `AggregatingMergeTree` so46 dashboard reads hit milliseconds, not seconds.477. **Query patterns** — `PREWHERE`, `LIMIT BY`, and avoiding `FINAL`.4849The essential first move — find the slowest queries:5051```sql52SELECT event_time, query_duration_ms, read_rows, read_bytes,53 substring(query, 1, 300) AS query_preview54FROM system.query_log55WHERE type = 'QueryFinish'56 AND event_time >= now() - INTERVAL 24 HOUR57 AND query_duration_ms > 1000 -- > 1 second58ORDER BY query_duration_ms DESC59LIMIT 20;60```6162## Output6364Applying this workflow produces:6566- A ranked list of the slowest queries with their `read_rows` / `read_bytes` cost.67- One or more concrete schema/query changes: a corrected `ORDER BY` key, added data68 skipping indexes, a projection, a materialized view, or tuned session settings.69- A before/after measurement from `system.query_log` proving the change reduced70 `read_rows`, `read_bytes`, `query_duration_ms`, or `memory_usage`.7172## Error Handling7374| Issue | Indicator | Solution |75|-------|-----------|----------|76| Full table scan | `read_rows` = total rows | Fix ORDER BY to match filters |77| Memory exceeded | Error 241 | Add LIMIT, use streaming, increase limit |78| Slow GROUP BY | High `read_bytes` | Add materialized view or projection |79| Merge backlog | Parts > 300 | Reduce insert frequency, increase merge threads |8081## Examples8283Worked before/after scenarios — full-scan → ORDER BY fix, slow GROUP BY → projection,84confirming a skipping index fires, and the query-cost measurement query — are in85[references/examples.md](references/examples.md). The core measurement, run right after86any query you are tuning:8788```sql89SELECT query_duration_ms, read_rows,90 formatReadableSize(read_bytes) AS read_size,91 formatReadableSize(memory_usage) AS memory92FROM system.query_log93WHERE query_id = currentQueryId() AND type = 'QueryFinish';94```9596## Resources9798- [references/implementation.md](references/implementation.md) — full 7-step SQL walkthrough99- [references/examples.md](references/examples.md) — worked before/after tuning examples100- [Projections](https://clickhouse.com/docs/sql-reference/statements/alter/projection)101- [Data Skipping Indexes](https://clickhouse.com/docs/engines/table-engines/mergetree-family/mergetree#table_engine-mergetree-data_skipping-indexes)102- [MergeTree Settings](https://clickhouse.com/docs/operations/settings/merge-tree-settings)103104## Next Steps105106For cost optimization, see `clickhouse-cost-tuning`.107108---109110**Source:** [`jeremylongshore/claude-code-plugins-plus-skills`](https://github.com/jeremylongshore/claude-code-plugins-plus-skills) → `plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md`