Sub topic 7
Choosing a data layout: partitioning and liquid clustering
This chapter assumes you can create Delta tables with SQL (CREATE TABLE, CTAS) and know that a Delta table is a set of Parquet data files plus a transaction log. It covers how rows are grouped into files, why that affects query speed, and how to choose between partitioning and liquid clustering.
Why layout matters
A Delta table stores rows in many Parquet files. For each file, the transaction log records statistics, including the minimum and maximum value of each indexed column. When a query filters on a column, Databricks compares the filter with those statistics and skips files that cannot contain matching rows. This is called data skipping.
Skipping only works well when similar values are stored together. If event_date values are scattered randomly across every file, each file's range spans the whole table and nothing can be skipped. A data layout is a strategy for grouping rows so that file ranges stay narrow on the columns you filter by.
Two strategies are in common use:
- Partitioning splits the table into separate directories, one per value of the partition column.
- Liquid clustering keeps related rows near each other inside files and lets you change the clustering columns later.
Partitioning
A partitioned table writes each distinct value of the partition column into its own directory. A query with a filter on that column reads only the matching directories.
CREATE TABLE main.sales.events_partitioned (
event_id BIGINT,
event_date DATE,
country STRING,
payload STRING
) USING DELTA
PARTITIONED BY (country);A filter such as WHERE country = 'DE' now reads only the country=DE directory.
Costs of partitioning
Partitioning looks simple, but it has costs that surprise people:
- Small files. Each partition holds its own files. A partition with only a few megabytes of data produces small files, and reading many small files is slow.
- Over-partitioning. Partitioning by a high-cardinality column such as
customer_idcreates millions of directories. Listing and planning over them can cost more than the scan you wanted to avoid. - Fixed choice. You cannot change the partition columns of an existing table. Changing them means rewriting the whole table into a new one.
- Skew. If one value holds most of the data, that partition is large and the rest are tiny.
Databricks guidance is that most tables under about 1 TB do not need partitioning. If you do partition, choose a low-cardinality column, and aim for each partition to hold at least about 1 GB of data.
Partitioning by a derived date
A common pattern is partitioning by day while the source column is a timestamp. A generated column derives the partition value automatically:
CREATE TABLE main.sales.clicks (
click_id BIGINT,
click_ts TIMESTAMP,
click_date DATE GENERATED ALWAYS AS (CAST(click_ts AS DATE)),
url STRING
) USING DELTA
PARTITIONED BY (click_date);Delta can also use a filter on click_ts to prune click_date partitions, because it knows how the generated column is computed.
Liquid clustering
Liquid clustering replaces both partitioning and ZORDER for new tables. You declare clustering columns, and Delta organizes data by those columns. No directory per value is created, so the small-file problem of over-partitioning does not arise.
CREATE TABLE main.sales.events (
event_id BIGINT,
event_date DATE,
payload STRING
) CLUSTER BY (event_date);You can cluster on up to four columns. Put the columns you filter on most often in the clause. Unlike partition columns, high-cardinality columns such as customer_id are acceptable.
Changing the clustering columns
The columns are metadata, so changing them does not rewrite existing data:
ALTER TABLE main.sales.events CLUSTER BY (event_date, event_id);
ALTER TABLE main.sales.events CLUSTER BY NONE;The new columns apply to data written or optimized from then on. Files written under the old columns stay as they are until OPTIMIZE rewrites them.
Clustering from Python
The DataFrame writer has a clusterBy method:
from pyspark.sql import Row
df = spark.createDataFrame([
Row(event_id=1, event_date="2026-12-01", payload="a"),
Row(event_id=2, event_date="2026-12-02", payload="b"),
])
(df.write
.format("delta")
.clusterBy("event_date")
.mode("overwrite")
.saveAsTable("main.sales.events_py"))When clustering happens
Clustering is not always applied at write time. Rows are clustered when you run OPTIMIZE, and some write paths (such as CTAS and large inserts on recent runtimes) cluster on write. Small appends may land in files that are not yet well clustered. Run OPTIMIZE on a schedule:
OPTIMIZE main.sales.events;OPTIMIZE on a clustered table is incremental. It reorganizes only files that are not yet clustered, so repeated runs are cheaper than a full rewrite.
Automatic clustering
For Unity Catalog managed tables, CLUSTER BY AUTO lets Databricks choose and adjust clustering columns from your query history. It relies on predictive optimization being enabled. Use it when you cannot predict the filter columns. Choose explicit columns when you know the access pattern.
CREATE TABLE main.sales.events_auto (
event_id BIGINT,
event_date DATE,
payload STRING
) CLUSTER BY AUTO;Limits and common errors
- Mixing strategies. A table cannot use liquid clustering together with
PARTITIONED BYorZORDER. Choose one. - Missing statistics. Clustering columns must have statistics collected. By default Delta collects statistics on the first 32 columns, so a clustering column placed later in a wide table needs the statistics setting changed or the column moved.
- Runtime support. Liquid clustering needs a recent Databricks Runtime (13.3 LTS or later for the basics, newer for features such as
clusterByin the DataFrame writer). Check the runtime of your compute ifCLUSTER BYfails to parse. - Expecting clustering without
OPTIMIZE. If queries on a freshly appended table are slow, check whetherOPTIMIZEhas run since the last loads. - Over-clustering. Listing four columns does not make four filters fast. Skipping is strongest on the first columns, and weaker if the columns are unrelated. Start with one or two.
- Data volume. On very small tables, any layout makes little difference. The table may be a single file.
Inspecting a layout
DESCRIBE DETAIL shows how a table is laid out:
DESCRIBE DETAIL main.sales.events;Look at these columns in the output:
| Column | What it tells you |
|---|---|
| `partitionColumns` | Partition columns, empty for a clustered table |
| `clusteringColumns` | Liquid clustering columns |
| `numFiles` | Number of data files in the current version |
| `sizeInBytes` | Total size, so `sizeInBytes / numFiles` gives average file size |
An average file size of a few megabytes on a large table suggests small-file trouble. Run OPTIMIZE, or reconsider the partitioning.
Decision guide
In short:
| Situation | Choice |
|---|---|
| New table, supported runtime | Liquid clustering |
| Filter columns change over time | Liquid clustering, then `ALTER TABLE ... CLUSTER BY` |
| High-cardinality filter column | Liquid clustering |
| Older runtime, very large table, low-cardinality filter | Partitioning |
| Small table | No layout clause |
Layout and writes
Layout also affects how the write operations from earlier chapters behave:
INSERT OVERWRITEandreplaceWhereon a partition column can replace whole partitions cheaply. On a clustered table, areplaceWherestill works with any predicate but rewrites the files that match.MERGE,UPDATE, andDELETEskip files using the same statistics. AMERGEwhoseONcondition includes the clustering column rewrites fewer files.- Concurrent writers conflict less often when their changes touch different partitions or file sets. Disjoint partitions are a classic way to avoid conflicts.
Exercise
A team has a 200 GB events table that is always filtered by user_id (millions of distinct values) and sometimes by event_date. A colleague proposes PARTITIONED BY (user_id). What do you recommend instead, and what do you run afterwards?
Show answer
Use liquid clustering: CREATE TABLE ... CLUSTER BY (user_id, event_date). Partitioning by user_id would create millions of directories with tiny files. The table is also under 1 TB, where partitioning is usually unnecessary. Afterwards, run OPTIMIZE regularly so that newly appended data is clustered.
Exercise
You created a table with CLUSTER BY (event_date) and later find that most queries filter on country. How do you change the layout, and does the existing data change immediately?
Show answer
Run ALTER TABLE main.sales.events CLUSTER BY (country). Only metadata changes. Existing files stay as they are until OPTIMIZE rewrites them under the new clustering columns.