EXPLAIN variants show ClickHouse’s execution plan so you can spot bottlenecks and optimize queries.
EXPLAIN returns the logical or physical execution plan for a query, revealing how ClickHouse will read, filter, join, and aggregate data before it runs the statement.
Use EXPLAIN AST
(abstract syntax), EXPLAIN SYNTAX
(rewritten SQL), EXPLAIN PLAN
(logical plan), and EXPLAIN PIPELINE
(execution pipeline with threads).
Prefix your query with EXPLAIN PLAN
.ClickHouse prints each step—table scan, filtering, aggregation—with estimated rows and cost.
EXPLAIN PLAN SELECT customer_id, sum(total_amount)FROM Orders GROUP BY customer_id;
Run EXPLAIN PIPELINE
to see threaded stages and buffer sizes. This helps identify CPU-bound joins or mis-balanced parallelism on large ecommerce tables.
Query system.query_log
, system.part_log
, and system.processes
to cross-check actual run times, parts read, and memory versus EXPLAIN estimates.
Compare EXPLAIN PLAN
and EXPLAIN PIPELINE
.Watch for full table scans on Orders
or OrderItems
when a composite primary key or skip index could help. Test effects of SET max_threads
and SET optimize_predicate_expression
.
If EXPLAIN shows a single-thread aggregation on Orders
, add GROUP BY
keys matching the primary key, enable GROUP BY IN ORDER
, or create a materialized aggregate table.
Relying only on EXPLAIN AST
hides parallelism details.Ignoring the rows
column in EXPLAIN PLAN
may lead to wrong memory sizing.
Tools like Galaxy display EXPLAIN output side-by-side with the query, highlight expensive steps, and let you share optimized versions with teammates instantly.
.
No. EXPLAIN shows estimates. Check system.query_log
for actual rows and time.
Yes. Prefix the full statement with EXPLAIN to inspect the read side.
Yes. Redirect EXPLAIN output to files and compare them in pull requests to detect query regressions.