Sub topic 7
Finding bottlenecks in the Query Profile
This chapter assumes you can read basic SQL and know what Spark stages, tasks and shuffles are. You should also have run EXPLAIN on a query, as covered in the previous chapter. EXPLAIN shows the plan Spark intends to run. The Query Profile shows what actually happened when the query ran.
What the Query Profile shows
The Query Profile is a page in Databricks that displays one executed query as a graph of operators. Each box is an operator, such as a scan, a join or an aggregation. Each box carries measured values for time, rows and memory. You can open it from the query history of a SQL warehouse, or from a query's details in a notebook or job.
Photon operators are labeled with a Photon prefix, for example "Photon Scan" or "Photon Shuffle Hash Join". An operator without that label ran in standard Spark. This is the quickest way to find where a query fell back, which the previous chapters called out as a common cause of lost speedup.
A repeatable reading order
Do not start by guessing. Work through the same questions each time.
- Find the operators that take the largest share of total time. Start your investigation there.
- Check how many rows each operator reads and outputs. A scan that reads billions of rows and a filter that keeps a few thousand points to missing data skipping.
- Check shuffle metrics. Large amounts of data written and read between stages point to an expensive join or aggregation.
- Look for spill. Spill means an operator ran out of memory and wrote data to disk, which slows it sharply.
- Look at the join type. A sort-merge join over a small table suggests a broadcast would be cheaper.
Matching symptoms to fixes
| What you see | Likely cause | Where to go next |
|---|---|---|
| Scan dominates time, few files pruned | Poor layout or no clustering on the filter column | `OPTIMIZE`, Z-order or liquid clustering |
| Many small files read | Small files problem | `OPTIMIZE` |
| Large shuffle before a join | Join strategy not suited to table sizes | Broadcast hint, fresh statistics |
| Spill to disk | Skew or too little memory per task | Adaptive query execution, larger nodes |
| Non-Photon operator in the middle | Unsupported feature or Python UDF | Built-in functions |
Common mistakes
- Tuning an operator that takes only a small share of total time. Fix the largest cost first.
- Judging by one run. The first run may include cold cache effects, so repeat the query before drawing conclusions.
- Treating the absence of the Photon label as an error. Some operators have no Photon version, and that is fine if they are cheap.
Exercise
A query is slow. In the Query Profile, the scan operator accounts for most of the time, reads every file in the table, and prunes none, although the query filters on customer_id. Which operator metric told you this, and what is a sensible next step?
Show answer
The files read and files pruned metrics on the scan operator showed that data skipping did nothing for this filter. A sensible next step is to cluster the table on customer_id, for example with OPTIMIZE orders ZORDER BY (customer_id) or liquid clustering, then rerun the query and compare the profile.