Sub topic 1
How Spark Executes a Query
Prerequisites
Assume you know basic SQL, Delta tables, and Databricks clusters or SQL warehouses. You should also know that Spark divides work into stages and tasks.
How Spark Executes a Query
Spark turns a SQL statement or DataFrame operation into a logical plan. It then creates a physical plan that describes scans, filters, joins, aggregations, and writes.
A cluster runs the physical plan in stages. A stage contains tasks that can run without moving data between executors. Operations such as joins and aggregations often create a shuffle, which redistributes rows across the cluster. Shuffles use network, memory, and disk, so they can dominate query time.
Why Columnar, Vectorized Execution Is Faster
Row-based execution processes one record at a time. Columnar execution stores values from the same column together. Vectorized execution processes batches of values in one operation.
This layout reduces interpretation overhead and improves CPU cache use. It also lets the engine read only referenced columns and apply filters to batches efficiently.
What Photon Is and What It Accelerates
Photon is Databricks’ native vectorized execution engine. Standard Spark operators commonly run through JVM-based code. Photon uses compiled native code for supported SQL and DataFrame operations.
Photon often accelerates scans, filters, projections, joins, aggregations, and Delta Lake writes. It does not make every operation faster. The largest gains usually occur in CPU-heavy queries that read and process substantial data.
Turning Photon On
On a classic cluster, select a Photon-enabled runtime and set the Photon option when creating or editing the cluster. In the Clusters API, the relevant setting is runtime_engine:
{
"cluster_name": "photon-demo",
"spark_version": "<Databricks Runtime version>",
"node_type_id": "<available node type>",
"num_workers": 2,
"runtime_engine": "PHOTON"
}Databricks SQL warehouses use Photon by default when the selected warehouse configuration supports it. Check the warehouse settings and query profile rather than assuming every compute type behaves identically.
When Queries Fall Back from Photon to Spark
Photon supports many common operators, but unsupported functions, data types, or execution paths can run in standard Spark instead. Python UDFs are a common example because they execute Python code outside Photon’s native engine.
A query can therefore contain both Photon and Spark operators. Use EXPLAIN FORMATTED and the Query Profile to identify the actual operators and locate fallback boundaries.
Exercise
Why can a query with Photon still be slow?
Show answer
It may spend most of its time shuffling data, scanning too many files, spilling to disk, or executing unsupported operations in standard Spark.