Exams / DBX-DEP / D1 Cost & Performance Optimisation
Cost & Performance Optimisation (part 1)
- Exam domain: Cost & Performance Optimisation
1. Lazy evaluation and honest benchmarking
Spark is lazy: transformations only extend the logical plan; an action (write, display(), count(), manual caching) triggers execution. Databricks optimises all transformations behind one action together.
| Practice | Effect |
|---|---|
display() after every step |
Forces a job each time and breaks whole-pipeline optimisation |
| Re-running the same cell | Hits cached data, so timings are optimistic |
| Production-like timing | Run the full pipeline with one final action, ideally as a job |
%sh cells |
Run on the driver only, never distributed |
collect() on a large result |
Pulls everything to the driver and can restart it |
2. Delta file layout and data skipping
- Delta stores per-file statistics (min/max, null counts, record count); queries skip files whose range cannot match, even for non-partition columns.
- Skipping is only effective when matching values are colocated (partitioning, Z-order, liquid clustering).
- Statistics cover the first 32 columns by default; long strings are truncated, so free-text search gets no benefit (no term index).
- Liquid clustering replaces partitioning and ZORDER (not compatible with them); choose keys from the columns most used in filters/joins.
- Clustering on write works for CTAS/RTAS, INSERT and batch appends; Structured Streaming needs
spark.databricks.delta.liquid.eagerClustering.streaming.enabled=true. - Deletion vectors speed up DELETE, UPDATE and MERGE by marking rows in metadata instead of rewriting Parquet files.
| Feature | When | What |
|---|---|---|
| Optimized writes | Before the write | Shuffle so fewer, larger files per partition |
| Auto compaction | After the write | Mini OPTIMIZE (shows auto=true in history) |
OPTIMIZE |
Manual / predictive | Compacts to larger target files (~1 GB) |
| Predictive optimization | Automatic | OPTIMIZE, VACUUM, ANALYZE on UC managed tables only |
| File-size autotune | Size-based | 256 MB under 2.56 TB, 1 GB above 10 TB |
3. Precompute for dashboards
Materialized views (or a nightly-written summary table) precompute expensive aggregations and joins so many interactive users read stored results. A plain view recomputes at each query; streaming is for continuous ingestion.
4. Structured Streaming cost and latency
- No trigger =
processingTimewith a 0 interval: checks every few milliseconds, causing storage API costs. - For incremental batch use
Trigger.AvailableNow(Trigger.Onceis deprecated); a Delta source is read incrementally via checkpoint offsets. - Large micro-batches cause spill; limit input rate to avoid it.
5. Spark UI diagnostics
| Need | Where |
|---|---|
| Spill | Stage detail page (spill statistics) and query detail |
| Skew | Stage Summary Metrics: max task duration far above the 75th percentile; fix with AQE, salting, pre-aggregation |
| Predicate pushdown | Physical plan (EXPLAIN) |
| Total elapsed query time | Query Profile wall-clock duration (not aggregated task time) |
Spill fixes: more memory per core, smaller input partitions (spark.sql.files.maxPartitionBytes). Broadcast joins use the broadcast(df) hint on a DataFrame. Compute-level Spark conf applies to every notebook; notebook conf is session-scoped.
Exam traps
- Auto compaction is after the write; optimized writes are a shuffle before it.
- Predictive optimization never runs on external tables.
- ZORDER and liquid clustering cannot be combined.
- Query Profile: wall-clock duration, not aggregated task time.
- The two "Storage tab, MEMORY_ONLY" items conflict (B vs C); the exam answer is usually "Size on Disk > 0".
- Spill indicators are Spark UI pages (Stage, Query), not log files.
- Auto-compaction 128 MB and merge-based autotuning are legacy facts that current docs no longer state.
Docs
- https://docs.databricks.com/aws/en/tables/tune-file-size
- https://docs.databricks.com/aws/en/tables/data-skipping
- https://docs.databricks.com/aws/en/tables/features/deletion-vectors
- https://docs.databricks.com/aws/en/delta/clustering
- https://docs.databricks.com/aws/en/optimizations/predictive-optimization
- https://docs.databricks.com/aws/en/structured-streaming/triggers
- https://docs.databricks.com/aws/en/optimizations/spark-ui-guide/long-spark-stage-page
- https://docs.databricks.com/aws/en/ldp/dbsql/materialized
- https://docs.databricks.com/aws/en/spark/faq
- https://docs.databricks.com/aws/en/notebooks/notebooks-code
- https://learn.microsoft.com/en-us/azure/databricks/sql/user/queries/query-profile