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

Tom Schreiber
14 min readbeginner
--
View Original

Overview

This article discusses how ClickHouse addresses the challenges of row-level updates in column stores by treating updates as inserts. It introduces purpose-built engines like ReplacingMergeTree, CoalescingMergeTree, and CollapsingMergeTree, which enhance performance for fast-changing data workloads.

What You'll Learn

1

How to use ReplacingMergeTree for efficient row updates in ClickHouse

2

Why ClickHouse's insert-based model is advantageous for high-throughput scenarios

3

How to implement partial updates using CoalescingMergeTree

4

When to use CollapsingMergeTree for efficient deletes in ClickHouse

Key Questions Answered

How does ClickHouse handle updates without traditional UPDATE operations?
ClickHouse sidesteps the performance challenges of row-level updates by treating updates as inserts. It uses purpose-built engines like ReplacingMergeTree, CoalescingMergeTree, and CollapsingMergeTree to manage updates efficiently without modifying existing rows directly.
What are the advantages of using an insert-based model for updates?
The insert-based model allows ClickHouse to maintain high insert throughput and avoid the performance penalties associated with in-place updates. This method leverages background merges to consolidate data, ensuring that updates and deletes are handled efficiently.
What is the role of the FINAL modifier in ClickHouse?
The FINAL modifier allows queries to apply the table engine's merge logic on the fly, providing up-to-date results by consolidating relevant data parts in memory. This ensures accuracy in query results without triggering a real merge on disk.
How does CollapsingMergeTree facilitate deletes in ClickHouse?
CollapsingMergeTree allows users to delete rows by inserting a special 'cancelling' row that marks the original as invalid. During background merges, ClickHouse collapses these rows, effectively removing the original data.

Key Statistics & Figures

Insert throughput
over 1 billion rows per second
This performance was observed in a production setup, highlighting ClickHouse's optimization for insert workloads.
Speedup for bulk changes compared to PostgreSQL
up to 4,000× faster
This statistic demonstrates ClickHouse's efficiency in handling bulk updates compared to traditional row-based databases.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Key Actionable Insights

1
Utilize ReplacingMergeTree for scenarios where data is frequently updated to maintain performance.
This engine allows you to insert new versions of rows, ensuring that the latest data is always available without the overhead of traditional updates.
2
Consider using CoalescingMergeTree when only partial updates are necessary, as it allows for more efficient data management.
By marking non-key columns as Nullable, you can insert only the fields that have changed, reducing the amount of data processed during merges.
3
Leverage the FINAL modifier in queries when immediate consistency is required, especially in high-ingest scenarios.
This approach ensures that users receive the most accurate results without waiting for background merges to complete.

Common Pitfalls

1
Users may struggle with the concept of treating updates as inserts, leading to confusion about how to model their data.
This misunderstanding can result in inefficient data management practices. It's crucial to adjust thinking to align with ClickHouse's architecture and update mechanics.

Related Concepts

Columnar Databases
Data Merging Strategies
Sql-style Updates