Use EXPLAIN, system tables, and query settings to trace, profile, and optimize slow or failing ClickHouse statements.
Run EXPLAIN
before your SELECT to view the optimizer’s planned steps. Use EXPLAIN SYNTAX
for parsed SQL, EXPLAIN AST
for the abstract syntax tree, and EXPLAIN QUERY TREE
for the full plan.
system.processes
lists currently running queries with memory and read metrics. system.part_log
and system.query_log
store historical details such as duration, rows read, and exception_code.
Append SET send_logs_level='trace'
to capture granular CPU, IO, and network statistics in server logs. Combine with profile_events
columns from system.query_log
to pinpoint bottlenecks.
Check if the ORDER BY or WHERE clauses hit the primary key. Use EXPLAIN QUERY TREE SELECT * FROM Orders WHERE order_date = today()
. If the plan shows an Unsorted read, add a data-skipping index or adjust the ORDER BY key.
Set trace_id
in the session, execute the failing statement, then inspect system.trace_log
filtered by that id. The stack trace reveals function names and line numbers inside the ClickHouse engine.
Yes. SET max_threads = 0
combined with EXPLAIN
validates syntax and plan generation without touching disks.
Attach SETs
after the query to keep scripts self-contained: EXPLAIN QUERY TREE SELECT … SETTINGS max_threads=1, allow_experimental_analyzer=1
.
Add SAMPLE 0.1
to SELECTs when investigating logic rather than performance. This returns 10 % of rows, reducing wait time.
Tag every ad-hoc query with SET log_comment = 'debug-order-pipeline'
so you can grep logs later.
Use EXPLAIN for the plan, system.processes for live stats, and system.query_log for history. Inline settings and comments keep debugging reproducible.
No. Internal node names can change. Focus on read types and filters rather than exact labels.
Yes. Use SET max_execution_time = 5
seconds in the session to abort runaway statements.
Server logs live in /var/log/clickhouse-server/
unless overridden in config.xml
. Use grep on the log_comment to isolate events.