New to Claude Skills? Learn how to install them →

civitai on GitHub

ClickHouse Query

Free

Run ClickHouse queries for analytics and metrics analysis.

by civitai7.2k stars on civitai/civitai
1 views
Updated Aug 10, 2026
Get this skill

Free · Opens the source repo

What ClickHouse Query does

The ClickHouse Query skill is designed for developers and data analysts who need to execute queries directly against a ClickHouse database. This skill allows users to run ad-hoc queries for analytics, metrics analysis, and event data exploration. It provides a straightforward command-line interface to interact with ClickHouse, making it easy to analyze data and debug queries without needing to set up a separate environment.

To use the skill, simply invoke the included query.mjs script with your SQL command. The skill supports various options, such as specifying a timeout for queries, reading queries from a file, and outputting results in JSON format. This flexibility allows users to tailor their queries to specific needs, whether they are counting rows, filtering data, or checking the execution plan of a query.

Safety features are built into the skill to prevent unintended data modifications. By default, it operates in read-only mode, requiring explicit permission to perform write operations. Additionally, it has a default timeout of 30 seconds to avoid long-running queries, which can be adjusted as needed. This makes it suitable for both exploratory data analysis and performance testing.

Overall, the ClickHouse Query skill is an essential tool for anyone working with ClickHouse who needs to perform quick queries and analysis on their data, ensuring both safety and efficiency in data handling.

When to use it

Use this skill when you need to run queries on ClickHouse for analytics, metrics, or debugging purposes.

When not to use it

This skill is not suitable for applications requiring frequent write operations unless explicit permission is granted.

What you can build with it

Count Rows in a Table

Quickly count the number of rows in a ClickHouse table to get a sense of data volume.

Analyze Event Tracking Data

Run queries to filter and analyze event data, such as user interactions or model events.

Check Query Performance

Use the `--explain` flag to analyze the execution plan of your queries for optimization.

How to install ClickHouse Query

View source

1. Install with the skills CLI

npx skills add civitai/civitai/clickhouse-query --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by civitai

ClickHouse Query Testing

Use this skill to run ad-hoc ClickHouse queries for analytics, metrics analysis, and debugging.

Running Queries

Use the included query script:

node .claude/skills/clickhouse-query/query.mjs "SELECT count() FROM views"

Options

FlagDescription
--explainShow query execution plan
--writableAllow write operations (requires user permission)
--timeout <s>, -tQuery timeout in seconds (default: 30)
--file, -fRead query from a file
--jsonOutput results as JSON
--quiet, -qMinimal output, only results

Examples

# Count rows in a table
node .claude/skills/clickhouse-query/query.mjs "SELECT count() FROM views"

# Query with filters
node .claude/skills/clickhouse-query/query.mjs "SELECT * FROM modelEvents WHERE modelId = 123 LIMIT 10"

# Check query execution plan
node .claude/skills/clickhouse-query/query.mjs --explain "SELECT * FROM views WHERE userId = 1"

# Override default 30s timeout for longer queries
node .claude/skills/clickhouse-query/query.mjs --timeout 60 "SELECT ... (complex aggregation)"

# Query from file
node .claude/skills/clickhouse-query/query.mjs -f my-query.sql

# JSON output for processing
node .claude/skills/clickhouse-query/query.mjs --json "SELECT type, count() FROM modelEvents GROUP BY type"

Safety Features

  1. Read-only by default: Blocks INSERT/ALTER/DROP unless --writable flag is used
  2. 30 second timeout: Prevents runaway queries (override with --timeout)
  3. Explicit permission required: Before using --writable, you MUST ask the user for permission

When to Use --writable

Only use the --writable flag when:

  • The user explicitly requests write access
  • You need to insert test data
  • You're running maintenance operations

IMPORTANT: Always ask the user for permission before running with --writable.

Common Tables

TableDescription
viewsPage/entity view events
modelEventsModel create/publish/update events
modelVersionEventsModel version events including downloads
userActivitiesUser registration, login, subscription events
imagesImage upload/delete events
reactionsLike/dislike events
reportsContent report events
entityMetricEventsAggregated metric events

Querying Replica Clusters

IMPORTANT: Production uses a ClickHouse replica cluster. When querying system tables (logs, metrics, etc.), you must use clusterAllReplicas() to get data from all nodes.

System Tables on Replica Clusters

-- WRONG: Only queries the node you're connected to
SELECT * FROM system.query_log WHERE event_time > now() - INTERVAL 1 HOUR

-- CORRECT: Queries all replicas in the cluster
SELECT * FROM clusterAllReplicas(default, system.query_log)
WHERE event_time > now() - INTERVAL 1 HOUR

Common System Table Queries

-- Find recent queries across all nodes
SELECT
    hostname(),
    event_time,
    query_duration_ms,
    formatReadableSize(memory_usage) AS memory,
    query
FROM clusterAllReplicas(default, system.query_log)
WHERE type = 'QueryFinish'
    AND event_time > now() - INTERVAL 5 MINUTE
ORDER BY event_time DESC
LIMIT 20

-- Find expensive queries by memory usage (last 24 hours)
SELECT
    count() as query_count,
    user,
    sum(memory_usage) AS total_memory,
    normalized_query_hash
FROM clusterAllReplicas(default, system.query_log)
WHERE event_time > now() - INTERVAL 1 DAY
    AND query_kind = 'Select'
    AND type = 'QueryFinish'
GROUP BY normalized_query_hash, user
ORDER BY total_memory DESC
LIMIT 10

-- Search query logs by pattern
SELECT event_time, query_id, query, type
FROM clusterAllReplicas(default, merge('system', '^query_log*'))
WHERE query ILIKE '%some_table%'
    AND event_time > now() - INTERVAL 5 MINUTE

-- Debug a specific query across all nodes
SELECT hostname(), message
FROM clusterAllReplicas(default, system.text_log)
WHERE query_id = 'your-query-id-here'
ORDER BY event_time_microseconds ASC

When to Use clusterAllReplicas()

Use CaseFunction
System tables (query_log, text_log, etc.)clusterAllReplicas(default, system.table_name)
Application tables (views, modelEvents, etc.)Direct query (already distributed)
Search multiple system tablesclusterAllReplicas(default, merge('system', '^pattern*'))

ClickHouse SQL Tips

-- Use count() not COUNT(*)
SELECT count() FROM views

-- Date filtering with toDate()
SELECT * FROM views WHERE toDate(time) = today()

-- Last 7 days
SELECT * FROM modelEvents WHERE time > now() - INTERVAL 7 DAY

-- Aggregations
SELECT type, count() as cnt FROM modelEvents GROUP BY type ORDER BY cnt DESC

Frequently asked questions about ClickHouse Query

Similar skills