Sub topic 14
Keeping tables fast and tidy
This chapter assumes you have read the earlier chapters on Delta Lake tables and the gold layer. You should be comfortable running SQL in a Databricks notebook and know that a Delta table is a set of Parquet files plus a transaction log.
Why tables need upkeep
Every write to a Delta table adds files. Streaming bronze ingestion and frequent MERGE runs on silver can leave thousands of small files. Small files slow reads, because the engine opens many files to scan little data. Updates and deletes also leave old files on storage, because Delta keeps them for time travel.
Two chores follow from this:
- Compact small files and organize data so queries skip files they do not need.
- Remove old files that no table version needs any more.
Inspect before you fix
DESCRIBE DETAIL shows how many files a table has and how large it is.
DESCRIBE DETAIL main.silver.orders;Check numFiles and sizeInBytes. A table with 20,000 files and 2 GB of data has a small-file problem.
Compact with OPTIMIZE
OPTIMIZE rewrites many small files into fewer, larger ones. It does not change table contents, and readers keep working while it runs.
OPTIMIZE main.silver.orders;Cluster by the columns you filter on
Liquid clustering lays out data by the columns you name, so queries that filter on them read fewer files. Pick columns that appear in WHERE clauses and joins. In this pipeline, order_id suits silver because MERGE matches on it. order_date suits the gold summary.
ALTER TABLE main.silver.orders CLUSTER BY (order_id);
OPTIMIZE main.silver.orders;Clustering is applied when OPTIMIZE runs, so the two statements belong together. You can change the clustering columns later without rewriting the whole table.
Remove old files with VACUUM
VACUUM deletes data files that are no longer referenced by the current table version and are older than the retention period. The default is 7 days (168 hours).
VACUUM main.bronze.orders RETAIN 168 HOURS;Bronze is meant to be replayable, but replay uses the retained rows in the current table version, not old file versions. VACUUM does not delete current data.
Let Databricks do it
Predictive optimization runs OPTIMIZE and VACUUM for Unity Catalog managed tables when it judges they are needed. You can enable it per schema.
ALTER SCHEMA main.gold ENABLE PREDICTIVE OPTIMIZATION;Enabling it on all three layer schemas removes most manual maintenance. If it is unavailable on your account, schedule OPTIMIZE and VACUUM as a separate job task, as described in the orchestration chapter.
What to maintain per layer
| Layer | Typical issue | Useful action |
|---|---|---|
| Bronze | Many small files from frequent ingestion | OPTIMIZE, VACUUM |
| Silver | Rewrites from MERGE | Cluster by the merge key, OPTIMIZE |
| Gold | Dashboard queries filter on date | Cluster by `order_date` |
Common mistakes
- Running VACUUM with a very short retention, which can break concurrent readers.
- Clustering on columns that no query filters on.
- Running OPTIMIZE after every micro-batch. It costs compute, so run it on a schedule that matches write volume.
Exercise
A silver table is MERGEd every few minutes, and queries that filter on order_id have become slow. DESCRIBE DETAIL shows 30,000 files for 3 GB of data. Which two statements would you run, and why?
Show answer
Run ALTER TABLE main.silver.orders CLUSTER BY (order_id) to organize data by the filter and merge key, then OPTIMIZE main.silver.orders to compact the small files and apply the clustering. Afterward, VACUUM removes the old files once the retention period passes.