Skip to main content

How to Diagnose Slow Queries in Databricks: Query Profile, Photon, Spill and Genie Code

Summary

  • Root-cause framework: check Photon percentage, spill and data pruning to diagnose slow queries.
  • Performance categories: query, data layout, concurrency or system constraints impact query time.
  • Performance tools: system tables, Genie Code and Perf Toolkit suggest and analyze optimization fixes.

How to Diagnose Slow Queries in Databricks: Query Profile, Photon, Spill and Genie Code

Watch: How to Diagnose Slow Queries in Databricks: Query Profile, Photon, Spill and Genie Code
Talk by Shannon Barrow, Principal SA and Practice Lead, Databricks
Diagnosing a slow Databricks query starts with one recursive question, where is the time going, and then placing the problem in one of four categories: the query itself, data layout, concurrency, or system-level constraints. This video gives a repeatable root-cause framework and shows how to read the query profile properly: check the Photon percentage first, treat spill as a symptom rather than a cause, compare bytes pruned with bytes read, use task time versus wall-clock time to judge parallelism, and watch for exploding rows.
It shows how to read the query profile like a pro: check the Photon percentage first, treat spill as a symptom of another issue, compare bytes pruned to bytes read, use task time versus wall-clock time to judge parallelism, and watch for exploding rows. One example traces a join that spilled 18 GB because the default 10 MB broadcast threshold forced a shuffle. You will also see how system tables help admins find the worst queries, how performance insights and Genie Code Agent Mode use AI to suggest fixes, and a Perf Toolkit that analyzes DBSQL queries and Spark jobs, compares before and after, and lets you ask Genie about the results.

Chapters

FAQs

Where does the time go in a slow query?

Slow queries fall into four categories: the query itself, data layout, concurrency or system constraints. Start with the query profile to see Photon percentage, spill, task time and data pruning statistics.

What does spill mean in the query profile?

Spill is usually a symptom of another issue, like an inefficient join strategy. This video shows an example where a join spilled 18 GB because the default 10 MB broadcast threshold forced a shuffle.

How do I tell if my query is parallelizing?

Compare task time with wall-clock time. If task time is much higher, parallelism is working. If they're similar, you have skew or too few shuffle partitions.

Can Genie help me optimize queries?

Yes—performance insights and Genie Code Agent Mode use AI to suggest fixes, and the Perf Toolkit analyzes DBSQL queries and Spark jobs, compares before and after, and lets you ask Genie about the results.

Full transcript

00:00 Introduction

All right, so my name is Shannon Barrow and we're going to talk performance optimization today. So we've only got 20 minutes. I'm going to jump right in and for a slide that you've all seen at least a dozen times so I don't get sued. And of course, whether you love or hate today's session, feedback is appreciated. So quick intro on me before we dig in. I'm Shannon Barrow. I've been at Databricks for seven years. I joined in March of 2019.
I was about employee 500. I'm a principal solutions architect here and I do spend most of my time in the performance arena of data engineering, data warehousing, and so on. So many of us have been in this position where you or one of your analysts in the organization just aren't happy with query performance or your jobs aren't even completing at all. So luckily Databricks is exposed as a tremendous amount of telemetry and query profile data that allow you to debug and troubleshoot these queries.
So the steps you want to do seem obvious. You optimize your queries or your data. You analyze whether they perform better. You right-size the warehouse or the cluster. But what's the fourth I's here? We've got optimize, analyze, right-size. I think the ultimate next one is criticize because it's still not working right. So performance issues are rarely isolated. So let's discuss some repeatable methods that I use for finding real bottlenecks and even some newer AI tooling that can help make this easier for us all.

01:37 Four places a performance problem lives

So I like to believe that every performance issue lives in one of these four categories, sometimes at the intersection of two. So the query, you know, this is everything about the query and resulting execution plan itself. You know, does it stay in Photon? Which join strategy was used? Were you able to broadcast instead of shuffling huge data sets? What about the data layout? How were the parquet files stored at rest?
Did you use liquid clustering or partitioning? In which columns did you use? Are the correct file sizes set for the table based upon the clustering or partitioning columns chosen? And you want to find the optimal size that reduces the amount of data, I'm sorry, reduces the amount of total files that you need to read, but also keeps the data co-located as much as possible. It's a balancing act. do you have column stats on the table?
These are statistics that help optimize the catalyst optimizer for the initial query plan. So you don't have to lean on AQE as much. They're different from the delta stats, which help identify which files to read. These are the query plan itself that it's trying to plan out. So likely predictive optimization in Databricks automatically collects this for you and in a much more efficient way than manual analyzed table commands.
So as long as you're turning on PO, you should be fine and this should already be there for you. But what about concurrency? How many jobs or queries in the cluster or warehouse are you serving at once? This can affect how long a job or query sits in a queue waiting for resources and waiting to get executed. So ideally there's no queue time at all and it just gets launched when you send it. And system, driver, network, cloud fetch, metadata.
This is all the background operations that you the user usually don't have much control over though. So here's the method, you know one question asked recursively, where's the time actually going? So take a stroll down this decision tree with me, it's a very simplified tree to introduce this pattern. You know, execution is, so I'm sorry, the total query is 14 seconds across startup, execution and results return, but the execution time is where we see 12 of those 14.

03:23 Where is the time going: a root-cause decision tree

And within that execution we see that there's various operators. You've got scans, aggregation, sorting, you've got shuffling and joining, but the join is is nine seconds. But what is this join, but why is this join? I'm sorry two-thirds of the total runtime of this query. You know, is there too much data? Did it pick the wrong strategy? Is there skew? Is there spilling? You know, so here we could find the culprit. It's a spill, but why is it spilling? and after further review that it's spilling 18 gigabytes.
So the root cause is that the broadcast threshold is default by 10 megabytes and here it's doing a shuffle hash join when a broadcast would have worked. So the solution would have been to use a broadcast tent or raise the threshold. So I'm not going to go through everything on the slide. This is kind of the one that you take a picture of, okay? But there's three patterns I will call out quickly. So there's files pruned equals zero.

04:27 Common patterns: shuffle vs broadcast joins, spill, Photon

So that's almost always a data layout problem or a missing filter or you're missing a broadcast that would have allowed for dynamic file pruning. Spilling is the next one and take a moment on that one. It could be caused by a number of reasons even potentially by almost all of these other symptoms you see here. It's usually one of the most important metrics to review since it can have a huge negative impact on performance.
It's actually usually the symptom of a completely separate problem. So it's usually the flag that you see that you have a different problem. There's multiple paths to remediation on it, starting with determining whether the query operators are ideal. So you have exploding rows, whether the query stays in photon or not, whether your data is skewed, which is causing some partitions to spill. And then finally, if there's no way to tune any of the above, whether it's just generally a big data set, and you're doing some complicated operations.
And so you just need to upsize your cluster or warehouse, right? So the last one I want to point out is Photon. So this one is actually usually the first one I go to. It's just as important as a spill metric. Keeping jobs in Photon could be the very first goal of your queries because when, from the moment it falls out of Photon, it then becomes the culprit of so many other downstream issues in your job. So how do we read the query profile like a pro?

05:52 How to read the query profile

So this, like, I hope everybody's familiar with this UI, right? You know, for those that aren't, I'm going to give you the 60 second summary here and clips notes on how to read it. So first up is the query details. This is what you see when you first initially land on the page. All right. So as you open the query profile, it's here on the top left. And then below that, you've got your top operations panel. So this panel is going to list every operation in your query in descending order of task time and allows you to quickly find the most problematic operations in your query.
This third one is performance insights. Sometimes you won't see it in your query profile because there aren't any. But in fact, that actually suggests if you could get your queries to not have this box, it's actually your goal. But this little icon doesn't appear on your queries at all if you don't have any query insights. So it's a relatively new addition. And what this does is it tells you if there's some sort of smoking gun.
There could be a case where you want to leverage the cluster of partition columns it suggests or better, you don't want it there at all, but like sometimes you do want to take the insights it gives just because it'll tell you, hey, this table's clustered or partitioned by this column, you should consider using it, right? And then this last tab on the rail is the query text window. So this is just going to show you the exact query or command sent to the actual warehouse or to your job.
It helps you whenever you're just trying to tune the query itself. And of course the execution graph on the right. So this is the DAG of all the stages in your query job that need to be executed along with a plethora of other metrics and telemetry about each one. So again this is the summarized look and even each one of these little boxes has its own extended huge amount of telemetry that you can see. It's much more than any other competitor has which often is one of the reasons why they say Databricks is complicated because we do expose this type of stuff for you right.

07:40 Query profile checklist: Photon, spill and data pruning

I'd rather be able to see it than not see it at all right. So let's talk about eye discipline. I call it eye discipline. It's like hey where do your eyes go when you have so much information. Where do you start? So query profiles could overwhelm you with the sheer amount of information being conveyed. So I end up having to review thousands of query profiles per year across dozens of customers, our own internal benchmarking and other applications.
So here's the order in which I tend to read profiles to find performance signals as quickly as possible. So first of all there's the photon percentage. So if a job falls out of photon your first task should be to find out why and determine if that's something you can remediate? You know, is it a UDF? Is it some sort of non-deterministic piece of code that you've written, right? You know, if you can remediate that, all of the other performance issues could easily stem from the query following out of Photon initially.
So if you can fix this, oftentimes you'll see all your other issues go away, okay? So your first goal should be to try to fix this. Second thing is spill, okay? Spill is actually a symptom of other issues, all right? And think of it like almost like the inverse of the photon issue where photon will cause you other issues. This one is the result of a different issue. And so it's kind of like your smoking gun in case there's something else.
Right. So if you see the spill, trace it back to what actually causes it. All right. And sometimes spill will be inevitable. OK. And so, you know, what you want to do is make sure if it's going to spill, it's only doing it in small amounts. You know, oftentimes they'll see where I'm getting spilled. but then even when I increase the warehouse or the cluster size, it may be still linear in total performance or cost. And so it's really, hey, maybe a little bit of spill sometimes is better than trying to upgrade the whole warehouse size.
And so sometimes you might be okay with it. Okay. But trying to get it down as much as possible and ideally zero would be ideal. Okay. So let's talk about bytes prunes versus bytes read. You know, how's this ratio looking? Are you able to prune most of the data? If not, how can you reduce the amount of data read by fixing the data layout, ensuring filters are in the right queries where possible, or optimizing the query itself to get more broadcast joins so you can get some of the dynamic file pruning?
So think of this like a ratio, all right? Again, if you're still reading the entire table, but it's only 10 gigabytes, but if I'm reading 10 gigabytes out of a 10 terabyte table, that's actually a pretty good percentage, right? So task time versus wall time. So the wall time is actually above. You can't see the wall time because it's cut off the screen, but task time is right here. And this will tell you how much parallelization you got.

10:05 Task time and parallelism: skew and shuffle partitions

So if you see a query that says you have 10 seconds of task time, but your total wall clock time is 10 seconds, that means you only averaged one core that you were using through the entire execution. So what you want to do is, hey, if I'm running an extra large warehouse, or I'm running something really big, but I'm seeing a very small ratio here, hey, I'm only getting like two to one or three to one and so on, you want to make sure like what's causing this parallelization to go down.
All right. So if the ratio is low, either it's fighting for resources or the number of shuffle partitions isn't high enough. This could be caused by a number of reasons. And like I said, restricted aggregations like dense rank. So dense rank brings everything down to a single partition. So there's nothing, not much you can do about that. Or maybe there's data skew or too few files without a repartition or shuffle after reading.
So let's talk about rows red versus the rows written. So this one's simple, right? Anytime you see less rows read than you wrote, then somehow you exploded the data. If I only read a million rows, but I wrote 10 million, then at some point it exploded, right? So exploding data, especially if it's several fold, can cause issues because the runtime won't implicitly increase the number of partitions. So if you exploded your data for a single partition of data, you could take that 100 megabyte partition until one gigabyte partition because you 10x'd it, for example.

11:08 How exploding rows hurt performance

And that's just too big for a single partition oftentimes. So it's not intention. Sorry, sometimes also it's not the intention of users. If you're seeing exploding data, well, maybe you just didn't do a distinct or a group buy and you're now getting exploded data because you just you didn't take care of the fact you had duplicates in some cases, right? So one thing you could do to help with this, right, is, you know, if you're not doing, well, one thing you could do is you could try to do the explode, like if you're doing an explode command and you're trying to make, you're purposely generating new rows, try to do it after all of your other processing and tasks have to be done because if you do that first and then now I'm going to join it to another table, well now that's just a huge piece of data I have to shuffle.
You may as well just do it after. And the second thing you could do is maybe you're about to do the explode to help with the fact that it will, it could potentially increase that partition size too much. Maybe you do a repartition first to get the partition size is really small, then you do the explode and now you're back to normal partition size again. All right. And then the last one here is the top operator. So once you get through all the summary findings we just went through, which is the aggregated total of everything that's running, then you go into the execution graph to find the longest running stages and you trace backwards from there.
So the top operators tab already has all those stages sorted by the longest running stages at the top. So you can start working your way back to find the bottleneck. Yeah. You know, sometimes as an admin, you may not even know that there's a problem or where there's a problem or which queries are causing problems. So remember, systems tables can help you easily find the cases where there's some problematic queries. And so this is just an example to get some of the worst offenders.

12:57 Finding problem queries with system tables

Here's for most gigabytes read. But you can use it to collect other metrics as well, like per query duration, queue time, and so on. Oh, no, my side went away. What happened? Okay, here we go. Pull this out. Over to the other tab. That was weird. Other previous display. Other pieces play. Sorry about that, guys. I didn't get a chance to rehearse this, so I definitely need the notes. I think the notes are great. I just didn't get a chance to rehearse.
I think it went to sleep, that's why. So AI tooling. What's funny is I had this entire presentation planned about I'm going to go geek out with everybody about how to go and tune queries. And then our product team says, yeah, we'd like to put as much AI into everything as we can. So can you talk about AI tooling as well? So we do actually have some AI pieces built in to try to help optimize. Some are more implicit, some are more explicit.

14:16 AI-assisted tuning with performance insights and Genie Code

So we have two very practical AI-assisted paths for SQL performance inside Databricks. So there's an easy AI-assisted capabilities for analyzing performance. So when a query runs, Databricks can surface performance insights directly in the query history and the query profile. And if there's an actionable query fix, the optimized flow opens Genie code and rewrites the SQL for your approval. So this is not just like a here's a graph, good luck. the product's increasingly telling you what is slow and helping you take the next step.
So we talked about like the performance insights when we went through the graph, right? So now we're trying to surface up to you and putting little like notifications that, Hey, look, you can actually get some better performance if you try these things. Again, sometimes it's hit or miss because it's going to surface it, whether you're able to use it or not. Hey, this, my table is clustered by this or partitioned by this, but I still need to read the whole thing.
Then it doesn't help me. Right. But in some cases, maybe you just don't know. And so it's trying to like pull that up for you. So on the right side though, we have Genie Code Agent Mode. This is a more general AI workflow. It can autonomously plan multi-step tasks, generate and run code, use outputs to improve results, and work across notebooks in the SQL editor within your existing Unity Catalog permissions. So the takeaway is Databricks is moving from AI that helps you write SQL to AI that can also help you diagnose why it's slow and accelerate the path to a better version without introducing a separate tool chain or governance model.
I'm going to use this to change slides now. Okay, last one. I want to take a moment because I actually have some amazing essays out of Japan who helped create this new performance toolkit. So this was actually put out by two of our essays out of Japan. I'm not going to, maybe I should, Mitsuhiro and Akihiro, I think is how it's pronounced. I didn't want to butcher it. So why do they build this tool? So first, query profiles and especially the Spark UI can be complex, right?

15:50 Perf Toolkit for before and after analysis

And it can require DeepSQL and Spark internals knowledge and expertise to help interpret. There's no automated way to compare the before and after, even, you know, nothing about what I showed you today even compares what the results were before and after. So when you tune a query, you need to track manually. Also, tuning knowledge is siloed. Sure, some Databricks essays can come help optimize problematic queries, or maybe you have a resident, you know, performance expert within your organization.
But you know, what about the customers who want self-service tuning, people who maybe aren't an expert at performance tuning? So the Perf Toolkit was built to help try to address all these. And this is where I have a video here I'll show. So there's no audio to it, but it's just a quick demo to show it like end to end. But you know, the tool does four things. It can help analyze DB SQL queries. It helps analyze Spark jobs.
It compares before and after. So you'll say, hey, this is what the results were before you ran this. And this is what it was after we made the changes of the performance tuning that we do. And then also lets you ask questions in natural language using Genie about what the results are telling you. So essentially the toolkit bottleneck analysis for all jobs, including, I'm sorry, it supports bottleneck analysis for all jobs, including classic compute, serverless SQL, serverless jobs, and SDP, and then compares the results while giving users the ability to ask those natural language questions about the results.
So the end goal is simple. You know, we want customers to be able to tune their own queries and Spark jobs without feeling like they need additional resources, right? And this is all, again, like the AI tooling you see here, a little bit more of the hands-on, I'm going to give you all the tools to help actually fix queries that you feel like have bad performance, but there's so much we've built in the last year that are tied to AI tooling that helps automate and help improve without you having to get involved at all, right?
So that's why you see queries maybe over time go from 20 seconds to 10 seconds to 5 seconds, just because the runtime itself uses AI to help improve itself. So I'll let this finish and then we can move on. So this is the Spark job analysis it's doing here. And then you can create report here. Awesome. And as I said earlier, again, Japanese, so apologies for the Japanese text here, but what it does, it actually now analyzes every piece of the bottlenecks and then gives you actual suggested patterns to help fix, and then you can ask questions about it too.
So I think it's really cool. so in closing databricks exposes a plethora of tools to help optimize you know this session deserves a whole lot more time than 20 minutes of course all right but you know at the end of the day you know find the issues do systematic root cause analysis and validate the fix that you applied actually works right all right i got one minute left so thanks guys Thank you.

Learn more about the Databricks Data and AI platform.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.