Overview

MergeTree is the core table engine family. Every INSERT writes a new immutable data part (a directory of column files). Background merges combine small parts into larger sorted ones. Understanding parts explains write performance, “too many parts” errors, and why deletes, updates and de-duplication are not immediate.

Core concepts

  • A part holds a block of rows sorted by the table key, one file per column plus the sparse primary index (primary.idx) and marks. Names look like all_1_1_0 (partition_minBlock_maxBlock_level); a mutation adds a version suffix (all_1_1_0_4) (ch-mutations).
  • Wide parts store each column in its own file; Compact parts keep small data in one file (for small inserts).
  • A big INSERT is split into blocks of about max_insert_block_size (1,048,449 rows by default), each becoming a part.
  • Merges run in the background with a pool of threads; they pick parts in the same partition (ch-partitions), merge-sort them and write a bigger part, then delete the sources. The default cap for merge size is 150 GiB (max_bytes_to_merge_at_max_space_in_pool).
  • Backpressure: when active parts in a partition exceed parts_to_delay_insert (1000) inserts are slowed; at parts_to_throw_insert (3000) they fail with TOO_MANY_PARTS.
  • Engines in the family add behaviour during merges: ReplacingMergeTree, SummingMergeTree, AggregatingMergeTree, CollapsingMergeTree (ch-replacing-collapsing-engines), and Replicated* variants (ch-replication-keeper).

Worked example

One INSERT of 5,000,000 rows into events_a created 5 active parts (about five 1M-row blocks), and system.part_log recorded 10 NewPart events and 5 RemovePart events as merges worked on them. Two experiments with 200 single-row inserts into a table with merges paused:

MethodParts after 200 insertsTime
INSERT ... SETTINGS async_insert=0 (one part per insert)2000.7 s
async_insert=1, wait_for_async_insert=1 (each statement waits for its own flush)20012.2 s
async_insert=1, wait_for_async_insert=0 (batched in the server buffer)20.2 s

After OPTIMIZE TABLE small_ins FINAL, the 200 parts merged into 1. Async insert is enabled by default in this version, but waiting for each flush serialises clients; batching across clients and not waiting is what produces few parts (ch-ingestion).

SELECT table, count() AS parts, sum(rows) AS rows
FROM system.parts WHERE active AND database = 'kb' GROUP BY table;

Pitfalls and production notes

  • Insert in large batches (tens of thousands to millions of rows) or use async inserts; avoid one part per row.
  • Watch system.parts per partition and system.merges; sustained high counts mean inserts outrun merges or merges are starved.
  • OPTIMIZE ... FINAL forces a full rewrite; use it sparingly on large tables.
  • Disk space needs head-room for merges (a merge writes the new part before deleting old ones).
  • Compare to PostgreSQL heap and vacuum (pg-vacuum-bloat) and SQL Server pages (sqlserver-storage-tempdb): the unit of change is the part.

Practice questions

  1. Why does ClickHouse prefer a few large inserts to many small ones?
  2. What happens at 3,000 active parts in one partition?
  3. How do you see which merges are running now?

References

  • ClickHouse documentation, MergeTree table engine; Data parts

ch-columnar-model · ch-sorting-key-sparse-index · ch-ingestion · ch-operations-monitoring