Skip to content
OpenSmartRoute
Skillv1.0.0

clickhouse-performance-tuning

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 C

by jeremylongshore(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from jeremylongshore/tons-of-skills-marketplace (plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md). Install upstream with npx skills add jeremylongshore/tons-of-skills-marketplace --skill clickhouse-performance-tuning. Copyright stays with the author (MIT).

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.

  1. 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.
  2. ORDER BY key optimization — the primary lever. Filtering on the ORDER BY prefix skips whole granules; a mismatched key forces a full scan.
  3. Data skipping indexesbloom_filter for high-cardinality lookups, set for low-cardinality columns, minmax for range filters on non-key columns.
  4. Projections — automatic pre-aggregation ClickHouse picks transparently when a query matches the projection's shape.
  5. Server settingsmax_threads, external sort/group-by spill, async_insert, and friends, set per-query or per-session.
  6. Materialized views — pre-aggregate on INSERT into an AggregatingMergeTree so dashboard reads hit milliseconds, not seconds.
  7. Query patternsPREWHERE, 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.

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/jeremylongshore-tons-of-skills-marketplace-clickhouse-pe-1dc8c0/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

jeremylongshore-tons-of-skills-marketplace-clickhouse-pe-1dc8c0.ocm.jsonjson
{
  "ocm": "1",
  "id": "jeremylongshore-tons-of-skills-marketplace-clickhouse-pe-1dc8c0",
  "kind": "skill",
  "name": "clickhouse-performance-tuning",
  "description": "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\".",
  "publisher": "jeremylongshore",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "data_analysis"
    ],
    "tags": [
      "skill-md",
      "saas",
      "database",
      "analytics",
      "clickhouse",
      "olap",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "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\"."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/jeremylongshore/tons-of-skills-marketplace",
      "path": "plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/jeremylongshore/tons-of-skills-marketplace/blob/HEAD/plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md",
      "key": "jeremylongshore/tons-of-skills-marketplace/plugins/saas-packs/clickhouse-pack/skills/clickhouse-performance-tuning/SKILL.md"
    },
    "compatibility": "Designed for Claude Code",
    "allowed_tools": [
      "Read,",
      "Write,",
      "Edit"
    ],
    "license": "MIT"
  },
  "instructions": "# ClickHouse Performance Tuning\n\n## Overview\n\nDiagnose and fix ClickHouse performance issues using query analysis, proper indexing,\nprojections, materialized views, and server settings tuning. Work top-down: measure\nfirst with `system.query_log`, then apply the single highest-leverage fix (usually the\nORDER BY key), then re-measure to confirm.\n\n## Prerequisites\n\n- ClickHouse tables with data (see `clickhouse-core-workflow-a`)\n- Access to `system.query_log` and `system.parts`\n\n## Instructions\n\nThe tuning workflow is seven independent steps. Diagnose first, then reach for the fix\nthat matches th",
  "cost": {
    "context_tokens": 1006
  }
}

Fetch it by URL: GET /api/v1/registry/jeremylongshore-tons-of-skills-marketplace-clickhouse-pe-1dc8c0/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.