Wide format, it reserves two write buffers of 1 MiB for every column stream of the part before it writes the first row. For a table with hundreds of columns this is hundreds of megabytes per part, however few rows you insert. An INSERT whose rows belong to several partitions, or that runs with several insert threads, holds the buffers of several parts at the same time.
Reproduce the behaviour
The following commands create a table with 102 columns, partitioned by day, and insert 5000 rows that belong to 10 partitions. The settingmin_bytes_for_wide_part = 0 makes every part Wide, whatever its size. The shell expands $(seq ...) into the list of 100 columns.
system.query_log shows what the insert cost:
Where the memory goes
- Two buffers per stream. A
Widepart stores every column in its own files. For each of them the writer allocates a file buffer and a compression buffer ofmax_compress_block_sizebytes each (1 MiB by default). A part with 102 simple columns therefore reserves a little more than 200 MiB. - Several streams per column. A column of the type
Nullable,Array,LowCardinalityorStringis written as two streams, and aMap(String, Float64)as four. In a test that wrote one part with 50 columns of the same type,Float64columns took 110 MiB,Nullable(Float64)orStringcolumns 213 MiB, andMap(String, Float64)columns 421 MiB. - One part per partition. ClickHouse splits the inserted block by the partition key and writes a part for each partition. The previous part is finalized only after the next one is written, so two parts are open at the same time, which gives the 449 MiB above. On local disks it does not grow further with the number of partitions.
- More open parts on object storage. On disks that support parallel writes (S3, Azure Blob Storage) ClickHouse delays the finalization of parts to upload their files in parallel. It keeps parts open until they have
max_insert_delayed_streams_for_parallel_writestreams together (100 by default on such disks), which matters for tables with fewer columns than that. The setting also works on a local disk if you set it explicitly: with a value of100000, an insert into 6 partitions of this table kept all six parts open and used 1.26 GiB. - Insert threads. Every parallel insert stream writes its own parts. An
INSERT ... SELECTof 400000 rows into two partitions of this table used 484 MiB withmax_insert_threads = 1and 958 MiB with two insert threads.
How to find such inserts
Look forINSERT queries with high memory usage that created Wide parts:
system.part_log:
How to reduce the memory
Use adaptive write buffers. With adaptive buffers a stream starts with a buffer ofadaptive_write_buffer_initial_size bytes (16 KiB by default) that grows as data arrives. They are used when a part has at least min_columns_to_activate_adaptive_write_buffer streams (500 by default; the setting is available since version 26.1). Lower the threshold for the table:
Wide parts. Since version 26.11 adaptive buffers are also activated automatically when the buffers of one part would take at least 1/32 of the memory limit of the server (max_memory_ratio_to_activate_adaptive_write_buffer).
Let small parts be Compact. A Compact part stores all columns in one file, so its writer does not reserve large buffers for every column. With the default min_bytes_for_wide_part of 10 MiB, a new part is Compact if its uncompressed size is below the threshold. If the table overrides the setting, as the example table does, remove the override:
Compact parts and uses about 30 MiB. The default does not help with larger inserts: 40000 rows (31 MiB) in two partitions of a table with default settings were written as two Wide parts and used 512 MiB. See Understanding part types and storage formats for the trade-offs between the formats.
Insert into fewer partitions at a time. An insert of the same rows into one partition of the example table used 225 MiB, half as much. Group the rows by the partition key on the client, or choose a coarser partition key. Writing to many partitions also creates many small parts; the setting max_partitions_per_insert_block (100 by default) rejects inserts that touch more partitions than that with the error TOO_MANY_PARTS.
Limit the parallelism of the write. Set max_insert_threads to 1 for inserts into very wide tables. On object storage, max_insert_delayed_streams_for_parallel_write limits how many streams stay open: a lower value trades the parallelism of the upload for memory.
For the memory used by the rows themselves in an INSERT ... SELECT, see How to reduce memory usage of INSERT INTO ... SELECT.