Using TTL to Manage Data Lifecycles in ClickHouse
ClickHouse's TTL clause on MergeTree tables automates data lifecycle tasks. It can delete expired rows, optionally filtered with WHERE and combined into multiple rules. It can also roll up old rows with GROUP BY ... SET, recompress older parts with heavier codecs like LZ4HC, and reset individual columns to their defaults. TTL work runs asynchronously during background merges, every 4 hours by default, tunable via merge_with_ttl_timeout but not below about 300 seconds to avoid I/O overhead. Materialized views can copy rows to a history table before they expire, and storage policies can move data from hot to cold disks.
Related posts
- ClickHouse Release 25.4
ClickHouse mediumAdept
ClickHouse 25.4 adds lazy materialization, which defers reading column data until it is needed. On a Top-N query over Amazon reviews it cut runtime from 219s to 0.139s, with 40× less I/O and 300× less memory. Other additions are MergeTree tables on read-only object storage that refresh parts, allowing one writer and unlimited readers for data sharing, and a CPU workload scheduler. The scheduler uses CPU resources and hierarchical workloads with max_concurrent_threads and weights. The release also brings experimental correlated subqueries in EXISTS, Apache Iceberg time travel via snapshot timestamps, a table-level default compression codec, persistent databases in clickhouse-local, and SSH access to the public playground.
- Handling Updates and Deletes in ClickHouse
ClickHouse mediumAdept
ClickHouse offers several ways to modify mostly-immutable analytical data, each with different costs. Lightweight Deletes (DELETE FROM) write a _row_exists mask and rely on background merges to physically remove rows, which makes them cheap but leaves space reclamation non-deterministic. ALTER UPDATE/DELETE mutations rewrite whole parts, so they are heavy but guarantee removal, which matters for compliance. Cheaper alternatives cover specific cases: swapping in a new column with a DEFAULT for whole-table updates, Join engine plus joinGet for join-based updates, dropping partitions or using TTL for bulk and time-based deletion, and CollapsingMergeTree sign rows for frequent row-level updates.
- Building a product analytics solution with ClickHouse
ClickHouse mediumAdept
ClickHouse's in-house product analytics platform, Galaxy, stores over 20 billion events (14 TB, roughly 14x compression) in a single denormalized MergeTree table keyed on created_at. A single sparse table avoids join overhead, since ClickHouse's sparse serialization makes empty columns cheap. Columns used for cohorts and filters should be top-level rather than buried in a JSON payload. Incremental materialized views speed up the few dozen recurring queries, and a custom SDK handles event capture.
- ClickHouse Release 26.3
ClickHouse mediumAdept
ClickHouse 26.3 ships 27 features, 40 performance optimizations and 202 bug fixes. Headline changes are asynchronous inserts enabled by default, JOIN reordering for ANTI/SEMI/FULL joins, and MATERIALIZED CTEs, which evaluate a WITH subquery once into a temporary table. In a UK property-prices example, materializing the CTE roughly halved query time and cut peak memory from 1.5 GiB to about 88 MiB. The release also adds pretty/compact EXPLAIN output, naturalSortKey, JSONExtract support for the JSON type, and experimental WebAssembly UDFs sandboxed with Wasmtime. TTL DELETE can now use vertical merges, and the post explains how horizontal and vertical merges work and when each is chosen.
- ClickHouse Release 23.12
ClickHouse mediumAdept
ClickHouse 23.12 adds experimental Refreshable Materialized Views, which periodically re-run an arbitrary query and atomically replace the results in a target table. That makes complex multi-join aggregations practical where incremental insert-trigger views fall short, and they can also serve as cron-style import/export jobs chained with DEPENDS. In the IMDB example, a refreshable view cuts an actor-summary query from 1.2 s to 3 ms. The release also speeds up SELECT ... FINAL by splitting data into non-intersecting sorting-key ranges, which are processed in parallel without merge logic, so only intersecting ranges pay the merge cost and merges are skipped across partitions when the partition key is a prefix of the sorting key.