Sub topic 7
Finding bottlenecks in the Query Profile
This chapter assumes you can read a basic query plan (see the previous chapter on EXPLAIN), and that you know what stages, tasks and shuffles are. You also need a Delta table and a SQL warehouse or cluster you can run queries on.
A query plan tells you what the engine intended to do. The Query Profile tells you what happened when it ran. It is a per-query view of the operators that executed, with timings, row counts, and data volumes. You use it to find the one operator that dominates the runtime, then decide which fix fits.
Opening the profile
- SQL warehouse: open Query History, select a query, and choose to view the query profile.
- Notebook or job cluster: use the link under a cell's output to open the query details, or open the SQL/DataFrame tab in the Spark UI.
The profile exists only for queries that have finished or are running. Re-run the query if the history entry has expired. Exact button names and metric labels change between releases, so treat the names below as the concepts to look for.
What you see
The profile has two main views.
- Summary or top operators: a ranked list of the operators that spent the most time. Start here.
- The operator graph: a DAG (a directed graph) of scans, filters, joins, aggregations, and shuffles. Data flows from the scans at the bottom to the result at the top. Each node shows its metrics.
Operators that run in Photon are labeled as such, for example with a "Photon" prefix on the name. Operators without the label run in the standard Spark engine. A boundary between the two usually adds a conversion step between columnar and row formats, which costs time.
Metrics that matter
You do not need every number. These cover most diagnoses.
| Metric | Where it appears | What a bad value suggests |
|---|---|---|
| Time spent (share of total) | Every operator | This operator is your target. Ignore the rest first. |
| Files read vs. files pruned | Scan | Few files pruned means data skipping is not helping. |
| Bytes read / rows read vs. rows output | Scan | Reading far more than the filter returns points to poor layout. |
| Cache hits vs. misses | Scan | Low hits on repeated queries mean the disk cache is cold or too small. |
| Rows output | Join, filter | A join that outputs far more rows than its inputs can mean a duplicate-key explosion. |
| Spill size | Sort, join, aggregate | Any spill to disk means the operator ran out of memory. |
| Peak memory | Join, aggregate | High values near the node's limit predict spill. |
| Shuffle bytes written/read | Exchange | Large shuffles are expensive. Check whether a broadcast join could avoid them. |
| Task time skew (max vs. median) | Stage details | One task far slower than the rest indicates data skew. |
A triage path
Work from the biggest cost down. Do not tune something that takes 3% of the runtime.
Worked example
Suppose this query is slow:
SELECT c.region, SUM(o.amount) AS total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2024-01-01'
GROUP BY c.region;You open the profile and see the following.
- The scan of
orderstakes 70% of the time. It read 4,000 files and pruned 0. - The join below it is a sort-merge join, with a large shuffle on both sides.
customersis small, yet it is shuffled too.
The first finding is the biggest. A date filter that prunes nothing means the files are not organized by order_date, or the table has many small files. You fix the layout first:
OPTIMIZE orders;If filters are mostly on date and customer, you can cluster on them. Liquid clustering is declared on the table (an existing table can be altered to use it):
ALTER TABLE orders CLUSTER BY (order_date, customer_id);
OPTIMIZE orders;Re-run the query and compare. If files pruned rises sharply and scan time drops, the fix worked. The join is the next target. A small customers table that was not broadcast suggests missing statistics, so you collect them:
ANALYZE TABLE customers COMPUTE STATISTICS FOR ALL COLUMNS;You then check the profile again. If the plan still shuffles, you can use a BROADCAST(c) hint. Change one thing per run, so you know which change helped.
Reading Photon coverage
When a query is slower than you expect on Photon-enabled compute, scan the graph for operators without the Photon label. Typical causes are:
- A Python UDF in a projection or filter.
- A function or data type that Photon does not support.
- A write path or table format that Photon does not accelerate.
Find the first non-Photon operator and ask what it computes. If it is a UDF, replace it with built-in functions. If it is an unsupported feature, you may be able to rewrite that part of the query, or accept the fallback. The fallback chapter lists the common limits.
Spill and skew
Spill happens when an operator's working set does not fit in memory, so it writes intermediate data to disk and reads it back. Disk is much slower than memory. Two different causes look alike in the profile:
- Too little memory per task. Every task spills a bit. Options: more memory per core, fewer concurrent tasks per node, or filter and aggregate earlier.
- Skew. One or a few tasks spill heavily while others finish fast. In the stage details, the maximum task time is far above the median. A hot key (such as a null or a default value) is a common reason. Adaptive query execution can split skewed join partitions, and the later chapter on it covers this.
Common errors
- Tuning the wrong operator. Always sort by time share first.
- Trusting row counts alone. A scan can return few rows but still read most of the table. Compare rows read to rows output.
- Comparing a cold run to a warm run. The disk cache changes scan times a lot.
- Changing several things at once. You cannot tell which change helped.
- Ignoring stale statistics. A bad join choice is often a statistics problem and not a hint problem.
Exercise
A profile shows a scan that read 3,000 files, pruned 0, and returned 2% of the rows. It accounts for 80% of query time. Name two things you would check or do first, and one thing you would not do.
Show answer
First, check the filter columns and whether the table layout matches them, then run OPTIMIZE (with clustering or ZORDER on the filter columns) to compact files and enable data skipping. Also check for many small files. You would not start by resizing the compute or tuning the join, because the scan is the dominant cost and the layout problem would remain.