icon Join with us for Oracle DBA Training Live Session ENROLL NOW

Azure Databricks Broadcast Join

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
  • 02 Dec, 2026
  • 0 Comments
  • 3 Mins Read

Azure Databricks Broadcast Join

1. What is Broadcast Join?

Broadcast Join is a Spark join optimization in which Spark distributes a small relation to the executors so each executor can join that relation with its local partitions of the larger relation. The common equality-join implementation is Broadcast Hash Join (BHJ).

The main benefit is avoiding a shuffle of the large side solely to perform the join. This can reduce network traffic, shuffle read/write, sorting work, and execution time when one side is genuinely small.

Simple example

Dataset Illustrative size Role
Employee 100 GB / many millions of rows Large fact-like dataset
Department A few MB / a few rows Small dimension-like dataset

 

Spark can broadcast Department to the executors. Each executor probes its local copy of the department hash relation while processing its employee partitions.

2. How Broadcast Hash Join works

  1. The driver plans the query and identifies the relation to broadcast.
  2. Spark executes the small-side plan and prepares a broadcast relation.
  3. The broadcast data is distributed to executor processes.
  4. Each executor makes the broadcast relation available locally and builds/uses the hash structure needed for the join.
  5. Tasks join local partitions of the large side against the broadcast relation.
  6. The join itself normally does not require shuffling the large side.

Note: Broadcast Join does not eliminate every shuffle in a query. A later groupBy, distinct, repartition, or sort may still require a shuffle.

3. Broadcast Join versus Sort Merge Join

Feature Broadcast Hash Join Sort Merge Join
Typical use One side is small enough to broadcast Both sides are large
Large-side join shuffle Usually avoided Usually required unless data is already suitably partitioned
Sorting Not required for hash join Sorts both sides by join key
Memory consideration Broadcast relation and hash representation on executors; driver preparation Shuffle, sort, and execution memory
Common plan operators BroadcastExchange, BroadcastHashJoin Exchange, Sort, SortMergeJoin

 

Do not choose a strategy from source-file size alone. Use estimated statistics and runtime metrics, and consider the in-memory broadcast relation size.

4. Hands-on PySpark example

Create a small employee DataFrame and a department DataFrame.

employees = [
    (101, "Rahul", 10, 50000),
    (102, "Amit", 20, 60000),
    (103, "Priya", 10, 70000),
    (104, "Sneha", 30, 45000),
    (105, "Vijay", 20, 55000),
    (106, "Kiran", 40, 65000),
    (107, "Neha", 30, 75000)
]

emp_columns = ["emp_id", "emp_name", "dept_id", "salary"]
emp_df = spark.createDataFrame(employees, emp_columns)

departments = [
    (10, "IT"),
    (20, "HR"),
    (30, "Finance"),
    (40, "Sales")
]

dept_df = spark.createDataFrame(
    departments, ["dept_id", "dept_name"]
)

4.1 Normal join

normal_join = emp_df.join(dept_df, "dept_id", "inner")
display(normal_join)
normal_join.explain("formatted")

Spark chooses the physical strategy based on statistics, configuration, hints, join type, and runtime conditions. A normal join does not guarantee Sort Merge Join.

4.2 Force a Sort Merge Join for demonstration

spark.conf.set("spark.sql.autoBroadcastJoinThreshold", "-1")

merge_join = emp_df.hint("merge").join(
    dept_df, "dept_id", "inner"
)
merge_join.explain("formatted")

For a suitable equality join, inspect the plan for SortMergeJoin and its Exchange/Sort operators.

4.3 Broadcast the small DataFrame

from pyspark.sql.functions import broadcast

broadcast_join = emp_df.join(
    broadcast(dept_df),
    "dept_id",
    "inner"
)

display(broadcast_join)
broadcast_join.explain("formatted")

Look for BroadcastHashJoin and BroadcastExchange. This tiny in-memory example demonstrates the plan; it is not a meaningful performance benchmark.

4.4 SQL broadcast hint

SELECT /*+ BROADCAST(d) */
       e.emp_id,
       e.emp_name,
       e.salary,
       d.dept_name
FROM employee e
JOIN department d
  ON e.dept_id = d.dept_id;

5. When is broadcast selected?

Apache Spark’s default spark.sql.autoBroadcastJoinThreshold is 10 MB in standard Spark configurations. The setting controls automatic broadcast selection based on estimated relation size; it is not a universal safe-memory limit. Databricks Runtime versions and adaptive settings may add platform-specific behavior.

# Check the current settings
print(spark.conf.get("spark.sql.autoBroadcastJoinThreshold"))
print(spark.conf.get("spark.sql.broadcastTimeout"))

# Example: set automatic threshold to 50 MiB
spark.conf.set(
    "spark.sql.autoBroadcastJoinThreshold",
    50 * 1024 * 1024
)

# Disable automatic broadcast selection
spark.conf.set(
    "spark.sql.autoBroadcastJoinThreshold",
    "-1"
)

A broadcast hint explicitly requests a broadcast strategy and can still request broadcast when automatic threshold-based selection is disabled. Hints are subject to join-strategy and join-type constraints.

 

lets talk - learnomate helpdesk

Book a Free Demo

lets talk - learnomate helpdesk

Book a Free Demo