EXPLAIN and EXPLAIN ANALYZE reveal how Amazon Redshift plans and executes a SQL statement, helping you optimize performance.
EXPLAIN shows the planned steps, node types, and data sizes before a query runs, so you can spot distribution or sort-key issues without spending credits.
EXPLAIN is compile-time only.EXPLAIN ANALYZE executes the query and appends run-time metrics such as actual rows, memory, and execution time, making it indispensable for final tuning.
Prefix the statement with EXPLAIN. Redshift returns a JSON-like text plan in one column.Use the advisor
view in Query Editor v2 for visuals.
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;
Add ANALYZE to execute the query and measure each step.Wrap in a transaction to roll back if you do not want to commit data-changing statements.
EXPLAIN ANALYZE
SELECT *
FROM Orders
WHERE order_date > CURRENT_DATE - INTERVAL '30 days';
Node Type shows operations like DS_DIST_ALL_INNER or SORT. Rows and Width reveal estimated volume, while StartupCost and TotalCost indicate planner assumptions.In ANALYZE output, compare actual rows to estimates to detect skew.
Copy the EXPLAIN text or query STL_EXPLAIN
and SVL_QUERY_REPORT
tables by filtering with the query
id. Store them in Galaxy Collections to collaborate.
1) Always examine distribution-style nodes; data movement kills speed. 2) Aim for even row counts across slices.3) Run ANALYZE
on tables so estimates stay accurate.
Make the join key of both tables match distribution keys or use KEY distribution on large tables joined together. Consider copying small dimension tables to ALL.
Skip it on terabyte-scale DELETEs unless you test in a dev cluster; ANALYZE will run the command in full.
.
EXPLAIN alone compiles the query without running it, so it is safe and fast. Only EXPLAIN ANALYZE executes the query.
Yes. Query STL_EXPLAIN
and SVL_QUERY_REPORT
with the past query
ids to retrieve prior plans.
Use AWS Query Editor v2 or third-party tools like Galaxy, which format the JSON into a tree and highlight costly nodes.