Amazon Redshift is a fast, scalable, secure, and fully managed cloud data warehouse that lets you analyze your data at scale. Amazon Redshift Serverless lets you access and analyze data without the usual configurations of a provisioned data warehouse. Resources are automatically provisioned and data warehouse capacity is intelligently scaled to deliver fast performance for even the most demanding and unpredictable workloads. If you prefer to manage your Amazon Redshift resources manually, you can create provisioned clusters for your data querying needs. For more information, refer to Amazon Redshift clusters.
Amazon Redshift provides performance metrics and data so you can track the health and performance of your provisioned clusters, serverless workgroups, and databases. The performance data you can use on the Amazon Redshift console falls into two categories:
- Amazon CloudWatch metrics – Helps you monitor the physical aspects of your cluster or serverless, such as resource utilization, latency, and throughput.
- Query and load performance data – Helps you monitor database activity, inspect and diagnose query performance problems.
Amazon Redshift has introduced a new feature called the Query profiler. The Query profiler is a graphical tool that helps users analyze the components and performance of a query. This feature is part of the Amazon Redshift console and provides a visual and graphical representation of the query’s run order, execution plan, and various statistics. The Query profiler makes it easier for users to understand and troubleshoot their queries.
In this post, we cover two common use cases for troubleshooting query performance. We show you step-by-step how to analyze and troubleshoot long-running queries using the Query profiler.
Overview
For Amazon Redshift Serverless, the Query profiler can be accessed by going to the Serverless console. Choose Query and database monitoring, select a query, and then navigate to the Query plan tab. If a query plan is available, you will observe a list of child queries. Choose a query to view it in Query profiler.
For Amazon Redshift provisioned, the Query profiler can be accessed by going to the provisioned clusters dashboard. Choose Query and loads, and choose a query. Navigate to the Query plan tab. If a query plan is available, you will observe a list of child queries. Choose a query to view it in Query profiler.
Prerequisites
- You can use the following sample AWS Identity and Access Management (IAM) policy to configure your IAM user or role with minimum privileges to access Query profiler from the AWS console. If your IAM user or role already has access to Query and loads section of Redshift provisioned cluster dashboard or Query and database monitoring section of Redshift serverless dashboard, then no additional permissions are needed:
- You can choose to use Query profiler in your account with an existing Amazon Redshift data warehouse and queries. However, if you would like to implement this demo in your existing Amazon Redshift data warehouse, download Redshift query editor v2 notebook, Redshift Query profiler demo, and refer to the Data Loading section later in this post.
- You must connect to the cluster using database credentials and grant the
sys:operator
orsys:monitor
role to the database user to view queries run by users.
Data loading
Amazon Redshift Query Editor v2 comes with sample data that can be loaded into a sample database and corresponding schema. To test Query profiler against the sample data, load the tpcds sample data and run queries.
- To load the tpcds sample data, launch Redshift query editor v2 and expand the database
sample_data_dev.
- Choose the icon associated with the
tpcds.
- The query editor v2 then loads the data into a schema tpcds in the database sample_data_dev.
The following screenshot shows these steps.
- Verify the data by running the following sample query, as shown in the following screenshot.
Use cases
In this post, we describe two common uses cases around query performance and how to use Query profiler to troubleshoot the performance issues:
- Nested loop joins – This join type is the slowest of the possible join types. Nested loop joins are the cross-joins without a join condition that result in the Cartesian product of two tables.
- Suboptimal data distribution – If data distribution is suboptimal, you might notice a large broadcast or redistribution of data across compute nodes when two large tables are joined together.
Use case 1: Nested loop joins
To troubleshoot performance issues with nest loop joins using Query profiler, follow these steps:
- Import notebook downloaded previously in prerequisites section of the blog into Redshift query editor v2.
- Set the context of database to
sample_data_dev
in Query Editor v2, as shown in the following screenshot. - Run
cell #3
from demo notebook to diagnose a query performance issue related to nested loop joins.
- Run
cell #5
to capture the query id from the SYS_QUERY_HISTORY system view filtering based on the query label you set in the preceding step. - On the Amazon Redshift console, in the navigation pane, select Query and loads and choose the cluster name where the query was originally executed, as shown in the following screenshot.
- This will open the new Query profiler. Under the Query history section, choose
Connect to database
.After successful connection to the database, you will observe the Status showing asConnected
and displaying the query history, as shown in the following screenshot. - You can find your queries either by Query ID or Process ID. Enter the Query ID captured in the preceding step to filter the long-running query for further analysis and choose the corresponding Query ID, as shown in the following screenshot.
- Under the Query plan section, choose
Child query 1
, as shown in the following screenshot. If there are multiple child queries, you will have to inspect each one for performance issues.
This will open the query plan in a tree view along with additional metrics on the side panel. This allows you to quickly analyze the query streams, segments and steps. For more information about streams, segments, and steps, refer to Query planning and execution workflow in the Amazon Redshift Database Developer Guide. - Turn on View streams and, in the Streams side panel, investigate and identify which stream has the highest execution time. In this case, Streams ID 5 is where the query spends the majority of time, as shown in the following screenshot
- In the Streams side panel, under ID, select 5 to focus on Stream 5 for further analysis. Stream 5 shows a step of Nestloop, as shown in the following screenshot.
- Choose the Nestloop step to further analyze. The side panel will change with step details and additional metrics about the nested loop join.
- By looking at Step details – nestloop, we can inspect the Input rows and compare that with the Output rows, as shown in the following screenshot. In this case, due to the cross-joining with the
Store_returns
table, 287,514 input rows explodes to 950,233,770 rows, thus causing our query to run slower. - Fix the query by introducing a join condition between the
store_sales
andstore_returns
. Runcell #7
from Query editor v2 demo notebook.The re-written query runs in just 307 milliseconds.
Use case 2: Suboptimal data distribution
- To demonstrate suboptimal data distribution, change the distribution style of tables
web_sales
andweb_returns
to EVEN by runningcell #10
of Query editor v2 demo notebook.
- Run
cell #12
. The query takes 409 milliseconds to run, as shown by the elapsed time in the following screenshot of the Query editor v2. - Follow steps 3–10 from use case 1 to locate the
query_id
and to open the Query profiler view for the preceding query. - On the Query profiler page for the preceding query, turn on View streams. In the Streams side panel, investigate and identify which stream has the highest execution time. In this case, Stream ID 6 is where the query spends a majority of the time, as shown in the following screenshot.
- Under ID, select 6 from the Streams side panel for further analysis.
Stream 6 shows a step of hash join, which involves a hash join of two tables that are both redistributed. This can be inferred from Hash Right Join DS_DIST_BOTH under Explain plan node information in the following screenshot. Usually, these redistributions occur because the tables aren’t joined on their distribution keys, or they don’t have the correct distribution style. In the case of large tables, these redistributions can lead to significant performance degradation and, hence, it is important to identify and fix such steps to optimize query performance.
- Fix this suboptimal data distribution pattern by choosing the appropriate distribution keys on the tables involved:
web_sales
andweb_returns
. To change the distribution styles, runcell #14
of demo notebook to alter table commands. - After the preceding commands finish running, run
cell #16
to re-execute the select query. As shown in the Query Editor in the following screenshot, now the same query finished in 244 milliseconds after updating the distribution style to key for tablesweb_sales
andweb_returns
.
- In the Query profiler view, turn on View streams and notice that Streams 5 now took the most time. It took 8 milliseconds to finish, as compared to 13 milliseconds in the preceding step.
- In the Streams side panel, under ID, select 5 to drill down further, then choose the Hashjoin As the following screenshot shows, after changing the distribution style to key for both
web_sales
andweb_return
tables, none of the tables need to be redistributed at the query runtime, resulting in optimized performance.
Considerations
Consider the following details while using Query profiler:
- Query profiler displays information returned by the SYS_QUERY_HISTORY, SYS_QUERY_EXPLAIN, SYS_QUERY_DETAIL, and SYS_CHILD_QUERY_TEXT views.
- Query profiler only displays query information for queries that have recently run on the database. If a query completes using a prepopulated resultset cache, Query profiler won’t have information about it because Amazon Redshift doesn’t generate a query plan for such queries.
- Queries run by Query profiler to return the query information run on the same data warehouse as the user-defined queries.
Clean Up
To avoid unexpected costs, complete the following action to delete the resources you created:
Drop all the tables in the sample_data_dev
under tpcds
schema.
Conclusion
In this post, we discussed how to use Amazon Redshift Query profiler to monitor and troubleshoot long-running queries. We demonstrated a step-by-step approach to analyze query performance by examining the query execution plan and statistics and identifying the root cause of query slowness. Try this feature in your environment and share your feedback with us.
About the Authors
Raks Khare is a Senior Analytics Specialist Solutions Architect at AWS based out of Pennsylvania. He helps customers across varying industries and regions architect data analytics solutions at scale on the AWS platform. Outside of work, he likes exploring new travel and food destinations and spending quality time with his family.
Blessing Bamiduro is part of the Amazon Redshift Product Management team. She works with customers to help explore the use of Amazon Redshift ML in their data warehouse. In her spare time, Blessing loves travels and adventures.
Ekta Ahuja is an Amazon Redshift Specialist Solutions Architect at AWS. She is passionate about helping customers build scalable and robust data and analytics solutions. Before AWS, she worked in several different data engineering and analytics roles. Outside of work, she enjoys landscape photography, traveling, and board games.