Three Delta Lake features for production pipelines
Most engineers who work in Databricks know how to create a Delta table, write data and query it. Fewer use the Delta Lake features that keep production pipelines reliable, fast and cost-efficient. In this Databricks in Practice series, Green Leaf Data Architect Kiran Lokhande uses live code, no slides, to demo three of them: time travel, OPTIMIZE with Z-ORDER, and VACUUM.
Part 1: Time travel (1:27)
Delta Lake records a new version of the table every time you write to or update it. Time travel lets you query any of those versions, which makes it one of the most useful features in production.
Create a sample table (1:48). Kiran builds a small employees dataset with ID, name and salary columns and saves it as a Delta table in an existing catalog and schema.
Make a few updates (4:08). He runs two updates that change the salaries for two employees. Each update creates a new version of the table.
View the version history (6:35). Running DESCRIBE HISTORY on the table lists every operation, including both updates. Each version in that list is one you can travel back to.
Query a past version (7:37). Adding VERSION AS OF to a query returns the table exactly as it looked at that version. An earlier version shows only the first salary change, and a later one shows both.
Query by timestamp (8:52). TIMESTAMP AS OF does the same thing using a point in time instead of a version number. This is handy when someone asks what the table looked like yesterday.
When to use it (9:26). Time travel gives you point-in-time snapshots with no extra infrastructure. It’s especially useful for debugging a bad pipeline run, auditing changes, or recovering from an accidental overwrite.
Part 2: OPTIMIZE and Z-ORDER (9:43)
If a Delta table is getting slow, OPTIMIZE is the first command to reach for.
The small file problem (10:02). Each time a pipeline writes a small batch of data or updates the table, Spark creates new small files. Over time, a table can pile up hundreds or thousands of tiny files, and every query has to read them.
Check the file count (10:34). DESCRIBE DETAIL shows how many files a table has. The small employees table has one file. A larger NYC taxi table that has been updated several times has three.
Compact the files (11:45). After OPTIMIZE runs on the NYC taxi table, its file count drops to one. OPTIMIZE combines small files into fewer, larger and more efficient ones.
Add Z-ORDER (13:15). Kiran then runs OPTIMIZE again with Z-ORDER on the pickup ZIP column. Z-ordering stores related values close together in the same files. When a query filters on pickup ZIP, Delta can skip the files that don’t hold matching values. This is called data skipping, and on large tables it can cut query time dramatically.
When it matters (14:50). Z-ORDER has the biggest impact on large tables, in the hundreds of gigabytes or more. On small tables you won’t notice much difference, but it’s still a good habit to build into routine pipeline maintenance.
Part 3: VACUUM (15:18)
VACUUM is the step teams skip most often, and then they wonder why their storage bill keeps climbing.
Why old files pile up. Time travel works by keeping the files behind older table versions. Delta never deletes those files on its own, so storage keeps growing. VACUUM cleans them up.
The default retention window (16:12). By default, VACUUM deletes files older than 7 days, and that 7 days is also your time travel window. Once those files are gone, you can’t query versions older than that. You can set a different retention period when you run VACUUM. For example, 168 hours matches the 7-day default.
Never set retention to 0 hours in production (17:31). Setting retention to 0 hours deletes every historical version right away. Time travel stops working, and the deleted versions can’t be recovered. Kiran has seen engineers do this and regret it almost immediately. Save it for cases where storage is tight and you know exactly what you’re deleting, and keep it out of production.
The production pattern (18:19). Run VACUUM on a schedule, weekly for most tables, and keep the 7-day default unless a compliance requirement calls for something else.
Key takeaways
- Time travel is your safety net. Every write creates a version you can query, so you can debug, audit and recover without extra tooling.
- OPTIMIZE and Z-ORDER speed up queries. Compacting small files and colocating commonly filtered columns lets Delta skip data it doesn’t need to read.
- VACUUM controls storage cost. Schedule it weekly and keep the default retention so you don’t lose time travel.
Frequently asked questions
What is Delta Lake time travel?
Delta Lake saves a new version of a table with every write or update. Time travel lets you query an earlier version by version number or by timestamp.
How do I fix the small file problem in Delta Lake?
Run OPTIMIZE to compact small files into larger ones. For large tables that are often filtered on the same column, add Z-ORDER on that column to improve data skipping.
How long does Delta Lake keep old versions?
By default, VACUUM removes files older than 7 days, so you can time travel back about a week. You can change the retention period, but setting it to 0 hours permanently removes all history.
How often should I run VACUUM?
Weekly works for most production tables, using the default 7-day retention.
Want to see more? Check out our other Databricks demos on here.
Watch the full demo below!