Home / Databricks on Azure

Databricks on AzureTroubleshootingAdvanced

Reading a Databricks SQL query profile on Azure - spill, missed pruning, skew and Photon fallbacks behind slow warehouse queries

Use the query profile to find why a SQL warehouse query is slow (spill, files not pruned, a skewed join or non-Photon operators) and apply the matching fix, not just a bigger warehouse.

Deepak Bhardwaj8 min read

Applies toAzure Databricks SQL warehouses (serverless and pro), Databricks SQL query history and query profile, system.query.history (Public Preview), Delta Lake tables in Unity Catalog

Symptom

A dashboard tile or scheduled query that used to take seconds now takes minutes. The warehouse is not queuing, other queries are fine, and resizing the warehouse "sort of" helped. Moving up a size doubles the cost. The query profile shows where the time goes, so you can usually fix the cause instead.

This post assumes you already know the slow statement. If you don't, find it in system.query.history first (section 1).

1. Confirm it's execution, not queueing or compilation

Split the duration into its parts before you open the profile:

SELECT statement_id,
       executed_by,
       total_duration_ms,
       compilation_duration_ms,
       execution_duration_ms,
       total_task_duration_ms,
       read_bytes,
       read_files,
       pruned_files,
       spilled_local_bytes,
       read_io_cache_percent,
       from_result_cache
FROM system.query.history
WHERE statement_text ILIKE '%fact_sales%'
  AND start_time >= current_timestamp() - INTERVAL 2 DAYS
ORDER BY total_duration_ms DESC
LIMIT 20;

Column names come from the current schema. The table is in Public Preview, so run DESCRIBE system.query.history on your workspace to confirm them.

Pattern Meaning Go to
total_duration_ms far above execution_duration_ms Waiting for capacity or long compilation Warehouse scaling and cluster count, not this post
High compilation_duration_ms Huge plans: many-way joins, giant IN lists, views of views Simplify SQL, check the Delta log size
pruned_files near 0, read_files high Data skipping isn't working Section 3
spilled_local_bytes > 0 Memory pressure Section 4
total_task_duration_ms / execution_duration_ms is low for the warehouse size Skew or a long single-task stage Section 5

total_task_duration_ms divided by execution_duration_ms is the effective parallelism. If it's close to 1 on a large warehouse, one task is doing all the work.

2. Open the profile the right way

In Query History, select the statement and choose See query profile. Two habits keep the numbers honest:

  • When re-running for diagnosis, turn off the result cache so you measure real execution:
SET use_cached_result = false;
  • Expect a second run to be faster because of the disk (IO) cache. Compare read_io_cache_percent between runs before concluding a change helped.

Switch the graph to show time spent per operator and sort by it. In the top-level metrics, check bytes read, files and partitions read versus pruned, bytes spilled, and the share of task time spent in Photon. Download the profile as JSON before changing anything, so you have a baseline to diff against. Labels move between releases, so map these to whatever your build's UI shows.

3. Files read that should have been pruned

Open the scan node for the large table and compare files read to files pruned. A selective filter that still reads most of the files means data skipping isn't working.

Cause How it shows in the plan Fix
Filter wraps the column: CAST(order_ts AS DATE) = ..., date_format(...), upper(col) Predicate appears only in a Filter above the scan, not in the scan's pushed filters Rewrite as a range on the raw column: order_ts >= '2026-11-01' AND order_ts < '2026-11-02'
Implicit type cast (string column compared to an int) cast(col as int) in the pushed predicate Compare using the column's own type
Column has no file statistics (outside the first 32 columns, or stats turned off) Few or no files pruned on that column Move the column into the stats set or cluster on it
Poor file layout: values spread across every file Stats exist but min/max ranges overlap Liquid clustering on the filter columns, then OPTIMIZE
Join filter not propagated Large fact scan with no dynamic pruning annotation Make the dimension filter selective, and keep current table statistics so the optimizer can use dynamic file pruning

Check what the optimizer receives:

EXPLAIN FORMATTED
SELECT region, sum(amount)
FROM main.sales.fact_sales
WHERE order_ts >= '2026-11-01' AND order_ts < '2026-11-02'
GROUP BY region;

Look for your predicate under the scan's data filters. A predicate that only shows up in a later Filter node gets applied after the files are read.

4. Spill: the warehouse is out of memory for this shape

Spill shows up on sort, aggregate, join and window operators as bytes spilled to disk. A little spill is harmless. Gigabytes on one operator usually explain most of the runtime.

Common causes and fixes:

  • The join builds on the wrong side. If statistics are stale, the optimizer may build a hash table on the big side. Refresh them:
ANALYZE TABLE main.sales.dim_customer COMPUTE STATISTICS FOR ALL COLUMNS;

With predictive optimization enabled on the schema, statistics are kept up to date automatically. Check whether it's on before running this by hand.

  • Exploding joins. The rows output by the join operator are much larger than either input. That usually means a many-to-many key, often duplicates in a dimension that should be unique. Fix the data. More memory just makes the bad join finish.
  • Wide window functions. ROW_NUMBER() OVER (PARTITION BY low_cardinality_col ORDER BY ...) puts huge partitions on a few tasks. Partition by a more selective key, or pre-aggregate.
  • SELECT * into a sort or aggregate. Every column is carried through the shuffle. Project only the columns you need before the heavy operator.

Tip

Scale the warehouse up a size only after these fixes, and only if one well-shaped query still spills. Adding clusters (scaling out) helps concurrency. It does nothing for one query's memory.

5. Skew: one task does the work

In the stage or operator details, compare the maximum task time with the median. A maximum many times the median means a skewed key: a null or default customer ID, a single giant tenant, or an "unknown" bucket.

SQL warehouses already run adaptive query execution, which splits skewed join partitions on its own. Where it can't help:

-- Find the hot keys
SELECT customer_id, count(*) AS n
FROM main.sales.fact_sales
WHERE order_ts >= '2026-11-01'
GROUP BY customer_id
ORDER BY n DESC
LIMIT 10;
  • If the hot key is NULL or a sentinel value that never matches, filter it out before the join, or handle it in a separate UNION ALL branch.
  • If the dimension is small, force a broadcast so the skewed side never shuffles:
SELECT /*+ BROADCAST(c) */ f.region, sum(f.amount)
FROM main.sales.fact_sales f
JOIN main.sales.dim_customer c ON f.customer_id = c.customer_id
GROUP BY f.region;

Warning

A broadcast hint on a table that later grows can cause memory errors. Use hints only on dimensions whose size you control. Recheck them when data volumes change.

6. Operators that are not running in Photon

The profile marks which operators ran in Photon. If a large share of task time falls outside Photon, look for the first non-Photon node in the plan. Everything above it may also fall back. Typical triggers are Python UDFs, some less common built-in functions or data types, and certain unsupported expressions. Names and coverage vary by runtime version, so check the release notes for your warehouse channel.

Fixes, in order of preference:

  1. Replace the UDF with built-in SQL functions.
  2. Move the non-Photon expression as late in the query as possible (after aggregation), so it handles fewer rows.
  3. Precompute it at write time into a column of the table or a materialized view.

Symptom to fix map

Profile shows Most likely cause First fix
Files read ≈ total files Function on filter column, no stats, bad layout Rewrite predicate; cluster on filter columns
GBs spilled on a join Stale stats, duplicate keys ANALYZE TABLE; dedupe the dimension
Max task ≫ median Skewed key Filter nulls/sentinels; broadcast small side
Low Photon share UDF or unsupported expression Built-in functions; move expression later
Fast second run only Disk cache Measure with cache state in mind

Prevent it next time

  • Turn on predictive optimization for production schemas so statistics and compaction stay current.
  • Save the profile JSON for your top 20 dashboard queries. After a runtime channel change, compare read_files, pruned_files and spilled_local_bytes from system.query.history against that baseline.
  • Add a review rule: no functions on clustered or partitioned columns in WHERE clauses.
  • Track spilled_local_bytes > 0 per query over time. A rising count warns you before users notice.