Overview
ClickHouse ingests best in large batches. Every insert block becomes a part (ch-mergetree-parts), so how you batch decides throughput and stability. Sources: client inserts, INSERT ... SELECT, files and object storage, Kafka and other table engines, and ingestion pipelines.
Core concepts
- Client batching: insert 10,000 to 1,000,000 rows per statement in an efficient format (
Native,RowBinary, orJSONEachRow/CSVwhen convenient). - Async inserts (
async_insert = 1): the server buffers small inserts and flushes them together on size (async_insert_max_data_size, 10 MiB), time (async_insert_busy_timeout_*, adaptive from 50 ms) or count.wait_for_async_insert = 1makes each client wait for the flush (durability confirmation),0returns immediately. - Formats and table functions:
INSERT INTO t FORMAT CSV,file(),url(),s3(),s3Cluster(), and theINFILEclause in the client. - Table engines as sources:
Kafka,RabbitMQ,S3Queueand CDC pipelines write into MergeTree through a materialized view (ch-materialized-views). Managed connectors (ClickPipes, Kafka Connect, Vector, Airbyte) are common in practice. - Insert deduplication: replicated tables ignore identical blocks within a window, which makes client retries safe (ch-replication-keeper).
- Distributed inserts: insert into the Distributed table (it splits rows by the sharding key) or directly into local tables on each shard (ch-sharding-distributed).
Worked example
Two hundred single-row INSERT statements into an empty MergeTree table with merges paused:
| Mode | Parts created | Client time |
|---|---|---|
async_insert=0 | 200 | 0.7 s |
async_insert=1, wait_for_async_insert=1 | 200 | 12.2 s (each waits for its flush) |
async_insert=1, wait_for_async_insert=0 | 2 | 0.2 s |
Defaults observed on ClickHouse 26.10: async_insert = 1, wait_for_async_insert = 1, async_insert_max_data_size = 10485760, async_insert_use_adaptive_busy_timeout = 1, max_insert_block_size = 1048449. A 5M-row INSERT ... SELECT created five parts.
The takeaway: batching happens when many clients send concurrently or do not wait; a single client waiting on each statement serialises on the flush timeout.
Pitfalls and production notes
- Keep an eye on
system.partscounts per partition and theTOO_MANY_PARTSthreshold (1,000 delay, 3,000 error). - Async insert trades a small window of potential loss (if you do not wait) for throughput; use
wait_for_async_insert = 1with many concurrent writers when you need acknowledgement. - Make pipelines idempotent: use deterministic block boundaries or insert tokens (
insert_deduplication_token). - Do not use ClickHouse as a queue; put Kafka in front for buffering.
- Schema changes: add columns with defaults (cheap metadata change) and avoid rewriting history (safe-schema-migrations).
Practice questions
- Why did 200 sequential waiting async inserts still create 200 parts?
- How do replicated tables make retried inserts safe?
- Which three settings control when the async buffer flushes?
References
- ClickHouse documentation, Asynchronous Inserts; Bulk inserts best practices
Related
ch-mergetree-parts · ch-materialized-views · ch-replication-keeper · ch-operations-monitoring