Sub topic 1
How Spark executes a query
Prerequisites
This chapter assumes you know basic SQL (SELECT, JOIN, GROUP BY) and have used a Databricks workspace with a cluster or SQL warehouse. You should also know what a Delta table is and have heard of Spark DataFrames, stages, and tasks. The chapter explains the last two in detail, because every later tuning step builds on them.
From query to plan
When you submit a query, Spark does not run it line by line. It turns it into a plan in several steps:
- The parser reads your SQL or DataFrame code and builds a logical plan, which says what to compute.
- The Catalyst optimizer rewrites that plan. It pushes filters down, drops unused columns, and picks join types.
- Spark chooses a physical plan, which says how to compute it with real operators such as scans, joins, and aggregations.
- The physical plan is split into jobs, stages, and tasks and sent to the cluster.
Photon replaces part of step 4. It runs some operators natively instead of in the JVM. You can only judge what Photon changes if you first understand the shape of this work.
Jobs, stages, and tasks
A task is the smallest unit of work. One task processes one partition, which is a slice of the data. Tasks run in parallel on the executors, which are the worker processes on your cluster.
A stage is a group of tasks that run the same steps on different partitions. A job is the set of stages needed to finish one action, such as a query run or a write.
Shuffles split stages
Some operations can work on each partition alone. Filters and column selection are examples, and Spark calls them narrow. Others need rows with the same key to end up together, such as a join or a GROUP BY. These are wide operations and cause a shuffle. In a shuffle, every task writes its output to local disk, and the next stage's tasks fetch the pieces they need over the network.
A shuffle marks a stage boundary. It is also the most common source of slow queries, because it uses disk, network, and memory at the same time.
Reading a query by stage
Take this query:
SELECT region, SUM(amount) AS total
FROM orders
WHERE order_date >= '2024-01-01'
GROUP BY region;Stage 1 scans the files of orders, applies the date filter, and computes a partial sum per region inside each task. The shuffle then sends all partial sums for one region to the same place. Stage 2 adds them up into the final totals.
Reading less data in stage 1 and moving less data across the shuffle are the two main ways to make this query faster. Most of the tutorial's later chapters work on one of these two goals.
Common errors in the mental model
- More tasks does not mean faster. Thousands of tiny tasks add scheduling overhead. Later chapters on file layout cover this.
- A filter in the query is not always a filter on the read. The scan only skips files if the table layout and statistics allow it.
- Lazy evaluation. Spark builds the plan when you write DataFrame code but runs it only when an action happens. Timing a transformation line measures almost nothing.
Exercise
A query joins two large tables and then groups the result by a different column. Roughly how many shuffles do you expect, and why?
Show answer
Usually at least two. The join needs rows with the same join key together, and the GROUP BY on a different column needs rows with the same group key together. Each requirement causes a shuffle and a new stage. Spark can skip one if a table is small enough to broadcast, or if the data is already distributed correctly.