← BuildandTeach Tech Tutorials posts

Published · By Henry Palma

Databricks Data Engineer Associate Exam Prep, Lesson 12: Spark Tuning Basics (Change One Setting, Then Re-measure)

Databricks Data Engineer Associate exam prep: spark.sql.shuffle.partitions, default parallelism, memory and the broadcast join threshold, then re-measure.

Databricks Data Engineer Associate Exam Prep, Lesson 12: Spark Tuning Basics (Change One Setting, Then Re-measure)

Exam-style question: An orders-to-customers join on classic compute got slow. The query plan shows a sort merge join that shuffles both tables, even though customers is small. Last month a teammate set spark.sql.autoBroadcastJoinThreshold to -1. What should you change? Set the threshold back to a size larger than the customers table (the Apache Spark default is 10 MB), then re-run the same query and compare the time. A value of -1 turns automatic broadcast joins off, so Spark has to shuffle and sort both sides. This post follows Lesson 12 of the Databricks Data Engineer Associate exam prep series, in Section 3 of the August 2026 exam guide, Data Transformation and Modeling (22% of the exam).

Watch on YouTube

Video thumbnail
Video thumbnail

The notebook and cheat sheet are in the A12 folder on GitHub. The PDF cheat sheet is in the same folder. This is independent study material, not official Databricks training.

The exam objective

Section 3 of the exam guide lists this objective: understand the basic tuning parameters (spark.sql.shuffle.partitions, spark.default.parallelism, spark.executor.memory and spark.driver.memory, spark.sql.autoBroadcastJoinThreshold) and re-measure the performance.

The second half matters as much as the first. A setting is a guess until you run the same query again and compare. The two traps are changing a setting and never checking whether it helped, and assuming that more executors will fix a slow stage that is really caused by skew.

Free Edition is partial for this lesson. Free Edition only runs serverless compute, and serverless notebooks and jobs allow only a handful of Spark properties. spark.sql.shuffle.partitions is one of them. spark.sql.autoBroadcastJoinThreshold, spark.default.parallelism and executor or driver memory are not, so those parts are classic (paid) compute only. This post says which is which as it goes. The timings below come from a local Apache Spark 4.0 run with open-source Delta Lake on a 4-core machine, not from Databricks. Your numbers will differ, and that is the point: measure your own.

The four settings

| Setting | What it controls | Default (Apache Spark) | Serverless / Free Edition | |---|---|---|---| | spark.sql.shuffle.partitions | Number of partitions after a shuffle for DataFrame and SQL joins and aggregations | 200 (Databricks serverless default: auto) | Can be set | | spark.default.parallelism | Default number of partitions for RDD operations such as parallelize, reduceByKey and RDD join | Total cores on the executors (at least 2) | Not settable. Serverless does not support RDD APIs | | spark.executor.memory, spark.driver.memory | Memory for each executor process and for the driver | 1g each in open-source Spark | Not settable. Databricks manages serverless memory | | spark.sql.autoBroadcastJoinThreshold | Largest table, in bytes, that Spark broadcasts to every worker in a join. -1 turns broadcasting off | 10485760 (10 MB) | Not settable. Use a broadcast hint instead |

A few details the exam likes:

  • Shuffle partitions vs default parallelism. DataFrame and SQL shuffles use spark.sql.shuffle.partitions. spark.default.parallelism is the RDD setting. Changing the wrong one does nothing for a DataFrame join.
  • Too many or too few shuffle partitions. 200 partitions for a small aggregate means many tiny tasks with scheduling overhead. Too few means huge partitions that can spill to disk. Adaptive Query Execution (AQE), on by default in Databricks, coalesces small post-shuffle partitions at runtime. Setting spark.sql.shuffle.partitions to auto turns on auto-optimized shuffle in Databricks.
  • Memory is set when the compute starts. Executor and driver memory belong in the compute configuration (on Databricks classic compute, mostly the node type you pick), not in spark.conf.set inside a running notebook. The driver JVM is already running by then.
  • Broadcast threshold. If one side of a join is smaller than the threshold, Spark copies it to every worker and skips shuffling the big side. On Databricks, AQE can also switch a sort merge join to a broadcast hash join at runtime when one side turns out to be small (its own threshold, spark.databricks.adaptive.autoBroadcastJoinThreshold, defaults to 30 MB). Raising the threshold too far is risky: a big table copied to every worker can run out of memory.

The scenario: a slow orders-to-customers join

The retail data from Lessons 8 to 11 continues, scaled up so a tuning change is visible. silver.ordersbig holds 5,000,000 practice orders from Nov 1 to Nov 28, and silver.customersbig holds 100,000 customers across TX, CO, OK and NM. The daily report joins them and totals orders and sales by state:

from pyspark.sql import functions as F

def ordersbystate(): o = spark.table("silver.ordersbig") c = spark.table("silver.customersbig") return (o.join(c, "customer_id") .groupBy("state") .agg(F.count("*").alias("orders"), F.sum("amount").alias("sales"))) ```

The report got slower after a shared config change, and the analyst wants to know why. ordersbystate().explain() shows SortMergeJoin, with an Exchange (a shuffle) on both sides. The customers_big table is small, so this join should not need to shuffle 5 million orders.

The checklist: change one setting, then re-measure

  1. Measure the baseline. Run the query several times and keep the median, so one slow run does not fool you. Note the join type in the plan, and the shuffle and spill numbers in the Spark UI (classic compute) or the query profile (serverless, where the Spark UI is not available).
  2. Name one suspect. Use the evidence. Here, a small table is being shuffled instead of broadcast.
  3. Change one setting. Only one. If you change three things and it gets faster, you do not know which one helped.
  4. Re-run the same query on the same data and the same compute, the same number of times.
  5. Compare. Keep the change only if it is faster and the results are identical. Otherwise put the old value back.
  6. Write it down: setting, old value, new value, before and after times.

The reusable snippet times any query the same way every time:

import time, statistics

def timeit(build, runs=5): build().collect() # one untimed warm-up run times = [] for in range(runs): t0 = time.time() build().collect() times.append(time.time() - t0) return round(statistics.median(times), 2) ```

collect() is safe here because the result is four rows. For a large result, write to a table instead, because collect() pulls every row to the driver.

Run it: one setting at a time (classic compute)

On classic compute you can set the threshold in the notebook session:

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", "-1")
print("baseline", time_it(orders_by_state))    # SortMergeJoin

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", "10485760") print("broadcast", timeit(ordersby_state)) # BroadcastHashJoin

spark.conf.set("spark.sql.shuffle.partitions", "8") print("shuffle 8", timeit(ordersby_state)) ```

Local run (Apache Spark 4.0.1, local mode with 4 cores, median of 5 runs after one warm-up):

| Run | One change | Join in the plan | Median time | |---|---|---|---| | 1 | Baseline: threshold -1, shuffle partitions 200 | SortMergeJoin | 1.46 s | | 2 | Threshold back to 10485760 (10 MB) | BroadcastHashJoin | 0.67 s | | 3 | Shuffle partitions 200 to 8, threshold kept | BroadcastHashJoin | 0.55 s |

All three runs returned the same four rows: 1,250,000 orders per state. Run 2 was about 2.2 times faster than the baseline, because the 5 million orders were no longer shuffled. Run 3 helped less, about 1.2 times, because only the small aggregate still shuffled, and fewer partitions meant fewer tiny tasks. On a real cluster with more data, 8 partitions could be too few, which is exactly why you measure instead of copying a number.

What works on Free Edition (serverless)

Serverless notebooks and jobs support setting only these Spark properties: spark.sql.shuffle.partitions, spark.sql.files.maxPartitionBytes, spark.sql.ansi.enabled, spark.sql.session.timeZone, spark.sql.legacy.timeParserPolicy and spark.databricks.execution.timeout. So on Free Edition:

  • You can change spark.sql.shuffle.partitions (its serverless default is auto) and time the query before and after.
  • You can ask for a broadcast with a hint. A hint is part of the query, not a Spark config: o.join(F.broadcast(c), "customer_id") in Python, or SELECT /+ BROADCAST(c) / ... in SQL. Spark prioritizes a broadcast hint even when the table is above the threshold.
  • You can read the plan with explain() and check shuffle and spill in the query profile.
  • You cannot set spark.sql.autoBroadcastJoinThreshold, spark.default.parallelism or executor and driver memory. Those are classic compute settings. On serverless, Databricks sizes the compute, and AQE still switches small joins to broadcast at runtime.

More executors do not fix skew

Skew means one key has far more rows than the others. Say customer 104 places most of the orders. After the shuffle, all of customer 104's orders land in one partition, and one task has to process that whole partition. The other tasks finish early and wait.

Adding executors adds more slots for tasks, but it does not split that one big partition. The stage still waits for the same single task. What helps is splitting the work: AQE skew join handling, on by default in Databricks, splits a skewed partition in a sort merge or shuffle hash join into smaller tasks. AQE treats a partition as skewed only when it is larger than 5 times the median partition and larger than 256 MB (the defaults). If the other side is small, a broadcast join avoids the shuffle, and its skew, entirely.

Practice exam-style question

A data engineer runs a nightly join of orders to customers on classic compute. In the Spark UI, 199 of the 200 tasks in the join stage finish in under a minute, and one task runs for 40 minutes. Most orders belong to one customer. The engineer doubles the number of workers. What is the most likely result?

  • A) The stage finishes in about half the time, because there are twice as many executors
  • B) Little changes, because the oversized partition is still processed by a single task
  • C) The job fails, because the shuffle partitions no longer match the executor count
  • D) Spark doubles spark.sql.shuffle.partitions automatically, which removes the skew

Answer: B. The slow stage is caused by skew. All rows for the heavy customer go to the same shuffle partition, and one task processes one partition no matter how many executors are free. More workers only add idle slots. Check that AQE skew join handling is on (it is by default) and that the partition passes its size thresholds, or broadcast the smaller side if it is small enough. A is the trap in this lesson. C is wrong because the number of shuffle partitions does not have to match the executor count. D is wrong because adding workers does not change spark.sql.shuffle.partitions.

Key takeaways

  • The exam's four settings: spark.sql.shuffle.partitions (DataFrame and SQL shuffles, default 200), spark.default.parallelism (RDDs), executor and driver memory (set when the compute starts), and spark.sql.autoBroadcastJoinThreshold (10 MB default, -1 turns broadcasting off).
  • A small table that gets shuffled instead of broadcast is a classic slow join. Check the plan for SortMergeJoin vs BroadcastHashJoin.
  • Re-measure every change: same query, same data, several runs, compare the median, and confirm the results match.
  • More executors do not fix skew. One oversized partition is still one task. AQE skew join handling or a broadcast join does.
  • Free Edition (serverless) lets you set spark.sql.shuffle.partitions and use broadcast hints. The threshold, default parallelism and memory settings are classic compute only.
  • Memory trick: one knob at a time, then measure again.

Next lesson (Lesson 13): Lakeflow Jobs: tasks and dependencies.

Check your understanding

1. In open-source Apache Spark, what is the default value of spark.sql.shuffle.partitions?

  • A) 8
  • B) 64
  • C) 200
  • D) The number of executor cores

2. What does setting spark.sql.autoBroadcastJoinThreshold to -1 do?

  • A) Broadcasts every table in a join
  • B) Turns off automatic broadcast joins
  • C) Uses the default 10 MB threshold
  • D) Lets AQE pick the threshold

3. A DataFrame join's post-shuffle partition count is wrong for the data size. Which setting controls it?

  • A) spark.default.parallelism
  • B) spark.executor.memory
  • C) spark.sql.shuffle.partitions
  • D) spark.driver.memory

4. Which of these Spark properties can you set in a serverless notebook, such as on Databricks Free Edition?

  • A) spark.sql.shuffle.partitions
  • B) spark.sql.autoBroadcastJoinThreshold
  • C) spark.executor.memory
  • D) spark.default.parallelism

5. An engineer raises the broadcast threshold and the job feels faster. What should they do next?

  • A) Also raise shuffle partitions and executor memory while they are at it
  • B) Re-run the same query on the same data several times, compare the median with the baseline, and check the results match
  • C) Keep the change, because a faster feeling means it worked
  • D) Add more executors to confirm

Answer key

  1. C. The Apache Spark default is 200. Databricks serverless uses auto instead.
  2. B. -1 disables automatic broadcasting, so joins shuffle both sides unless a hint asks for a broadcast.
  3. C. DataFrame and SQL shuffles use spark.sql.shuffle.partitions. spark.default.parallelism applies to RDDs.
  4. A. It is one of the few properties serverless allows. The others are classic compute settings.
  5. B. Change one setting, then re-measure the same way, so you know the change, not luck, made the difference.

Have an idea, question, or feedback?

We'd genuinely like to hear from you.