Sub topic 14
Migrating an existing table to liquid clustering
This chapter assumes you know OPTIMIZE, partitioning, Z-ordering, and the CLUSTER BY syntax from earlier chapters. You should also be able to read DESCRIBE DETAIL output. Migration is the last step of the plan: you pick a strategy, then move an existing table onto it.
What decides the migration path
How you migrate depends on the table's current layout. Liquid clustering replaces both partitioning and Z-ordering. A table cannot use PARTITIONED BY and CLUSTER BY together, and OPTIMIZE ... ZORDER BY is not supported on a clustered table.
Path 1: an unpartitioned table
For an unpartitioned Delta table, you can enable clustering in place.
ALTER TABLE events CLUSTER BY (event_date, user_id);
OPTIMIZE events;The ALTER statement only records the clustering columns in table metadata. It rewrites no data. Files already in the table stay where they are until OPTIMIZE runs. Run OPTIMIZE right after the ALTER, or the old layout will keep serving your queries.
Liquid clustering is incremental. Later OPTIMIZE runs rewrite only files that are not yet clustered, so routine runs are cheap. If you want every existing file rewritten, for example after changing the clustering columns, Databricks Runtime 15.2 and later supports OPTIMIZE events FULL. Expect that run to be expensive on a large table.
Path 2: a partitioned table
You cannot add CLUSTER BY to a partitioned table. Build a new table instead.
CREATE TABLE events_new
CLUSTER BY (event_date, user_id)
AS SELECT * FROM events_partitioned;
OPTIMIZE events_new;Validate the new table before you switch to it.
SELECT
(SELECT COUNT(*) FROM events_partitioned) AS old_rows,
(SELECT COUNT(*) FROM events_new) AS new_rows;
DESCRIBE DETAIL events_new;Check that the row counts match. Then compare numFiles and sizeInBytes against the old table. Run your most common filtered queries on both tables and compare the files read in the query profile.
When you are satisfied, swap the names.
ALTER TABLE events_partitioned RENAME TO events_partitioned_old;
ALTER TABLE events_new RENAME TO events_partitioned;Keep the old table until the new one has served production traffic for a while.
Choosing the clustering columns
Apply the same rules as in the earlier chapter on key choice. Pick the columns that appear most often in filters. Put the date column and the high-cardinality column you filter on, such as user_id, in the list together. You can usually drop the old partition column into the clustering list as well. Keep the list short. Each extra column dilutes the benefit for the others.
You can change your mind later without another migration:
ALTER TABLE events_new CLUSTER BY (user_id);New writes and later OPTIMIZE runs follow the new columns. Existing files are not rewritten unless you run OPTIMIZE ... FULL.
Common mistakes
- Running
ALTER TABLE ... CLUSTER BYand never runningOPTIMIZE, then wondering why nothing improved. - Trying to keep
PARTITIONED BYandCLUSTER BYon the same table. - Leaving
ZORDER BYin scheduled maintenance jobs. Those jobs fail once the table is clustered. - Dropping the old table before checking row counts and query behavior.
Exercise
A table is partitioned by eventdate and you want liquid clustering on (eventdate, user_id). Why can't you just run ALTER TABLE ... CLUSTER BY, and what do you do instead?
Show answer
A table cannot be both partitioned and liquid clustered, so clustering cannot be enabled in place on a partitioned table. Create a new table with CREATE TABLE ... CLUSTER BY (eventdate, userid) AS SELECT * FROM the old table, run OPTIMIZE on it, validate row counts and query performance, then rename the tables to swap them.