Bulk insert data into ClickHouse and deduplicate rows using ReplacingMergeTree

domain: clickhouse · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Create a table with the ReplacingMergeTree engine, specifying a version column (e.g., updated_at UInt64 or DateTime) as the engine parameter: ENGINE = ReplacingMergeTree(version); set the ORDER BY clause to your natural deduplication key (the combination of columns that identifies a unique logical row)
  2. Insert data in large batches rather than row-by-row: ClickHouse is optimized for bulk inserts of at least 1,000–100,000 rows per INSERT statement; small frequent inserts create many small parts and degrade merge performance
  3. Use the HTTP interface or native protocol with async_insert=1 for high-throughput streaming ingestion where batching at the client is impractical; ClickHouse will buffer and merge writes server-side
  4. Understand that ReplacingMergeTree deduplication happens lazily during background merges — immediately after insert, duplicate rows exist and will be returned by SELECT; use FINAL modifier (SELECT ... FROM table FINAL) to force deduplication at query time, or use the argMax pattern for latest-value queries
  5. Use INSERT with SETTINGS max_insert_block_size and adjust max_partitions_per_insert_block if inserting across many partitions; partition by a low-cardinality column like toYYYYMM(event_date) not by a high-cardinality field
  6. Monitor part count via SELECT count() FROM system.parts WHERE table='<table>' AND active=1; a very high part count (thousands) indicates merges are falling behind inserts and query performance will degrade

Known gotchas

Related routes

Use ClickHouse ReplacingMergeTree for upsert semantics and manage deduplication
clickhouse · 6 steps · unrated
Define ClickHouse MergeTree TTL expressions for automatic row deletion and data tiering to cold storage volumes
clickhouse.com/docs · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans