Sub topic 6
Co-locating data with Z-ordering
This chapter assumes you know how OPTIMIZE compacts files and how Delta stores per-file min/max statistics for data skipping. You should also be comfortable with basic SQL on Databricks.
The problem Z-ordering solves
Data skipping works by comparing a query's filter against the minimum and maximum values stored for each file. If every file contains a wide spread of user_id values, each file's range overlaps almost any filter. The engine cannot skip anything and reads every file.
Z-ordering rewrites files so that rows with similar values in the chosen columns end up in the same files. Each file then covers a narrow range of those columns, and a filter on them can rule out most files.
Running Z-order
Z-ordering is an option on OPTIMIZE. It compacts files and clusters the data in the same pass.
OPTIMIZE events_partitioned
WHERE event_date >= '2024-01-01'
ZORDER BY (user_id);The WHERE clause can only reference partition columns. It limits the work to recent partitions, so you do not rewrite the whole table each time. The Python Delta API offers the same operation:
from delta.tables import DeltaTable
dt = DeltaTable.forName(spark, "events")
dt.optimize().executeZOrderBy("user_id")You can list several columns, as in ZORDER BY (user_id, country). The same call without ZORDER BY is plain compaction.
How it works
Z-ordering uses a space-filling curve (the Z-order curve) to map values from several columns onto a single sort order. Rows that are close together on the curve are close together in every chosen column. This is different from a plain sort, which clusters tightly on the first column and leaves later columns scattered.
The effect has a cost. Each added column dilutes the clustering for the others. Z-ordering on one column behaves like a sort. Z-ordering on four columns gives only a modest skip rate on each.
Common errors and gotchas
- Z-ordering a partition column. Partition values already separate files by directory. Z-ordering on them adds nothing, and Databricks rejects it.
- Columns without statistics. Data skipping only helps on columns with collected statistics, which by default are the first 32 columns of the table. A Z-order column beyond that limit gives no benefit unless you change the statistics settings.
- Too many columns. Pick the one to three columns that your queries filter on most.
- Expecting incremental work. Z-ordering is not incremental in the way liquid clustering is. Re-running it over a range of data can rewrite files that are already well ordered, so scope the
WHEREclause to the data that changed. - Forgetting that new writes are unordered. Rows added after an
OPTIMIZE ... ZORDER BYare not clustered until the next run.
Checking the result
Compare file counts and sizes with DESCRIBE DETAIL before and after. The OPTIMIZE output also reports how many files were added and removed. The real test is a query on the Z-ordered column: check the query profile for files read versus files pruned.
Exercise
A table is partitioned by event_date. Queries often filter on user_id, but each query still reads nearly every file in the selected partition. What command would you run to improve skipping, and why might you restrict it with a WHERE clause?
Show answer
Run OPTIMIZE events_partitioned WHERE event_date >= '<recent date>' ZORDER BY (user_id);. This rewrites files so each covers a narrow user_id range, so a filter on user_id can skip most files. The WHERE clause (on the partition column) limits the rewrite to recent partitions, so you do not pay to reorder old data that has not changed.
Exercise
Why would OPTIMIZE events ZORDER BY (event_date) be a poor choice when events is partitioned by event_date?
Show answer
Partitioning already places each date's rows in separate directories, so the files are already separated by that column. Z-ordering on a partition column gives no extra skipping, and Databricks does not allow it.