Sub topic 6
Reading query plans with EXPLAIN
This chapter assumes you know basic SQL (SELECT, JOIN, GROUP BY), have run queries on a Databricks cluster or SQL warehouse, and understand Delta tables and Spark stages. It builds on the previous chapters on how Spark runs a query and what Photon accelerates.
What a query plan is
A query plan is the list of steps Spark will take to answer your query. The optimizer builds it before any data is read. Reading it tells you how much data is scanned, where data is shuffled between machines, which join type was picked, and which steps run in Photon.
Running EXPLAIN
Add EXPLAIN FORMATTED before a query. It prints the physical plan without running the query.
EXPLAIN FORMATTED
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;The output has two parts. The first is a tree that shows the shape of the plan. The second lists each numbered operator with its details.
How to read the tree
Read from the bottom up. The bottom operators read data, and the top operators produce the final result. Each operator passes its output to the one above it.
The diagram below shows the typical shape for the query above.
Operators to look for
| Operator | What it tells you |
|---|---|
| Scan (FileScan or PhotonScan) | Which table is read, and which filters were pushed down |
| Exchange | A shuffle. Data moves across the network, which is costly |
| BroadcastHashJoin | The small side is sent to every executor, so the large side is not shuffled |
| SortMergeJoin | Both sides are shuffled and sorted. Common for two large tables |
| HashAggregate | Aggregation, often in a partial step and a final step |
In a scan, check PushedFilters and PartitionFilters. If your WHERE clause does not appear there, Spark may read more files than you expect.
Finding Photon operators
On Photon-enabled compute, operators that run in Photon have names that start with Photon, such as PhotonScan, PhotonBroadcastHashJoin, and PhotonGroupingAgg. Operators without that prefix run in standard Spark.
A Spark operator sitting between Photon operators often means a fallback. Look at it closely. A Python UDF or an unsupported expression is a common cause.
Common mistakes
- Reading the plan from the top and missing where the data is read.
- Assuming an
Exchangeis a problem. Some shuffles are needed. Look for extra ones. - Comparing plans on different compute types. Plans on a Photon cluster differ from plans on a non-Photon cluster.
- Treating
EXPLAINas proof of speed. It shows the plan, not the run time. Use the Query Profile for real timings.
Exercise
In an EXPLAIN FORMATTED output you see PhotonScan, then a plain Project operator, then PhotonGroupingAgg. What does the plain Project suggest, and what would you check?
Show answer
The plain Project runs in standard Spark, so the query likely fell back from Photon for that step. Check the expressions in the Project for a Python UDF or another unsupported function, and replace it with a built-in function if you can.