12-04 更新 实时
首页/ 技术栈/ 正文

无服务器作业性能诊断:查询性能洞察实践指南

技术栈 来源:Databricks Blog 2026-11-30 15:22 1 阅读 机器翻译
无服务器作业性能诊断:查询性能洞察实践指南

验证问题:利用作业的运行分解确认查询执行才是真正的瓶颈。

深入四个实操排查场景:数据量增加、查询形态低效、数据溢出(data spill)和数据倾斜。

当作业变慢时,可以着手排查的地方很多,却没有显而易见的先后顺序。查询指标、代码、输入数据布局:每一项都可能耗掉你一个下午,而且往往要等你真正花时间深挖之后,才知道自己看的地方对不对。定位瓶颈不该让你两眼一抹黑地摸索。

这类性能排查如今变得容易多了。现已正式发布(generally available)的 Query performance insights 会自动标记有优化空间的查询,并给出修复建议,覆盖查询历史中的任意查询,无论它来自作业还是交互式会话。这些洞察还会汇总到 Jobs UI,其中包含以下改进:

带有性能洞察的查询会在作业运行中直接以红色灯泡图标标出——见下方截图。

你既可以下钻并挑选想要优化的查询,也可以直接使用作业运行侧边栏中的“Optimize”按钮,Genie Code 还能帮助按优先级排列各项洞察,这些洞察预计能为给定作业带来最大的性能提升。

数据量会汇总到作业和任务运行层级,让你可以立即判断变慢是否仅仅是因为处理的数据更多了。

本文将介绍如何针对一些常见的性能排查场景,利用这些新的能力和功能:

在本篇博客中,我们始终围绕一个基于 orders 表构建的零售分析作业展开。我们会从一个简单的场景入手——对订单进行聚合,按状态拆分计算每日营收。在此基础上,流水线逐步扩展:我们把 orders 表与 region_promotions 表关联,以应用促销折扣;接着引入 shipments 表来跟踪履约情况;最后为每位客户的每笔订单计算一个客户内序号。每一步都反映了真实流水线随着时间扩展的常见方式:当同一份核心数据被提出更多问题时,就不断加入关联、数据丰富和窗口函数。

在开始优化查询之前,我们先确认:导致运行缓慢的根源确实是作业执行本身,而不是耗费在其他状态上——那些状态需要的是另一类优化。

我们可以借助执行时间分解,在下面这个示例中验证这一点。要打开执行时间分解,请将鼠标悬停在作业或任务运行侧边栏中显示的时长上:

这里我们看到,一次任务运行总耗时为 41 秒,但其中 19 秒花在安装库上,只有 16 秒用于实际执行。在这种情况下,优化查询性能对改善整体运行时间帮助不大。

如果 41 秒太长,你可以通过减少所需的安装包数量,或使用工作区基础环境,来优化库安装时间。想进一步了解如何优化不同的状态以及具体做法,请参阅我们的文档。

让我们来看看不同的场景。场景 1 会讲得更详细,以展示如何在 UI 中导航并获取相关信息,但同样的模式也适用于场景 2-4。

场景 1:数据量——读取了过多数据

你有一个运行缓慢的作业,用于计算业务的每日收入,并按订单状态细分。现在你想弄清楚为什么会这样。你已经确认运行时间是瓶颈。

打开该作业运行后,你会进入 Timeline 标签页。

在这里,你可以看到该作业中的两个任务,而收入那个任务显示了一个灯泡图标,表明此任务中的查询包含性能洞察。

点击它会展开该任务,以显示其中的多个查询,并让你看到带有该洞察的具体查询。

如果你点击查询旁边的灯泡图标,它会打开 Query Profile 侧边栏,并向你显示该洞察及相关建议。

然后,你可以单击按钮进行优化,这可以在 notebook 单元格级别(通过点击那里的优化按钮)完成,也可以从 Query Profile 侧边栏完成。metrics 部分中的“Optimize”按钮将跨任务或作业中的所有查询进行优化,并帮助你优先处理预计影响最大的查询。

点击“Optimize”后,Genie Code 建议在 order_ts 列上添加过滤器,表正是按该列进行聚簇的。

这是导致问题的查询:

再仔细看 Query Profile 侧边栏,你会看到底部还有额外的指标。

在这里你可以看到,没有任何文件被剪枝。也就是说,所有文件都被读取了,没有跳过任何一个。被剪枝的文件代表我们跳过而非读取的数据,这能加快查询速度。

点击建议上 “Optimize query” 按钮旁边的 “View all”(见上方截图),就能获得更多信息。你可以展开该洞察的文本,并在图中算子上点击建议。如果展开右侧的建议,你还可以点击 “Highlight metrics”,它会用方框圈出与该洞察相关的指标。

你意识到,这个查询读取的是从历史最早时期开始的 orders 数据。而你真正关心的是近期订单按状态划分的收入明细。由于该表按 order_ts 聚簇,对该字段进行过滤有助于文件剪枝,从而提升效率。

更新后的查询只过滤最近一个月的数据。

运行改进后的版本,把耗时从 2m15s 降到了 16s。在这里你可以看到,我们现在按预期完成了文件剪枝。

场景 2:低效的查询形态

现在你扩展了已有的作业,数据中还包括与 “orders” 关联的 “promotions”,因此你必须对这两张表进行 join。

深入查看作业 UI 后,你确定“有问题的”查询是添加到你的作业中的以下查询。

深入查看查询指标,你可以看到,将这两个分别有 778k 行和 1k 行的表连接后,会生成一个包含 1.55 亿行的表!我们的输出意外激增。

用 Genie Code 优化它时,建议过滤输入数据,或添加另一个可用于收窄输出的连接键。查看这些表后,你发现两个表上实际上都有 promo_code 列,于是你加上了它。

将该键加入连接并重新运行查询后,使写入的行数降至原来的 1/10。

接下来,假设你现在还添加了一个 shipments 表,需要将其与 orders 表连接以获得履约信息。

同样,深入查看作业 UI 并打开带有性能洞察的查询后,你会得知以下查询因数据溢写而应被优化。

深入查看指标,你可以看到数据非常大,以至于其中一部分在连接的 Shuffle 过程中溢写到磁盘。

使用“Optimize” Genie Code 有助于识别出,这些表中实际上都包含一个占据大部分数据量的大列。这些列实际上并不需要,可以删除,这使查询变成了以下内容。

使用优化后的查询重新运行,将运行时间缩短至原来的 ¼。

最后,假设我们尝试查询 orders 表,并为每个客户的每个订单附加一个序列号。

该作业的查询性能洞察显示,Shuffle 上的数据倾斜导致执行时间比预期更长。有问题的查询如下:

Genie 识别出了问题——当你为 row_number() 函数按 customer_id 对数据分区时,该列出现了数据倾斜。

按 customer_id 统计计数显示,customer_id=0 的行数远多于其他值。customer_id=0 的行表示无法分配给客户的订单,因此尝试为它们生成客户内订单序列没有意义。新查询只是将它们的序列设为 NULL。

这将运行时间缩短至原来的 ⅛。

就是这样!查询性能洞察帮助我们在四种不同场景下提升了作业性能,而无需手动确定最佳处理方案。取而代之的是,它借助 Genie Code,只需点击一下按钮就能提出优化后的代码。

1. 什么是 Query Performance Insights?

Query Performance Insights 是一项自动化建议功能,它会标记出有优化潜力的查询,并就如何修复给出建议。Databricks 会直接在 UI 中呈现这些建议(以红色灯泡图标显示),因此你无需自己翻查底层指标,就能发现存在问题的查询。Query Performance Insights 现已正式发布(GA)。

2. 它们只适用于作业,还是也适用于交互式查询?

原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。

查看英文原文(Diagnosing serverless job performance: A practitioner's guide to query performance insights)

Verifying the problem: Use the job's run breakdown to confirm that query execution is the actual bottleneck.

Dive into four hands-on troubleshooting scenarios: data volume increase, inefficient query shape, data spill, and data skew.

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:

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.

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. :

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

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.

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:

Read Query Performance Insights to understand details about which insights are currently covered.

See Query Performance Insights in jobs to learn how the insights and metrics roll up into the Jobs UI.

Check out Diagnosing job performance for a full explanation of the execution breakdown to confirm and how to optimize the other parts of a job run beyond query execution.

Get the latest posts in your inbox

原文为英文,正文由自动抓取并保留原文。

本文由采集器自动同步

原文发布于 Databricks Blog(Diagnosing serverless job performance: A practitioner's guide to query performance insights),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。

阅读原文 ↗