Learn Labs
Storage Engine

Parts Lab: Small Inserts, Merges and Batch Size

Why ClickHouse wants big inserts, measured, and what the 26.2 async_insert default changed

Every INSERT into a MergeTree table writes a new, immutable part. Background merges combine parts later. labs/parts_merges.py counts them.

docker compose -f compose_prod.yaml run --rm labs python parts_merges.py

1,000 rows, three ways

time to write 1,000 rows1,000 single-row INSERTs, sync0.9 s, 1,000 parts1,000 single-row INSERTs, 26.x default59.4 s, 1,000 parts20 clients x 50, async_insert9.5 s, 69 partsone INSERT of 1,000 rows0.004 s, 1 part

One part per insert, whatever the insert size: 1,000 single-row inserts made 1,000 parts, one 1,000-row insert made one.

The first version of this lab reported 59 ms per single-row insert, which is far too slow for a local write. The server's own query_log agreed (57 ms), but only 0.26 ms of that was CPU. A trace log showed every insert going through AsynchronousInsertQueue: async_insert is on by default since ClickHouse 26.2. With one client inserting one row at a time, each insert waits for the flush timer and is then written alone. The lab now turns it off explicitly for the synchronous case (0.9 s for all 1,000) and shows the default separately. More in Async Inserts.

Merges catch up

2. Background merges
 t=  0.0s  1000 active parts
 t=  1.0s  706 active parts
 t=  3.6s  412 active parts
 t=  6.3s  118 active parts
 t=  7.3s  1 active parts

A thousand tiny parts merged into one in about 7 seconds. That is fine once. The problem is a client that keeps producing them.

Too many parts

With merges stopped and the limits lowered to make it quick (parts_to_delay_insert = 20, parts_to_throw_insert = 50):

inserts 1-20: 1 ms average; 21-50: 523 ms average (deliberately delayed)
insert 51 failed: Code: 252. DB::Exception: Too many parts (50 with average size of 974.00 B)

ClickHouse first slows inserts down, then refuses them. The defaults in 26.8 are 1,000 and 3,000 parts per partition. The ClickHouseTooManyParts alert fires at 500.

Batch size

2,000,000 rows built in Python and sent over HTTPS:

batches of 1,0000.50 M rows/sbatches of 10,0002.74 M rows/sbatches of 100,0004.90 M rows/sbatches of 1,000,0005.70 M rows/s

Going from 1,000-row to 10,000-row batches is worth 5.5x. Past 100,000 rows there is little left to gain. A common rule, a batch at least every second and at least thousands of rows, comes straight from these numbers.

On this page