Lakehouse Execution Plan Analysis Guide (EXPLAIN)
Overview
In SQL query performance tuning, understanding the query execution plan is the first step. Singdata Lakehouse provides the EXPLAIN command to display the logical and physical execution plan of a query, helping you identify full table scans, inefficient JOINs, and missed index usage. This guide categorizes usage by business scenario to help you quickly master how to view and analyze execution plans.
Quick Navigation
- View Basic Execution Plan -- Use EXPLAIN to view the logical plan
- View Physical Execution Plan -- Use EXPLAIN EXTENDED to view the physical plan
- Identify Full Table Scans -- Discover queries not using partitions or indexes
- Verify Index Hit -- Confirm whether a query uses index acceleration
- Compare Plans Before and After Optimization -- Evaluate optimization effectiveness
SQL Commands Covered
| Command | Purpose | Applicable Scenario |
|---|---|---|
EXPLAIN SELECT ... | View logical execution plan | Quick check of query structure |
Prerequisites
The following examples use a simulated order table orders_explain:
View Execution Plan
Use EXPLAIN to view a query's execution plan and understand how the query will be decomposed and executed.
Output Description:
PhysicalTableScan: Table scan, reads underlying data.Filter: Row filtering, appliesWHEREconditions.Project: Column pruning, reads only needed columns.
Identify Full Table Scans
When a query does not use partition columns or index filtering, the execution plan shows a full table scan (TableScan without filter conditions).
Optimization Suggestions:
- If
order_dateis a partition column, addingorder_date >= '2024-06-01'inWHEREcan trigger partition pruning. - Creating a Bloom Filter index on the
amountcolumn can accelerate equality or range filtering.
Verify Index Hit
After creating an index, use EXPLAIN to confirm whether the query automatically uses the index.
Output Changes:
- In the optimized plan,
TableScanwill include aBloomFilterorIndexScanmarker, indicating that non-matching data blocks are skipped.
Compare Plans Before and After Optimization
After optimizing a query (e.g., adding indexes, adjusting JOIN order), compare the execution plan changes.
Evaluation Metrics:
- Whether
TableScanchanges toIndexScanor includes filter conditions. - Whether the
Costestimate drops significantly. - Whether
Statisticsestimated scan row count decreases.
Clean Up Test Data
After completing execution plan verification, it is recommended to clean up test tables:
Important Notes
- EXPLAIN Does Not Execute the Query:
EXPLAINonly generates the execution plan and does not actually read data or modify tables. - Dynamic Plans: Execution plans are generated by the optimizer based on current statistics; plans may differ after table data changes.
- EXTENDED Output Is Large: For complex queries,
EXPLAIN EXTENDEDoutput can be hundreds of lines long. Search by keywords (e.g.,Join,Scan,Filter) for efficient navigation. - JSON Format: Use
EXPLAIN (FORMAT JSON)to output the plan as JSON, facilitating programmatic parsing or visualization tool integration.
