AgentSkillsCN

altinity-expert-clickhouse-reporting

诊断 ClickHouse 的 SELECT 查询性能,分析查询模式,识别慢查询,并挖掘优化空间。适用于处理查询延迟及超时问题。

SKILL.md
--- frontmatter
name: altinity-expert-clickhouse-reporting
description: Diagnose ClickHouse SELECT query performance, analyze query patterns, identify slow queries, and find optimization opportunities. Use for query latency and timeout issues.

Query Performance Analysis

Diagnose SELECT query performance issues, analyze query patterns, and identify optimization opportunities.


Diagnostics

Run all queries from the file checks.sql and analyze the results.


Query Optimization Hints

Index Usage Check

sql
-- Check if data skipping indices exist
select
    database,
    table,
    name as index_name,
    type,
    expr,
    granularity
from system.data_skipping_indices
where database = '{database}' and table = '{table}'

Mark Count for Query

For a specific slow query, check how many marks (granules) were read:

sql
select
    query_id,
    read_rows,
    selected_marks,
    selected_parts,
    formatReadableSize(read_bytes) as read_bytes,
    round(read_rows / nullIf(selected_marks, 0)) as rows_per_mark
from system.query_log
where query_id = '{query_id}'
  and type = 'QueryFinish'

High selected_marks relative to result = index not selective enough.


Ad-Hoc Query Guidelines

Required Safeguards

sql
-- Always time-bound
where event_date >= today() - 1
-- or
where event_time > now() - interval 1 hour

-- Always limit
limit 100

-- Filter by type
where type = 'QueryFinish'  -- completed
where type like 'Exception%'  -- failed

Useful Filters

sql
-- By user
where user = 'analytics_user'

-- By query pattern
where query ilike '%SELECT%FROM my_table%'

-- By duration threshold
where query_duration_ms > 10000  -- > 10 seconds

-- By normalized hash (for specific query pattern)
where normalized_query_hash = 1234567890

Cross-Module Triggers

FindingLoad ModuleReason
High memory queriesaltinity-expert-clickhouse-memoryMemory limits/optimization
Reading too many partsaltinity-expert-clickhouse-mergesPart consolidation
Poor index selectivityaltinity-expert-clickhouse-schemaIndex/ORDER BY design
Cache missesaltinity-expert-clickhouse-cachesCache sizing
MV slowaltinity-expert-clickhouse-ingestionMV optimization

Settings Reference

SettingScopeNotes
max_execution_timeQueryQuery timeout
max_rows_to_readQueryLimit rows scanned
max_bytes_to_readQueryLimit bytes scanned
max_threadsQueryParallelism
use_query_cacheQueryEnable query result caching
log_queriesServerEnable query logging
log_queries_min_query_duration_msServerLog threshold