Sub topic 7
Choosing a data layout: partitioning and liquid clustering
This chapter assumes you can create Delta tables with SQL, as covered in the earlier chapters on table creation. It explains how a table's physical layout affects query speed, and how to pick between partitioning and liquid clustering.
Why layout matters
A Delta table is a set of Parquet data files plus a transaction log. The log records minimum and maximum values for each column in each file. When a query filters on a column, Databricks skips files whose value range cannot match. This is called data skipping.
Skipping works best when rows with similar values sit in the same files. A layout strategy controls how rows are grouped into files.
Partitioning
Partitioning splits a table into separate directories, one per distinct value of the partition column. A query that filters on that column reads only the matching directories.
CREATE TABLE main.sales.events_partitioned (
event_id BIGINT,
event_date DATE,
payload STRING
) USING DELTA
PARTITIONED BY (event_date);Partitioning has costs:
- The partition columns are fixed when you create the table. Changing them means rewriting the table.
- A column with many distinct values creates many small directories and small files, which slows both reads and writes.
- Each partition is written separately, so skewed values produce uneven partition sizes.
Partitioning is a reasonable fit when the column has low cardinality (few distinct values), each partition holds a large amount of data, and queries almost always filter on that column.
Liquid clustering
Liquid clustering is the layout feature Databricks recommends for new Delta tables. You name clustering columns, and Delta organizes rows so that similar values are stored together. There are no partition directories.
CREATE TABLE main.sales.events (
event_id BIGINT,
event_date DATE,
payload STRING
) CLUSTER BY (event_date);Clustering is applied when you run OPTIMIZE, so schedule it regularly. Newly written data is not fully clustered until then.
OPTIMIZE main.sales.events;You can change the clustering columns later without rewriting existing data:
ALTER TABLE main.sales.events CLUSTER BY (event_date, event_id);New writes and later OPTIMIZE runs use the new columns. To remove clustering, run ALTER TABLE main.sales.events CLUSTER BY NONE.
Comparing the two
| Aspect | Partitioning | Liquid clustering |
|---|---|---|
| Declared with | `PARTITIONED BY` | `CLUSTER BY` |
| Changing columns later | Requires a table rewrite | `ALTER TABLE ... CLUSTER BY` |
| High-cardinality columns | Poor fit | Supported |
| Physical structure | One directory per value | No directories, files grouped by value ranges |
| Applied by | Every write | Writes and `OPTIMIZE` |
Common mistakes
- Partitioning by a timestamp or an ID column. This creates huge numbers of tiny partitions.
- Combining
PARTITIONED BYandCLUSTER BYon one table. They cannot be used together. - Choosing clustering columns that queries never filter on. Pick the columns that appear most often in
WHEREclauses and join conditions. - Forgetting to run
OPTIMIZE, then expecting clustered files.
Exercise
A table has a customer_id column with millions of distinct values, and most queries filter on it. Should you use PARTITIONED BY (customer_id) or CLUSTER BY (customer_id), and why?
Show answer
Use CLUSTER BY (customer_id). Partitioning on a high-cardinality column creates millions of tiny directories and files, which hurts performance. Liquid clustering groups similar values into files without separate directories, and you can change the columns later with ALTER TABLE.
Exercise
You created a table with CLUSTER BY (event_date) and appended a large batch. Queries filtering on event_date are not skipping many files. What should you run?
Show answer
Run OPTIMIZE main.sales.events;. Clustering is applied during OPTIMIZE, so freshly appended data is not fully clustered until it runs.