Skip to main content
The storage that backups use is roughly the product of three things: the size of the data, how long backups are kept, and how much of the data is rewritten between two backups. Work on them in the order of effort: retention is a setting, removing unused data is a decision, rewriting less means changing how you delete and update, and better compression needs measurements.

Keep fewer backups

  • In ClickHouse Cloud, the retention and the frequency of backups can be configured on the Scale and Enterprise tiers. See configurable backups for the allowed values and understanding backup cost. Backups form chains of one full backup and the incremental backups that follow it, and a chain is kept as long as one of its restore points is within the retention.
  • If you export backups to your own bucket, ClickHouse Cloud continues to take its own backups by default. Backups to your bucket that are configured in the Cloud console are full backups. Incremental ones need the BACKUP statement with the base_backup setting.
  • With the BACKUP statement you manage the chain yourself. To create an incremental backup, ClickHouse reads only the .backup file of the base backup. To restore one, it reads the data files of the whole chain down to the full backup, so delete a backup only together with the backups that are based on it.

Back up less data

Tables and partitions that nobody uses are backed up like everything else. This query lists the tables in which no part was written during the last 30 days, which finds abandoned copies and tables left over from migrations:
Group by partition for one table to see how much old data it holds. Drop what is not needed, or let a TTL delete or roll up old rows. The BACKUP statement can also leave tables out with EXCEPT TABLES.

Avoid rewriting data between backups

An incremental backup stores every file that is not in its base backup with the same size and checksum. Data parts never change, but merges and mutations replace them with new parts, and those are stored again. The following test shows how much each kind of change costs. It writes to a disk named backups, configured as in BACKUP / RESTORE to disk. The two table settings make the small example behave like a large table: columns are stored in separate files, and the codec is ZSTD(3), which parts of 100 MiB and more get by default.
An incremental backup based on the previous backup was then taken after each of the following changes, for example BACKUP TABLE readings TO Disk('backups', 'inc_2_insert') SETTINGS base_backup = Disk('backups', 'inc_1_no_change'). The first one, inc_1_no_change, was taken before any change:
total_size is what a restore brings back, compressed_size is what the backup wrote to the destination:
  • An ALTER TABLE ... UPDATE stores the updated column again (inc_4). An ALTER TABLE ... DELETE stores every part that contains a matching row again in full: one partition in inc_5, and the whole table in inc_6. That delete removed only 0.1% of the rows, but every part has rows of that sensor.
  • A merge creates a new part, so all the rows of the merged parts are stored again (inc_7). Background merges do the same to newly inserted data.
  • A lightweight DELETE only added a small mask to each part (inc_3). The rows are removed, and the part is rewritten, by a later merge.
To keep incremental backups small, remove data by partition (DROP PARTITION, TTL) instead of by row where you can, limit a mutation to the partitions that need it with a condition on the partition key or with IN PARTITION, run row-level deletes in few large batches instead of many small ones, and do not run OPTIMIZE ... FINAL on a schedule. Parts of different partitions are never merged, so a partition that no longer receives data stops changing.

Compress the largest columns better

First find where the bytes are:
For the readings table as it was before the changes above:
Three columns have the same uncompressed size, and their compressed sizes differ by a factor of almost 500. What decides is the position in the sorting key: sensor_id, the first key column, consists of long runs of equal values. ts, the last key column, rises within each sensor, which a general-purpose codec can hardly use. Only ts and value are worth any work. Measure a candidate codec on a sample before you change the table. CREATE TABLE ... AS keeps the sorting key, the partitioning and the settings identical, so that the codec is the only difference:
Delta makes the rising key column 46% smaller. For value, which changes from row to row, both specialized codecs are worse than plain ZSTD(3), so that column is left alone. The numbers belong to this data: measure your own, and compare against the general-purpose codec that your parts really use (default_compression_codec in system.parts). Parts below 100 MiB are written with LZ4 by default, so a small benchmark table without explicit codecs does not show what a large part would do. Changing the codec of a column changes only the table metadata:
The existing parts are not touched and keep their size. New inserts and merges use the new codec. To recompress existing data, rewrite it one partition at a time:
  • For a column that is not part of the sorting or partition key, ALTER TABLE ... UPDATE value = value IN PARTITION 202602 WHERE 1 rewrites that column only.
  • A key column cannot be updated (Cannot UPDATE key column), so the parts have to be rewritten as a whole: OPTIMIZE TABLE readings PARTITION 202601 FINAL. In the example this reduced the partition from 7.42 MiB to 5.50 MiB. The next incremental backup wrote 2.30 MiB, about the size of the recompressed column, because the partition was a single part and the files of its other columns came out unchanged.
Changing the type of a column is different from changing its codec: it rewrites the column in all parts. See also Compression in ClickHouse, the reference of the column compression codecs and Avoid mutations.