TOO_MANY_PARTS) is raised for several different limits, and only the message text tells you which one. Two of the messages point to the partition key and not to slow merges: Too many parts (N) in all partitions in total and Too many partitions for single INSERT block. Parts are merged only within one partition, so a table can never have fewer active parts than partitions. If the two numbers are close, merges have nothing left to do, and the fix is a coarser partition key.
Which limit was reached
The first row is the classic case of too many small inserts, covered in Resolving the “Too many parts” exception. The rest of this article is about the second and the third row.
To see which limits your inserts reached, group the failed queries by message:
Example
The table is partitioned by day and tenant. To keep the example small,max_parts_in_total is lowered to 100.
max_parts_in_total, so the next insert fails on the other limit:
system.query_log returns:
Compare the number of parts with the number of partitions
OPTIMIZE TABLE metrics FINAL leaves 120 parts. When active_parts is close to partitions:
- Don’t look for the cause in merge throughput or insert batching. The number of partitions is the lower bound for the number of parts.
- Raising
max_parts_in_totalonly buys time until the number of partitions grows to the new value. - Check which elements of the partition key multiply the count. A tuple key produces up to one partition for every combination of its elements. Here it is 40 days times 3 tenants:
Fix the partition key
Compare the partition key with the sorting key. In the exampletenant_id is the first column of ORDER BY, so the primary index already narrows a query to one tenant, and repeating the column in PARTITION BY only multiplies the number of partitions. A daily partition is also finer than this amount of data needs. See Choosing a partitioning key for guidance.
The partition key of an existing table can’t be changed: there is no ALTER TABLE ... MODIFY PARTITION BY. Create a new table, copy the data, and swap the tables:
system.parts now returns:
metrics_new (the old data) when the result is verified. If old partitions are no longer needed, a TTL or ALTER TABLE ... DROP PARTITION also lowers the count, but it doesn’t change how fast new partitions appear.
When one INSERT spans many partitions of a well-partitioned table
Too many partitions for single INSERT block can also appear with a reasonable key, for example when a backfill loads several months into a table partitioned by day. The options are:
- Split the load so that each
INSERTcovers fewer partitions, for example one month per query. - Raise
max_partitions_per_insert_blockfor that query, or set it to 0 to remove the limit, as in the example above. - Set
throw_on_max_partitions_per_insert_blockto 0 to log a warning and continue.
INSERT query, in the session, or in the settings profile of the user that runs the insert. A SETTINGS clause inside the SELECT of a materialized view doesn’t apply to the write into the view’s target table, so set them on the INSERT into the source table.
Every partition in an inserted block becomes a separate part. Lifting the limit is fine for a bounded backfill into a table with a coarse key. On a table that is already close to max_parts_in_total it moves the failure from one message to the other, as in the example.