Sub topic 12
Efficient Workloads and Storage Maintenance
This chapter assumes you know how to run Spark jobs and Delta tables on Databricks, can read SQL, and have seen the billing system tables from earlier chapters. Compute and storage settings control how much you pay per hour. Workload and storage efficiency control how many hours you need.
Why efficiency is a cost lever
DBU charges and cloud instance charges both scale with run time. A job that reads less data, shuffles less, or finishes sooner uses fewer DBUs and fewer instance hours. Storage adds its own charge: the cloud provider bills for every data file in object storage, including files that Delta no longer uses.
Three areas give the most return for the least effort:
- Reading less data per query.
- Keeping files at a healthy size.
- Removing files nobody needs.
Read less data
Most savings come from not scanning data you do not need. These habits apply to any job or query:
- Select only the columns you need instead of
SELECT *. Delta stores data in a columnar format, so unused columns are never read. - Filter early and on columns that the table is organized by, so Delta can skip files using file-level statistics.
- Avoid repeated full reads of the same large source. Write an intermediate result to a table once and reuse it.
- Process data incrementally (new files or changed rows only) instead of recomputing the whole table on every run.
Compact small files with OPTIMIZE
Frequent small writes, such as streaming micro-batches, create many tiny files. Each file adds overhead to every read, so queries run longer and cost more. OPTIMIZE rewrites small files into larger ones.
OPTIMIZE main.sales.orders;
VACUUM main.sales.orders;OPTIMIZE itself uses compute, so run it on a schedule that matches how fast files accumulate, such as daily on a busy table. Running it hourly on a table that changes once a day wastes money. For some tables you can enable predictive optimization in Unity Catalog, which lets Databricks decide when to run maintenance. Check that it is available for your table type before relying on it.
Remove unused files with VACUUM
When you update, delete, or compact a Delta table, the old data files stay in storage so that time travel and concurrent readers keep working. VACUUM deletes files that the table no longer references and that are older than the retention period. The default is 7 days.
Keep these points in mind:
VACUUMlowers storage cost only. It does not speed up queries.- After it runs, you cannot time travel to versions that depended on the deleted files.
- Do not set the retention period below the longest-running job or stream that reads the table. Doing so can break those readers.
- Tables you never vacuum keep growing, even if the live row count is flat.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Never running `VACUUM` | Storage bills grow with every rewrite | Schedule it, or use predictive optimization |
| Running `OPTIMIZE` too often | Compute spent for little gain | Match the schedule to write volume |
| Shortening retention to save space | Long jobs and time travel fail | Keep retention longer than your longest reader |
| Using `SELECT *` on wide tables | More data scanned per query | Name the columns you need |
| Recomputing full tables each run | Run time grows with data size | Process incrementally |
Find the expensive workloads first
Do not tune at random. Use the billing system tables to find which jobs and warehouses consume the most DBUs, then look at those first. A job that runs for hours every day is a better target than ten jobs that run for minutes. After a change, compare DBU use for the same job before and after, so you know the change paid off.
Exercise
A Delta table receives small streaming writes all day. Queries have slowed down, and storage cost keeps rising even though the table's row count is stable. Which two commands address these two problems, and what does each one fix?
Show answer
OPTIMIZE compacts the many small files into larger ones, which speeds up reads and reduces compute time. VACUUM deletes data files that the table no longer references and that are past the retention period (7 days by default), which reduces storage cost. OPTIMIZE does not free storage by itself, because the old files remain until VACUUM removes them.
Exercise
A teammate wants to set VACUUM retention to 0 hours to cut storage cost. Name one risk.
Show answer
Running jobs or streams that still read older files can fail, and time travel to earlier versions stops working. Retention should stay longer than your longest-running reader.