Skip to main content
πŸ“˜ Complete PDF

PySpark Certification Practice Test 11

Practice Test 11

Question 1 of 20
πŸ“‹ Multiple Choice
Question 201
A PySpark job joins a large 4 TB Sales fact table with a small 15 MB Product Lookup dimension table. The Spark UI indicates a massive shuffle exchange stage and long execution times during the join. What is the BEST optimization to eliminate the shuffle and accelerate the join?
Collect()
βœ“
Broadcast Join
Cross Join
Coalesce(1)
Answer
βœ… B. Broadcast Join
Explanation

1. Direct Question Answer & Core Concept βœ… B. Broadcast Join

Core Concept: A Broadcast Hash Join replicates small dimension tables to all worker executors, completely eliminating expensive network shuffle of the multi-terabyte fact table.

2. Deep-Dive Analytical Explanation By default, when joining two DataFrames on an equality key, Apache Spark uses a Sort Merge Join (SMJ) or Shuffle Hash Join. This requires hashing both datasets across the network by the join key (a wide transformation), causing massive disk I/O, network bandwidth saturation, and serialization overhead. Because the Product Lookup table is only 15 MB, it easily fits within executor memory (default threshold `spark.sql.autoBroadcastJoinThreshold` is 10 MB, or can be forced with `broadcast(lookup_df)`). With a Broadcast Join, the driver collects the small 15 MB table and broadcasts it read-only to every executor node. Each executor performs a localized hash join against its existing partition of the 4 TB Sales table with zero network shuffling.

3. Comprehensive Option Analysis & Distractor Dissection - **Collect():** Fetches data to driver memory; executing collect on the sales table will crash the driver with an OutOfMemoryError. - **Cross Join:** Computes a Cartesian product ($M \times N$), creating trillions of rows and catastrophic cluster failure. - **Coalesce(1):** Forces the entire 4 TB dataset onto a single executor core, instantly crashing that node.

4. Architectural & Execution Context / Physical Execution In a Broadcast Hash Join (BHJ), the driver coordinates an HTTP/BitTorrent broadcast of the small table's serialized hash table to all executor JVM blocks. During task execution, worker threads read local memory references without transmitting sales partitions.

5. Practical Code Example

/* Implementing Broadcast Hash Join in PySpark */
from pyspark.sql.functions import broadcast

# Sales Fact (4 TB) joined with Product Lookup (15 MB)
sales_df = spark.read.parquet("s3://lake/fact_sales/")
products_df = spark.read.parquet("s3://lake/dim_products/")

# Explicit broadcast hint eliminates shuffle stage
optimized_df = sales_df.join(
    broadcast(products_df),
    sales_df["product_id"] == products_df["product_id"],
    how="inner"
)

# Verify execution plan shows 'BroadcastHashJoin' instead of 'SortMergeJoin'
optimized_df.explain()

6. Key Takeaway Always broadcast small lookup tables (under ~100MB) to completely eliminate wide network shuffles on large fact tables.

7. Best Practice & Production Recommendation Increase `spark.sql.autoBroadcastJoinThreshold` (e.g., to 64MB or 128MB) or use explicit `broadcast(df)` hints when dimension tables are known to fit comfortably in executor off-heap memory.

8. Real-World Engineering Scenario A retail ETL pipeline took 3 hours to join daily transactions with store metadata. Inspecting the Spark UI revealed 95% of stage time was spent in shuffle write/read. Adding `broadcast(store_df)` reduced the runtime from 180 minutes to 7 minutes.

9. Common Exam Trap / Anti-Pattern / Misconception Broadcasting a table that exceeds executor memory or driver memory; broadcasting a 10 GB table will crash the Spark driver JVM during table serialization.

10. Related InsightfulSaga Tutorial Master these concepts in [Join Optimization Techniques in PySpark β€” Broadcast, Skew Handling & Efficient Joins](/tutorials/pyspark/join-optimization-pyspark).

Answered: 0 / 20
Question Navigator (Test 11)0% Answered