EXPLAIN and EXPLAIN ANALYZE let you preview or time a query plan in Redshift without changing data.
Testing reveals performance issues before users feel them. It also prevents runaway scans that lock clusters or inflate costs. Redshift provides EXPLAIN and EXPLAIN ANALYZE to preview or benchmark execution plans.
EXPLAIN parses SQL, builds a plan, and returns step-by-step operations with estimated rows, width, and cost. The query never runs, so data remains untouched.
EXPLAIN ANALYZE runs the query and appends actual row counts and timing to each step. Use it on limited datasets or with a LIMIT clause for safe benchmarking.
EXPLAIN [ANALYZE] [VERBOSE] your_query;
Add VERBOSE to show predicate filters and table statistics.
EXPLAIN ANALYZE
SELECT o.id,
o.order_date,
SUM(oi.quantity * p.price) AS order_total
FROM Orders o
JOIN OrderItems oi ON oi.order_id = o.id
JOIN Products p ON p.id = oi.product_id
WHERE o.order_date >= '2024-01-01'
GROUP BY o.id, o.order_date
ORDER BY order_total DESC
LIMIT 10;
The output highlights scan types (DS_DIST_BOTH, HASH JOIN), row counts, and total query runtime.
• Always start with plain EXPLAIN to check distribution and join types.
• Add LIMIT to reduce execution time when using ANALYZE.
• Compare estimated vs. actual rows; a large gap signals stale statistics—run ANALYZE or VACUUM.
• Watch for DS_DIST_* steps; redistribute keys or sort keys to minimize.
EXPLAIN ANALYZE executes the query. Without LIMIT, it can consume cluster resources. Use plain EXPLAIN first or test in a development cluster.
Redshift cost numbers are relative, not milliseconds. Focus on step order and high row counts. Combine with STL_QUERY runtime data for full insight.
Start at the deepest indented line—that’s the first operation. Follow upward to see join order. Large row counts early indicate expensive scans.
Yes. Store SQL in a test suite table and iterate with PL/pgSQL or Python scripts, capturing EXPLAIN plans into logging tables for regression tracking.
Plain EXPLAIN has negligible cost because it never reads data. It only builds and returns the query plan.
Statistics may be outdated. Run ANALYZE or VACUUM to refresh them, then test again.
Yes. Insert EXPLAIN outputs into a table with INSERT ... SELECT plan; this enables historical comparison.