Sub topic 13
Choosing between partitioning, Z-ordering, and liquid clustering
This chapter assumes you can run OPTIMIZE, read DESCRIBE DETAIL output, and explain how Delta stores Parquet files plus a transaction log. It also assumes you know that data skipping uses per-file min/max statistics. The earlier chapters covered each layout tool on its own. Here you will compare them and pick one for a given table.
The three tools solve the same problem differently
All three techniques aim to make a query read fewer files. They differ in how they group rows into files and how much maintenance that grouping needs.
| Aspect | Hive-style partitioning | Z-ordering | Liquid clustering |
|---|---|---|---|
| Declared with | `PARTITIONED BY` | `OPTIMIZE ... ZORDER BY` | `CLUSTER BY` |
| Physical effect | One directory per distinct value | Rewrites files so similar values sit together | Groups similar values into files incrementally |
| Changing the columns | Rewrite the whole table | Pass different columns on the next run | `ALTER TABLE ... CLUSTER BY` |
| Cost of each maintenance run | None for the layout itself | Can rewrite many files again | Targets only data that is not yet clustered |
| Works with the other two | Combines with Z-ordering | Combines with partitioning | Cannot combine with either |
| Best fit | Very large tables with a low-cardinality filter | Existing tables already built this way | Default for new tables |
Partitioning is a hard physical boundary. Z-ordering and liquid clustering are soft: they improve the min/max ranges inside files so that statistics can rule files out.
Partitioning: use it rarely, and on purpose
A partition column creates one directory per value. A filter on that column lets Spark skip whole directories without reading any statistics.
The cost is that every partition holds its own files. Choose a column with too many distinct values, such as user_id, and you create many tiny partitions, each with tiny files. Compaction cannot merge files across partition boundaries, so the small-files problem becomes permanent. Changing the partition column later means rewriting the table.
Partitioning is a reasonable choice when all of these hold:
- The table is very large. A common guideline is around 1 TB or more.
- You filter on the column in nearly every query, usually with equality or a range.
- The column has low cardinality, such as a date, a region, or a tenant tier.
- Each partition holds at least about 1 GB of data.
Date columns are the classic case. Even then, a daily partition on a table that receives only a few megabytes per day produces thousands of undersized partitions. Check before you commit.
CREATE TABLE sales_by_day (
sale_id BIGINT,
customer_id BIGINT,
sale_date DATE,
amount DECIMAL(12, 2)
)
PARTITIONED BY (sale_date);Z-ordering: improve layout inside an existing design
OPTIMIZE ... ZORDER BY compacts files and sorts rows along a space-filling curve over the listed columns. Rows with similar values for those columns end up in the same files. Narrower min/max ranges per file mean more files can be skipped.
Z-ordering has these properties:
- It is a manual operation. The layout is only as good as your last
OPTIMIZErun. New writes land unordered until you run it again. - It is not cheap to repeat. Each run can rewrite files that were already ordered, because new data has to be merged into the curve. Scope the run with a
WHEREclause on partition columns to limit the work. - Extra columns dilute the benefit. One or two columns usually work well. Each additional column weakens the clustering for the others.
- The columns need statistics. By default Delta collects statistics for the first 32 columns. A Z-order column outside that range gains nothing.
- It sits on top of partitioning. You can partition by date and Z-order by
user_id, which is the design shown in earlier chapters.
OPTIMIZE sales_by_day
WHERE sale_date >= '2024-01-01'
ZORDER BY (customer_id);Z-ordering is a good choice when you already have a partitioned table that works well and you need to add a second filter dimension without a migration.
Liquid clustering: the default for new tables
Liquid clustering replaces both partitioning and Z-ordering. You declare clustering columns with CLUSTER BY, and OPTIMIZE applies the clustering. Three differences matter for the decision:
- Keys can change.
ALTER TABLE ... CLUSTER BYchanges the columns when query patterns shift, and no up-front rewrite is required. Existing files keep their old layout until they are rewritten, so measure after changing keys. - Maintenance is incremental.
OPTIMIZEworks on data that is not yet clustered instead of redoing the whole table. - The layout is not tied to directories. High-cardinality columns such as
user_idare acceptable clustering keys. They would be disastrous partition columns.
The restrictions are real. You cannot declare both PARTITIONED BY and CLUSTER BY on one table, and you cannot run ZORDER BY on a liquid clustered table. The number of clustering columns is limited, currently up to four. Clustering columns must be among the columns that have statistics.
CREATE TABLE sales_clustered (
sale_id BIGINT,
customer_id BIGINT,
sale_date DATE,
amount DECIMAL(12, 2)
)
CLUSTER BY (sale_date, customer_id);
OPTIMIZE sales_clustered;Clustering only takes effect when OPTIMIZE runs or when a maintenance feature runs it for you. Pair it with predictive optimization on Unity Catalog managed tables so you do not have to schedule the jobs.
A decision flow
The diagram below summarizes the order in which to ask the questions.
Matching the layout to the workload
The following cases show how the decision plays out.
- Append-only event log, filtered by date and sometimes by user. Use
CLUSTER BY (event_date, user_id). Date works as a clustering key because data arrives roughly in date order, anduser_idhandles the secondary filter. - A 5 TB fact table with a mature daily partition, healthy partition sizes, and queries that mostly filter on the date. Keep the partitioning. Add Z-ordering on one secondary column only if queries on that column are slow. Moving to liquid clustering is a migration project, not an emergency.
- A 50 GB table that someone partitioned by
customer_id. This is the textbook failure case. Look for a very large number of partitions and average file sizes far below the target. Move to liquid clustering. - A table where the common filter columns change every quarter. Liquid clustering wins because the keys can change without a rebuild.
- A tiny table, under a few gigabytes. Do none of these. The table fits in a handful of files and skipping has little to gain.
Check what a table is using
DESCRIBE DETAIL shows both the partition columns and the clustering columns, so you can audit a catalog quickly.
DESCRIBE DETAIL sales_clustered;Read the partitionColumns, clusteringColumns, numFiles, and sizeInBytes fields. Divide sizeInBytes by numFiles for the average file size. After a change, run the same representative queries before and after, and compare the files read and bytes read in the query profile.
Common mistakes
- Partitioning on a high-cardinality or rarely filtered column "just in case".
- Z-ordering by five or six columns and expecting each to benefit equally.
- Choosing clustering keys by guessing instead of by reading the
WHEREclauses in your real query history. - Declaring
CLUSTER BYand never runningOPTIMIZE, so no clustering happens. - Forgetting that liquid clustering and partitioning cannot coexist, then planning a design that needs both.
Exercise
A 300 GB table is partitioned by device_id, which has about 2 million distinct values. Queries filter on device_id and on reading_date. DESCRIBE DETAIL shows an average file size of 400 KB. Which layout would you move to, and why is the current design failing?
Show answer
Move to liquid clustering with CLUSTER BY (reading_date, device_id). Partitioning on a column with 2 million distinct values creates millions of tiny partitions, each holding tiny files, and OPTIMIZE cannot merge files across partitions. That explains the 400 KB average. Liquid clustering groups similar device_id and reading_date values into properly sized files and still allows filters on both columns to skip files.
Exercise
A team wants a table that is both PARTITIONED BY (sale_date) and CLUSTER BY (customer_id). Is this allowed, and what should they do instead?
Show answer
It is not allowed. Liquid clustering cannot be combined with partitioning. They should use CLUSTER BY (sale_date, customer_id) on a new table, or keep partitioning by sale_date and use OPTIMIZE ... ZORDER BY (customer_id) on the existing table.