> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Why does a small INSERT into a table with many columns use so much memory?

> An INSERT that writes Wide parts reserves about 2 MiB of write buffers per column stream of every open part, whatever the number of rows. How to find such inserts and reduce the memory.

When ClickHouse writes a data part in the `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 setting `min_bytes_for_wide_part = 0` makes every part `Wide`, whatever its size. The shell expands `$(seq ...)` into the list of 100 columns.

```bash theme={null}
clickhouse-client --query "
    CREATE TABLE metrics (day Date, id UInt64, $(seq -f 'm%g Float64' -s ', ' 1 100))
    ENGINE = MergeTree
    PARTITION BY day
    ORDER BY id
    SETTINGS min_bytes_for_wide_part = 0"

clickhouse-client --query "
    INSERT INTO metrics
    SELECT toDate('2026-01-01') + number % 10, number, $(seq -f 'number + %g' -s ', ' 1 100)
    FROM numbers(5000)"
```

[`system.query_log`](https://clickhouse.com/docs/reference/system-tables/query_log) shows what the insert cost:

```sql theme={null}
SYSTEM FLUSH LOGS;

SELECT
    written_rows,
    formatReadableSize(written_bytes) AS written,
    formatReadableSize(memory_usage) AS memory,
    ProfileEvents['InsertedWideParts'] AS wide_parts,
    ProfileEvents['InsertedCompactParts'] AS compact_parts
FROM system.query_log
WHERE event_date >= today() - 1
    AND type = 'QueryFinish'
    AND query_kind = 'Insert'
    AND has(tables, 'default.metrics')
ORDER BY event_time_microseconds;
```

```text theme={null}
   ┌─written_rows─┬─written──┬─memory─────┬─wide_parts─┬─compact_parts─┐
1. │         5000 │ 3.86 MiB │ 448.88 MiB │         10 │             0 │
   └──────────────┴──────────┴────────────┴────────────┴───────────────┘
```

The insert wrote less than 4 MiB of data and used about 450 MiB of memory. The memory figures in this article vary by several MiB between runs.

## Where the memory goes

* **Two buffers per stream.** A `Wide` part stores every column in its own files. For each of them the writer allocates a file buffer and a compression buffer of [`max_compress_block_size`](https://clickhouse.com/docs/reference/settings/session-settings/max#max_compress_block_size) bytes 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`, `LowCardinality` or `String` is written as two streams, and a `Map(String, Float64)` as four. In a test that wrote one part with 50 columns of the same type, `Float64` columns took 110 MiB, `Nullable(Float64)` or `String` columns 213 MiB, and `Map(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_write`](https://clickhouse.com/docs/reference/settings/session-settings/max-insert#max_insert_delayed_streams_for_parallel_write) streams 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 of `100000`, 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 ... SELECT` of 400000 rows into two partitions of this table used 484 MiB with `max_insert_threads = 1` and 958 MiB with two insert threads.

The number of rows does not matter: an insert of 10 rows into the same 10 partitions used 436 MiB.

## How to find such inserts

Look for `INSERT` queries with high memory usage that created `Wide` parts:

```sql theme={null}
SELECT
    event_time,
    query_id,
    written_rows,
    formatReadableSize(memory_usage) AS memory,
    ProfileEvents['InsertedWideParts'] AS wide_parts,
    ProfileEvents['InsertedCompactParts'] AS compact_parts
FROM system.query_log
WHERE event_date >= today() - 1
    AND type = 'QueryFinish'
    AND query_kind = 'Insert'
    AND memory_usage > 100000000
ORDER BY memory_usage DESC
LIMIT 10;
```

Then check how many columns the table has, and how many parts and partitions the inserts create in [`system.part_log`](https://clickhouse.com/docs/reference/system-tables/part_log):

```sql theme={null}
SELECT count() AS columns
FROM system.columns
WHERE database = 'default' AND table = 'metrics';
```

```text theme={null}
   ┌─columns─┐
1. │     102 │
   └─────────┘
```

```sql theme={null}
SELECT
    part_type,
    count() AS parts,
    uniqExact(partition_id) AS partitions,
    sum(rows) AS rows
FROM system.part_log
WHERE event_date >= today() - 1
    AND event_type = 'NewPart'
    AND database = 'default'
    AND table = 'metrics'
GROUP BY part_type;
```

```text theme={null}
   ┌─part_type─┬─parts─┬─partitions─┬─rows─┐
1. │ Wide      │    10 │         10 │ 5000 │
   └───────────┴───────┴────────────┴──────┘
```

## How to reduce the memory

**Use adaptive write buffers.** With adaptive buffers a stream starts with a buffer of [`adaptive_write_buffer_initial_size`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/other#adaptive_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`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/min#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:

```sql theme={null}
ALTER TABLE metrics MODIFY SETTING min_columns_to_activate_adaptive_write_buffer = 50;
```

The same insert now uses about 38 MiB and still writes 10 `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`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max#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`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/min-bytes#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:

```sql theme={null}
ALTER TABLE metrics RESET SETTING min_bytes_for_wide_part;
```

The same insert then writes 10 `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](https://clickhouse.com/docs/resources/support-center/knowledge-base/data-management/understanding-part-types-and-storage-formats.md) 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`](https://clickhouse.com/docs/reference/settings/session-settings/max-partitions#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`](https://clickhouse.com/docs/reference/settings/session-settings/max-insert#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`](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/memory-limit-exceeded-insert-select).
