Redshift Explain Command and Examples

  • Post author:
  • Post last modified:April 18, 2023
  • Post category:Redshift
  • Reading time:11 mins read

Redshift Explain plan Command is used to get information or cost on individual operations required to execute given query. This command provides information on join operations, aggregate functions, etc. It provides information on how your query would be executed on Redshift under current condition. In this article, we will very how Redshift explain command works with an example.

Redshift Explain Command and Examples

Page Content

Introduction to Redshift

Amazon Redshift is a cloud-based data warehousing service provided by Amazon Web Services (AWS), designed to handle large-scale data analytics workloads. With Redshift, users can easily and cost-effectively analyze their data using SQL and Business Intelligence (BI) tools. One of the most notable features of Redshift is its ability to scale quickly and easily, allowing users to add or remove nodes as their data needs change. Redshift also offers advanced compression techniques, which significantly reduce storage costs, and supports a wide range of data integration options, including ETL (Extract, Transform, Load) tools and APIs.

Additionally, Redshift provides strong security and data encryption features, ensuring the confidentiality and integrity of users data.

Redshift Explain Command

The Redshift EXPLAIN command doesn’t actually run the query. It only shows the plan that Amazon Redshift runs if the query is executed under current operating conditions. The plan may change if you change the database or schema information. The Redshift query plan will also be affected if you collect statistics using Analyze command.

Note that, the EXPLAIN command provides more accurate information if you collect statistics prior to generating query execution plan.

The Explain command will not work for certain commands such as DDL’s or database operations.

You can use Redshift EXPLAIN only for the following commands:

Now, let us see how to use Redshift EXPLAIN plan to optimize your input queries.

Redshift Explain Command Syntax

The EXPLAIN command in Amazon Redshift is used to display the query execution plan for a given SQL statement. The syntax for the EXPLAIN command is as follows:

EXPLAIN [ VERBOSE ] query;

The query parameter is the SQL statement for which you want to see the execution plan. The optional VERBOSE keyword provides additional details about the query plan, such as the distribution and sort keys used, the join strategy, and any data filtering that takes place. The VERBOSE option display full query plan instead of just summery and query is input query of type mentioned earlier.

Redshift Explain Command Example

The following example shows the simple EXPLAIN output:

explain  select * from test_table1;
                                  QUERY PLAN
------------------------------------------------------------------------------
 XN Seq Scan on test_table1  (cost=0.00..0.03 rows=3 width=10)
 ----- Tables missing statistics: test_table1 -----
 ----- Update statistics by running the ANALYZE command on these tables -----
(3 rows)

Note that, VERBOSE option generate more detail query plan.

Redshift Explain Output Operators

In this sections we will check the operators that you see most often in the EXPLAIN output.

OperatorDescription
Sequential Scan The sequential scan operator indicates a table scan
Nested Loop A nested loop is used mainly for cross-joins. Least optimal
Hash Join and Hash A hash join and hash are used for inner joins and left and right outer joins. Faster then Nested loop.
Merge Join A merge join is used for inner joins and outer joins. This is the fastest join compared to other two.
Aggregate Operator for scalar aggregate functions (AVG and SUM)
HashAggregate It is used when you have unsorted grouped aggregate functions.
GroupAggregate It is for sorted grouped aggregate functions.
Sort This operator usually evaluates ORDER BY clause or when sorting is required for example in UNION.
Merge Provides final sorted records.
Subquery Used to run UNION queries.
Hash Intersect Distinct and Hash Intersect All Used to run INTERSECT and INTERSECT ALL queries.
SetOp Except Used to run EXCEPT (or MINUS) queries.
Unique Unique records
LimitProcesses the LIMIT clause.
Window Runs Analytics or window functions.
Result Runs scalar functions that do not involve any table access.
Subplan Used for certain sub-queries.
Network Sends intermediate results to the leader node for further processing.
Materialize Saves rows for input to nested loop joins and some merge joins.

Benefits of Redshift explain plan Command

There are several benefits to using the EXPLAIN command in Amazon Redshift to analyze the execution plan of a SQL statement. Here are some of the key benefits:

  • Optimize query performance: By analyzing the execution plan of a query, you can optimize the query to improve performance.
  • Troubleshoot errors: If a query is not returning the expected results or is producing an error, analyzing the execution plan can help identify the cause of the problem. This can help you troubleshoot and resolve issues more quickly.
  • Understand query execution process: The execution plan provides a detailed view of how the query will be executed, including the distribution and sort keys used, the join strategy, and any data filtering that takes place. This can help you understand how the query is processed and how the data is retrieved.
  • Evaluate query cost: The EXPLAIN output provides an estimated cost for each step in the query execution plan. This can help you evaluate the cost of the query and make decisions about how to optimize it to reduce costs.
  • Select appropriate distribution and sort keys: The EXPLAIN output can help you select appropriate distribution and sort keys for your tables, which can improve query performance and reduce costs.

Conclusion

In conclusion, the EXPLAIN command in Amazon Redshift is a powerful command for analyzing the execution plan of SQL statements. By using this command, users can gain valuable insights into the query processing steps, identify potential issues, optimize query performance, and troubleshoot errors. Additionally, the EXPLAIN output provides estimated costs for each step in the query execution plan, which can help users make informed decisions about how to manage and analyze their data more efficiently.

Related Articles

Hope this helps 🙂