INSERT INTO ... SELECT keeps in memory the rows that are about to become new data parts: for every insert thread, a block of up to min_insert_block_size_rows rows or min_insert_block_size_bytes bytes. If the query fails with MEMORY_LIMIT_EXCEEDED (code 241), reduce the number of insert threads first, and the size of the blocks second.
The error
Query is max_memory_usage, User is max_memory_usage_for_user, and (total) is the memory limit of the server. When the message ends with OvercommitTracker decision: Query was selected to stop by OvercommitTracker, the limit of the user or of the server was reached and this query was chosen as the one to stop; see Memory overcommit.
Where the memory goes
- The
SELECTpart reads the source with up tomax_threadsthreads. - The rows flow into up to
max_insert_threadsparallel insert streams, but not more than the number of streams that theSELECTproduces. - Each insert stream collects rows until it has
min_insert_block_size_rowsrows (1048449 by default) ormin_insert_block_size_bytesbytes (268402944 by default, about 256 MiB), whichever comes first. It then sorts the block and writes it as a new part, or as one part for each partition that the block touches.
max_insert_threads is 0, which means the number of CPU cores, so an INSERT ... SELECT writes in parallel. Before version 26.8 the default was 1. The settings reference lists the lower defaults that ClickHouse Cloud uses.
Example
Create a source table with 6 million rows of about 120 bytes each, and an empty copy:SETTINGS clause at the end of the query applies to both the SELECT and the INSERT part.
What each setting costs
The following table shows the peak memory (memory_usage in system.query_log) and the number of new parts for the same copy without a memory limit. The numbers are from one test machine and differ on yours, but the direction of each change is the same.
- Fewer insert threads lower the memory without changing the number of parts; the insert takes longer. Setting
max_threads = 1has the same effect, because the insert cannot have more streams than theSELECT, but it also makes the reading single-threaded. - Smaller blocks lower the memory further, but every block becomes a part: ten times smaller blocks mean ten times more parts for the background merges to process. For wide rows, lower
min_insert_block_size_bytesinstead of the number of rows. - Do not go too low. Blocks are not split, only combined, so a value like
min_insert_block_size_rows = 1000turns every block that theSELECTproduces into a part of its own: 733 parts instead of 6 in this test. On a large table this can end with delayed inserts and theTOO_MANY_PARTSerror.
Other things to check
- A failed
INSERT ... SELECTis not rolled back. The blocks that were written before the error stay in the target table. Remove them before you retry, for example withTRUNCATE TABLEif the table was empty before. - The
SELECTitself can be the consumer. If it has aGROUP BY, anORDER BYor aJOIN, see Memory limit exceeded for query. - Wide target tables and many partitions. Every block is split by the partition key of the target table, and every open part reserves write buffers for each column. See Why does a small
INSERTinto a table with many columns use so much memory? - The settings that make inserts larger and faster instead are described in How do I solve the
TOO_MANY_PARTSerror during anINSERT ... SELECT?