EXPLAIN in Redshift shows the planned steps, node types, and data flow so you can spot bottlenecks and tune queries.
EXPLAIN reveals how Redshift intends to execute your query—scan type, join order, distribution, and estimated rows. Viewing the plan first prevents blind index changes and pinpoints the exact step that needs work.
Prefix your query with the keyword EXPLAIN
. The command returns a JSON-like plan in a single column called QUERY PLAN
.
EXPLAIN SELECT c.name, SUM(oi.quantity * p.price) AS lifetime_value
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
JOIN OrderItems oi ON oi.order_id = o.id
JOIN Products p ON p.id = oi.product_id
GROUP BY c.name;
Read from the bottom up. The last line is the plan root (often XN GroupAggregate
). Indented lines above represent child nodes such as scans, joins, or aggregates. Key columns include Rows (estimate) and Bytes.
DS_DIST_BCAST_INNER: Broadcast inner table to all slices.
DS_DIST_HASH: Re‐hash and redistribute rows.
DS_SORTED_MERGE: Merge join on sorted streams.
SCAN: Sequential or index scan of a table.
Look for DS_DIST redistribution nodes—they signal large network shuffles. Also watch for sequential scans on massive tables, high estimated rows, and nested loop joins on unsorted data.
Match DISTSTYLE and DISTKEY across joined tables to remove redistribute nodes. Add SORTKEYs to support merge joins. Break huge aggregations into CTES. Filter early with WHERE to shrink intermediate sets.
Use EXPLAIN VERBOSE
to include predicate text and distribution keys. This deeper detail helps verify that Redshift uses the intended DISTKEY/SORTKEY.
Estimates rely on block-level statistics. After large INSERTs, run ANALYZE
to refresh stats before depending on EXPLAIN numbers.
1) Run ANALYZE
first. 2) EXPLAIN every expensive query. 3) Eliminate DS_DIST nodes. 4) Favor merge/hash joins over nested loops. 5) Re-EXPLAIN after schema changes.
No. EXPLAIN only parses and plans; it never reads or writes data.
VERBOSE adds predicate text, distribution style, and sort keys, providing deeper insight for advanced tuning.
Yes. Query SVL_QLOG
or enable the enable_result_cache_for_session
parameter and inspect SVL_EXPLAIN
for real execution metrics.