Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

ClickHouse 13 min read How-to mediumAdept

Handling Updates and Deletes in ClickHouse

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.

Related posts

  • How we built fast UPDATEs for the ClickHouse column store – Part 2: SQL-style UPDATEs

    ClickHouse hardMaster

    ClickHouse implemented standard SQL UPDATE in its MergeTree column store by evolving from classic mutations (which rewrite whole affected columns and hard-link unchanged ones) through lightweight DELETEs via a _row_exists mask column and on-the-fly in-memory updates, to patch parts. A patch part is a compact delta containing only changed column values plus _part and _part_offset metadata. It is visible to queries immediately and applied during normal background merges in a single linear pass, with no resorting or extra indexing. This avoids full column rewrites and blocking on prior merges, making frequent row-level updates practical (experimental in 25.7, beta expected in 25.8).

  • ClickHouse Newsletter April 2023: Lightweight Deletes

    ClickHouse mediumAdept

    ClickHouse 23.3 makes lightweight deletes (DELETE FROM with a row condition) generally available and the preferred way to remove data, and also adds trace visualization to the Grafana plugin and parallel replicas that split a shard's data across replicas. Lightweight deletes simplify incremental updates of denormalized, JOIN-materialized tables. Instead of copying the whole target table while excluding changed ids and then swapping it in with EXCHANGE, changed rows go into a temporary table, are deleted from the target by id, and are re-inserted. The dbt-clickhouse plugin now recommends this approach for incremental materializations.

  • How we built fast UPDATEs for the ClickHouse column store – Part 1: Purpose-built engines

    ClickHouse mediumAdept

    ClickHouse avoids slow in-place row updates in its column store by expressing updates and deletes as inserts that get resolved during the background merges MergeTree already performs. Sorted, immutable data parts can be merged in a single linear pass, so keeping only the latest version adds almost no overhead. ReplacingMergeTree keeps the latest full row per sorting key, CoalescingMergeTree merges sparse partial updates using Nullable columns, and CollapsingMergeTree cancels rows with a sign column, which also lets it change sorting key values. All three give UPSERT semantics but are only eventually consistent, so queries that need current results use FINAL to apply the merge logic in memory at read time.

  • ClickHouse and dbt - A Gift from the Community

    ClickHouse mediumAdept

    The dbt-clickhouse adapter, started by community contributor Dmitriy Sokolov and later taken over as a vendor-supported plugin, maps dbt's view, table, incremental and ephemeral materializations onto ClickHouse features. The default incremental strategy builds a temporary table of changed rows, copies all unchanged rows into a new table and atomically swaps it in, which duplicates the whole dataset on every run. A newer delete+insert strategy uses ClickHouse 22.8's experimental lightweight deletes (tombstones removed during merges) to write only the changed rows and performs significantly better. Its caveats: it needs the experimental setting enabled, and because it modifies the target table directly with no transaction, a failure can leave the model in an invalid state.

  • ClickHouse Release 25.7

    ClickHouse mediumAdept

    ClickHouse 25.7 adds standard SQL UPDATE and DELETE built on lightweight "patch parts". Instead of rewriting columns, small immutable parts are applied during background merges and matched at query time, making declarative updates up to 1,000× faster in ClickHouse's benchmarks. count() GROUP BY aggregations now store the 64-bit count directly in the hash table cell, skipping the memory arena and pointer indirection, which cut query time ~34% and memory ~21% on a 100M-row benchmark. The release also brings four hash-join optimizations: tighter single-key loops, faster OR-condition joins, no repeated hashing, and precisely sized result buffers, worth roughly 1.4–1.8× speedups, plus AI-powered SQL generation in clickhouse-client via a `??` prefix.

The quest for software engineering knowledge.

Look:
Layout: