Databricks query performance is driven mainly by four levers: right-sizing files, leveraging AQE (Adaptive Query Execution), choosing the right join strategy, and avoiding spill (disk overflow). This article walks through the design patterns and parameter settings for each lever, framed for both the Data Engineer Professional exam and real-world production use.
Delta Lake and Parquet file sizes directly drive read performance. Files that are too small inflate metadata overhead and task-startup cost (the Small File Problem), while files that are too large reduce intra-partition parallelism.
| Parameter / Operation | Purpose | Recommended Value |
|---|---|---|
| OPTIMIZE | Compact small files into optimally sized files | Target size 128MB (default 1GB; tune per table characteristics) |
| delta.targetFileSize | Target file size for OPTIMIZE | 128MB to 256MB (tune based on read patterns) |
| spark.sql.files.maxPartitionBytes | Max bytes per task during reads | 128MB (default) |
| spark.sql.files.openCostInBytes | Estimated file-open cost | 4MB (default) |
| autoOptimize(autoCompact) | Auto-compact small files after writes | Recommended for streaming-write tables |
-- Run OPTIMIZE (can be combined with ZORDER)
OPTIMIZE gold.orders ZORDER BY (customer_id, order_date);
-- Set the target file size via table properties
ALTER TABLE gold.orders
SET TBLPROPERTIES (
'delta.targetFileSize' = '134217728' -- 128MB
);
-- Enable autoOptimize
ALTER TABLE silver.events
SET TBLPROPERTIES (
'delta.autoOptimize.optimizeWrite' = 'true',
'delta.autoOptimize.autoCompact' = 'true'
);Set ZORDER BY on the columns that show up most often in filter predicates. ZORDER spatially clusters data by column value to maximize Data Skipping. It is less effective on very high-cardinality columns (like UUIDs), so keep it limited to 3-4 columns.
AQE dynamically re-optimizes query plans using runtime statistics. It is enabled by default on Databricks and automatically performs the following three optimizations:
| Parameter | Default | Description |
|---|---|---|
| spark.sql.adaptive.enabled | true | Enables/disables AQE as a whole |
| spark.sql.adaptive.coalescePartitions.enabled | true | Enables/disables coalescing of small partitions |
| spark.sql.adaptive.coalescePartitions.minPartitionSize | 1MB | Minimum partition size after coalescing |
| spark.sql.adaptive.advisoryPartitionSizeInBytes | 64MB | Advisory partition size after coalescing |
| spark.sql.adaptive.skewJoin.enabled | true | Enables/disables skew-join optimization |
| spark.sql.adaptive.skewJoin.skewedPartitionFactor | 5 | Multiple of the median size used to flag a partition as skewed |
| spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes | 256MB | Absolute threshold for declaring a partition skewed |
| spark.sql.adaptive.autoBroadcastJoinThreshold | 30MB | Size threshold at which AQE switches to Broadcast Join |
Spark has three main join strategies, chosen based on table size and data characteristics. With AQE enabled they can switch at runtime, but understanding them at design time is still essential.
| Join Strategy | When It Applies | Benefit | Caveat |
|---|---|---|---|
| Broadcast Hash Join | One side fits under autoBroadcastJoinThreshold | No shuffle; fastest option | Broadcasting a large table can OOM the driver |
| Sort-Merge Join | Default when both sides are large | Stable; works reliably for large-vs-large joins | Sort + shuffle costs are high |
| Shuffle Hash Join | When one side is fairly large but the other fits in memory | No sort required; can beat Sort-Merge in some cases | Heavy memory use; risk of spill |
-- Explicitly broadcast a small table (hint syntax)
SELECT /*+ BROADCAST(dim_products) */
f.order_id,
f.amount,
d.product_name
FROM fact_orders f
JOIN dim_products d ON f.product_id = d.product_id;
-- Tune autoBroadcastJoinThreshold
SET spark.sql.autoBroadcastJoinThreshold = 50m; -- broadcast tables up to 50MBThe BROADCAST hint overrides the optimizer's decision. For joins between a dimension table (tens of thousands to hundreds of thousands of rows) and a fact table (hundreds of millions of rows), the standard play is to put a BROADCAST hint on the dimension side.
spark.sql.shuffle.partitions controls the number of partitions during shuffles. The default is 200, but you should tune it based on data volume.
-- Tune the shuffle partition count
SET spark.sql.shuffle.partitions = 400;
-- Advisory partition size when AQE is enabled
SET spark.sql.adaptive.advisoryPartitionSizeInBytes = 128m;Spill is what happens when shuffle, sort, or aggregation runs out of memory and offloads data to disk. It tanks performance, so designing to minimize spill matters.
| Mitigation | How | Effect |
|---|---|---|
| Increase partition count | Raise spark.sql.shuffle.partitions | Shrinks per-task data so it fits in memory |
| Add memory | Switch to memory-optimized instance types | Expands per-executor memory capacity |
| Use Broadcast Join | Broadcast the small side to skip the shuffle entirely | Eliminates the shuffle itself |
| Pre-filter the data | Drop unneeded rows with WHERE before the join | Reduces the volume of data shuffled |
| Skew mitigation | Enable AQE skewJoin, or apply a salting technique | Distributes skewed partitions |
-- Metrics to check in the Spark UI
-- Stages > Task Details:
-- Shuffle Spill (Memory): bytes spilled from memory to disk
-- Shuffle Spill (Disk): bytes written to disk
-- Example mitigations when spill is happening
SET spark.sql.shuffle.partitions = 800; -- finer-grained partitions
SET spark.sql.adaptive.advisoryPartitionSizeInBytes = 64m; -- smaller AQE advisory sizePhoton is Databricks' native vectorized execution engine that accelerates the Scan, Filter, Aggregation, and Join stages. Just use a Photon-enabled cluster and it kicks in automatically — no SQL rewrites required.
Data Engineer Professional
問題 1
A query joining a 1-billion-row fact table to a 50,000-row dimension table is slow. The Spark UI shows that Sort-Merge Join was chosen and Shuffle Spill (Disk) is enormous. Which is the most effective fix?
正解: A
A 50,000-row dimension table is small enough to broadcast. A BROADCAST hint switches the plan to Broadcast Hash Join, which removes the shuffle and the spill with it. Reducing the partition count makes each task larger and worsens spill. Disabling AQE strips out optimization features. Running ZORDER on non-join-key columns has no effect on the join's shuffle/spill behavior.
When should OPTIMIZE be executed?
The best timing is after a batch write completes and before downstream queries run. Typically you put an OPTIMIZE task at the end of an ETL job, or schedule it as a daily maintenance job. For streaming-write tables, enabling Databricks autoOptimize reduces the need for manual runs. Running OPTIMIZE too often increases compute cost, so balance frequency against read performance.
Is AQE enabled by default? When would you disable it?
On Databricks Runtime 12.x and later, AQE (spark.sql.adaptive.enabled) is enabled by default. Cases for disabling it are rare: occasionally to exclude AQE effects during benchmarking, or when AQE's partition coalescing hurts a specific query (verified by testing). For normal operations, keep it enabled.
Where can I check whether spill (disk overflow) is happening?
In the Spark UI Stages tab, you can see Shuffle Spill (Memory) and Shuffle Spill (Disk) per task. If Spill (Disk) is non-zero, data is being spilled to disk. You can also check spill metrics on Sort and HashAggregate nodes in the query execution plan on the SQL tab. For queries that consistently spill, consider tuning spark.sql.shuffle.partitions or increasing memory.
Practice with certification-focused question sets
無料で問題を解いてみるNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
Databricks Certifications: All 7 Exams, Difficulty & Study Plan (2026)
Complete guide to all 7 Databricks certifications — Data Eng...
Databricks Exam Difficulty Ranking: All 7 Certs Compared (2026)
Every Databricks certification ranked by difficulty, with st...
Databricks Study Guide: Fastest Pass Route & Time Estimates (2026)
How to pass Databricks certifications efficiently. Official ...
Databricks Data Engineer Associate: Complete Guide (2026)
Domain-by-domain breakdown of the Databricks Certified Data ...
Databricks Data Engineer Professional: Complete Guide (2026)
Tactics for the Databricks Certified Data Engineer Professio...