MergeTree table without an ALTER TABLE ... DELETE or ALTER TABLE ... UPDATE mutation, write the rows you want to keep, already corrected, into a staging table with the same structure, and swap whole partitions with ALTER TABLE ... REPLACE PARTITION. The expensive part is an ordinary INSERT ... SELECT that you can check and repeat before the production table is touched, and each partition is then replaced atomically.
Example
Theevents table is partitioned by month:
17 and 42 have to be deleted, and the payload of user 99 has to be emptied:
CREATE TABLE ... AS, which copies the columns, the keys, the indexes, the projections and the settings. Fill it with the cleaned version of the affected partitions:
REPLACE PARTITION statement. With many partitions, generate the statements from system.parts:
REPLACE PARTITION does not remove the data from the staging table, so drop it at the end:
What it requires
- Both tables must have the same structure, partition key, sorting key, primary key and storage policy, and the production table must have every index and projection of the staging table. The full list is in the
REPLACE PARTITIONreference. A mismatch is reported as an error such asTables have different ordering, and nothing is replaced. - The staging table must have data in the partition. Since version 26.6, replacing a partition from a source that has no parts in it fails with
BAD_ARGUMENTSinstead of silently emptying the partition (seeallow_replace_partition_from_empty_source). If no row of a partition is to be kept, useALTER TABLE ... DROP PARTITION. - You need disk space for a second copy of the rows you keep until the old parts are gone. The swap itself does not copy data when both tables are on the same local disk: the files of the staging parts are hard-linked into the production table. The replaced parts become inactive and are deleted from disk after
old_parts_lifetime, which is 480 seconds by default.
What to watch out for
- Rows inserted after the copy are lost. The partition is replaced with what the staging table contains. A row that is inserted into the production partition between the
INSERT ... SELECTand theREPLACE PARTITIONdisappears. Use the method for partitions that no longer receive data, or stop the writes to them for the duration. - Atomic for a partition, not for the set. Each partition is replaced atomically, but the partitions are replaced one after another. A query that runs in between sees some partitions in the new state and some in the old one.
- Materialized views are not updated. Materialized views are triggered by inserts into their source table. Neither the insert into the staging table nor
REPLACE PARTITIONtriggers the views of the production table, so their target tables still contain data derived from the removed or changed rows. - The whole partition is rewritten. The method fits changes that affect many rows, or rows scattered across a partition. For a small number of rows, a lightweight
DELETEor a mutation is less work.
Related pages
- Delete overview compares the other ways to delete data.
- Avoid mutations explains what a mutation costs.
- Achieving atomic inserts and multi-table consistency uses staging tables and partition operations to load new data.