跳到正文
Databricks:Blog·· 34 分鐘前AI 評分56

Databricks 無伺服器工作效能診斷:查詢效能洞察實用指南

Diagnosing serverless job performance: A practitioner's guide to query performance insights

AI 導讀

Databricks 的 Query Performance Insights 現已正式推出,會自動標示有優化空間的查詢並提供修正建議,適用於工作及互動工作階段中的查詢。Jobs UI 會彙總工作及任務層級的查詢指標,並可透過 Genie Code 的「Optimize」功能優先處理預計改善幅度較大的項目;文章亦示範讀取過多資料、低效查詢結構、數據溢出至磁碟及數據偏斜四類問題。

正文

When a job slows down, you have many places to start investigating and no obvious order to do it in. Query metrics, the code, the input data layout: each one can eat an afternoon, and you often don’t know if you’re looking in the right place until you’ve already spent the time digging in. Finding the bottleneck shouldn’t require you going in blind.

Such performance investigations have just gotten significantly easier. Query performance insights, now generally available, automatically flags queries with optimization potential and recommends how to fix them, for any query in your query history, whether it came from a job or an interactive session. And they also roll up to the Jobs UI which includes the following improvements:

  • Queries with performance insights are flagged directly in the job run for you with a red lightbulb icon - see the screenshot below.
  • You can either drill down and pick which query you want to optimize or you can use the “Optimize” button directly from the job run sidebar and Genie Code can help prioritize insights which are estimated to help achieve the most performance improvements for a given job.
  • Data volumes are rolled up to the job and task run level so you can immediately identify if a slowdown was simply caused by more data being processed.

Query performance insights

This blog post walks through how to leverage this new capabilities and features for some common performance troubleshooting scenarios:

  1. Data volume
  2. Inefficient query shape
  3. Data spill
  4. Data skew

Setting the scene

Throughout this blog we follow a retail analytics job built on an orders table. We will start with a simple scenario – aggregating orders to calculate daily revenue broken out by status. From there the pipeline grows: we join orders to a region_promotions table to apply promotional discounts, then bring in a shipments table to track fulfillments, and finally compute a per-customer sequence number across each customer's orders. Each step reflects a common way real pipelines expand over time, adding joins, enrichments, and window functions as more questions get asked of the same core data.

Prerequisites

Before we start optimizing queries, let’s confirm that the source of the slowness is actually caused by the job execution and not spent in other statuses which would require different optimizations.

Let’s verify this for the below example using the execution breakdown. To open the execution breakdown hover over the duration from the sidebar of your job or task run. :

retail analytics job

Here we observe that we had a task run that took a total duration of 41 seconds, but 19 of those were spent installing libraries and only 16 seconds were spent on running actual execution. In this case, optimizing the query performance would not help much to improve the overall runtime.

If 41 seconds is too much, you could optimize the library installation time by reducing the number of packages needed or use a workspace base environment. Learn more about optimizing different statuses and how to optimize them in our documentation.

Let’s take a look at the different scenarios. Scenario 1 will be more detailed to showcase how to navigate across the UI to access the information but the same patterns also apply to scenarios 2-4.

Scenario 1: Data volume - reading too much data

You have a slow running job that calculates the daily revenue for your business, broken out by order status. Now you want to find out why this is the case. You already confirmed that the running time is the bottleneck.

Opening the job run lands you in the Timeline tab.

Data volume

Here you can see the two tasks in the job and the revenue one is showing a lightbulb that indicates that queries in this task contain performance insights.

Clicking it will expand that task to show the multiple queries inside and allow you to see the specific one with the insight.

multiple queries inside

If you click on the lightbulb icon next to the query, it will open up the Query Profile sidebar and show you the insight and associated recommendations.

Query Profile sidebar

You can then optimize with a click of the button, which can be done on the notebook cell level (by clicking the optimize button there) or from the Query Profile sidebar. The “Optimize” button in the metrics section will optimize across all queries in the task or job and help you prioritize the ones with the biggest estimated impact.

notebook cell level

After clicking on “Optimize” Genie Code suggests adding a filter on the order_ts column, on which the table is clustered.

This is the offending query:

Looking closer at the Query Profile sidebar, you see additional metrics at the bottom.

Genie Code

Here you see that no files were pruned. That means that ALL of the files were read and none were skipped. Pruned files represent data we skipped instead of reading, which speeds up the query.

Clicking “View all” next to the “Optimize query” button on the recommendation (see the screenshot above) will give you more info. You can expand the text of the insight and click recommendations on the operator in the graph itself. If you expand the recommendations on the right side, you can also click “Highlight metrics”, which will put a box around the relevant metrics for this insight.

Optimize query

You realize that the query is reading data from orders going back to the beginning of history. What you were really interested in is the revenue breakdown by status on recent orders. Since the table is clustered by order_ts, filtering on this field helps with file pruning and makes it more efficient.

The updated query only filters on the recent month.

Running the improved version brought the time down from 2m15s to 16s.
Here you can see that we now pruned files as expected.

clustered

Scenario 2: Inefficient query shape

Now you have expanded your existing job and the data now includes also “promotions” associated with the “orders”, so you have to join those two tables.

After drilling into the jobs UI you identify the “offending” query to be the following one that was added to your job.

Drilling down into the query metrics you can see that joining these two tables with 778k and 1k rows respectively results in a table with 155 million rows! Our output exploded unexpectedly.

Inefficient query shape

Optimizing it with Genie Code suggests filtering the input data or adding another join key we could use to tighten the output. Looking at the tables, you see there’s actually a promo_code column on both tables so you add it.

Adding that key to the join and rerunning the query, brings our rows written down by a factor of 10.

Scenario 3: Data spill

Next, assume you now also have added a shipments table that needs to be joined to the orders table to get fulfillments.

Again, drilling into the jobs UI and opening the query with performance insights tells you the following query should be optimized due to data spill.

Drilling into the metrics you can see that the data is so large, that some of it is spilling to disk as part of the shuffle in the join.

Drilling into the metrics

Using “Optimize” Genie Code helps to identify that each of those tables actually contains a large column that is making up the majority of the data size. These columns were actually not needed and can be removed, which changed the query to the following.

Rerunning with the optimized query reduced the runtime down to ¼ of the original.

Scenario 4: Data skew

Finally, let’s say we are trying to query the orders table with a sequence number attached to each order for each customer.

Data skew

The job has a query performance insight that data skew on Shuffle is causing a longer than expected execution. The offending query is the following:

Genie identifies the issue – when you partition the data by customer_id for the row_number() function, it’s running into data skew on that column.

Getting counts by customer_id shows that the 0 customer_id has a lot more rows than the rest. The customer_id=0 rows represent orders that could not be assigned to a customer, so it doesn’t make sense to try to sequence-orders-within-customer for them. The new query just sets the sequence for them to NULL.

This reduced the runtime to ⅛ of the original.

And that’s it! Query performance insights helped us improve job performance for the four different scenarios without having to manually identify the best course of action. Instead, it proposes the optimized code with a click of a button using Genie Code.

FAQs

1. What are Query Performance Insights?

Query Performance Insights are automated recommendations that flag queries with optimization potential and suggest how to fix them. Databricks surfaces them directly in the UI (as red lightbulb icons), so you can spot a problematic query without digging through low-level metrics yourself. Query Performance Insights are now generally available (GA).

2. Do they only work for jobs, or for interactive queries too?

Query Performance Insights work for any query in your query history which has a query profile, whether it came from a job or an interactive session. In addition, the insights and query metrics now roll up into the Jobs UI, so you can see at the job and task level which queries are worth optimizing.

3. What does the "Optimize" button actually do?

It hands the flagged query to Genie Code, which proposes a concrete fix directly on the asset, for example updating the code in your notebook for you to review, as long as you have permissions to edit the underlying asset. Used from the job run, it prioritizes the insights for the given job that are estimated to deliver the biggest performance improvement for the job, so you can start with the highest-impact changes.

4. What kinds of performance problems can it help with?

The blog walks through four common scenarios: reading too much data, an inefficient query shape, data spilling to disk, and data skew. More broadly, the insights span query optimization, data layout, and compute and resource issues. They also highlight accelerations Databricks has already applied on your behalf. For the detailed definitions take a look at the official documentation.

5. How does this compare to the Spark UI?

The query profile replaces the Spark UI in serverless, providing easy to understand visualizations of the query execution and in depth query metrics. Query profile and performance insights focuses on what matters and provides the information you need to understand query performance without having to dig through the entire execution. Common troubleshooting scenarios like detecting data skew or disk spill, covered in this blog, are automatically detected and flagged in the UI as query performance insights. Since serverless compute manages infrastructure for you, the focus is on the queries themselves.

Try these new capabilities out on your own jobs! Open a recent run of a serverless job and look for the red lightbulbs, then let Genie Code propose the fix and apply it with one click. To go deeper, explore the documentation:

來源:Databricks:Blog · databricks.com