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
BACKUPstatement with thebase_backupsetting. - With the
BACKUPstatement you manage the chain yourself. To create an incremental backup, ClickHouse reads only the.backupfile 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: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 namedbackups, 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.
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 ... UPDATEstores the updated column again (inc_4). AnALTER TABLE ... DELETEstores every part that contains a matching row again in full: one partition ininc_5, and the whole table ininc_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
DELETEonly added a small mask to each part (inc_3). The rows are removed, and the part is rewritten, by a later merge.
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:readings table as it was before the changes above:
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:
- For a column that is not part of the sorting or partition key,
ALTER TABLE ... UPDATE value = value IN PARTITION 202602 WHERE 1rewrites 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.