Skip to main content
The explain() function in Spark is used to display the execution plan of a DataFrame or Dataset operation. It provides detailed information about how Spark will execute a query, including the logical and physical plans. This is particularly useful for debugging, optimizing performance, and understanding the underlying execution process.

1. Syntax

PySpark:
Spark SQL:

2. Parameters

  • mode (optional): Specifies the level of detail in the execution plan. Options include:
    • "simple": Displays only the physical plan (default).
    • "extended": Displays both the logical and physical plans.
    • "codegen": Displays the physical plan and the generated code (if applicable).
    • "cost": Displays the logical plan with cost-based optimization details.
    • "formatted": Displays a split output of the physical plan.

3. Key Components of the Execution Plan

  • Logical Plan: Represents the high-level transformation steps (e.g., filters, joins, aggregations).
  • Physical Plan: Represents the low-level execution steps (e.g., scans, shuffles, exchanges).
  • Optimized Logical Plan: Shows the logical plan after Spark’s Catalyst optimizer applies optimizations.
  • Parsed Logical Plan: Shows the initial logical plan before optimization.

4. Examples

Example 1: Simple Execution Plan

PySpark:
Spark SQL:
Output:

Example 2: Extended Execution Plan

PySpark:
Spark SQL:
Output:

Example 3: Codegen Execution Plan

PySpark:
Spark SQL:
Output:

Example 4: Formatted Execution Plan

PySpark:
Spark SQL:
Output:

Example 5: Explaining a Join Operation

PySpark:
Spark SQL:
Output:

Example 6: Explaining an Aggregation

PySpark:
Spark SQL:
Output:

5. Common Use Cases

  • Debugging complex queries.
  • Identifying performance bottlenecks (e.g., shuffles, expensive operations).
  • Verifying that optimizations (e.g., predicate pushdown, join reordering) are applied.

6. Performance Considerations

  • Use explain() to analyze and optimize queries, especially for large datasets.
  • Look for expensive operations like shuffles, wide transformations, or full table scans.

7. Key Takeaways

  1. The explain() function is used to display the execution plan of a DataFrame or Dataset operation.
  2. It supports multiple modes (simple, extended, codegen, cost, formatted) for different levels of detail.
  3. Using explain() is a metadata operation and does not impact performance.
  4. In Spark SQL, similar functionality can be achieved using EXPLAIN.