Project: ClickHouse (App Definition and Development / Database)
File: query-complexity.md

slug: /en/operations/settings/query-complexity sidebar_position: 59 sidebar_label: Restrictions on Query Complexity Restrictions on query complexity are part of the settings. They are used to provide safer execution from the user interface. Almost all the restrictions only apply to `SELECT`. For distributed query processing, restrictions are applied on each server separately. ClickHouse checks the restrictions for data parts, not for each row. It means that you can exceed the value of restriction with the size of the data part. Restrictions on the maximum amount of something can take the value 0, which means unrestricted. Most restrictions also have an overflow_mode setting, meaning what to do when the limit is exceeded. It can take one of two values: `throw` or `break`. Restrictions on aggregation (groupbyoverflow_mode) also have the value `any`. `throw` Throw an exception (default). `break` Stop executing the query and return the partial result, as if the source data ran out. `any (only for groupbyoverflow_mode)` Continuing aggregation for the keys that got into the set, but do not add new keys to the set. The maximum amount of RAM to use for running a query on a single server. The default setting is unlimited (set to `0`). Cloud default value: depends on the amount of RAM on the replica. The setting does not consider the volume of available memory or the total volume of memory on the machine. The restriction applies to a single query within a single server. You can use `SHOW PROCESSLIST` to see the current memory consumption for each query. Besides, the peak memory consumption is tracked for each query and written to the log. Memory usage is not monitored for the states of certain aggregate functions. Memory usage is not fully tracked for states of the aggregate functions `min`, `max`, `any`, `anyLast`, `argMin`, `argMax` from `String` and `Array` arguments. Memory consumption is also restricted by the parameters `maxmemoryusageforuser` and . The maximum amount of RAM to use for running a users queries on a single server. Default values are defined in . By default, the amount is not restricted (`maxmemoryusageforuser = 0`). See also the description of . The following restrictions can be checked on each block (instead of on each row). That is, the restrictions can be broken a little. A maximum number of rows that can be read from a table when running a query. A maximum number of bytes (uncompressed data) that can be read from a table when running a query. What to do when the volume of data read exceeds one of the limits: throw or break. By default, throw. The following restrictions can be checked on each block (instead of on each row). That is, the restrictions can be broken a little. A maximum number of rows that can be read from a local table on a leaf node when running a distributed query. While distributed queries can issue a multiple sub-queries to each shard (leaf) - this limit will be checked only on the read stage on the leaf nodes and ignored on results merging stage on the root node. For example, cluster consists of 2 shards and each shard contains a table with 100 rows. Then distributed query which suppose to read all the data from both tables with setting `maxrowsto_read=150` will fail as in total it will be 200 rows. While query with `maxrowstoreadleaf=150` will succeed since leaf nodes will read 100 rows at

A maximum number of bytes (uncompressed data) that can be read from a local table on a leaf node when running a distributed query. While distributed queries can issue a multiple sub-queries to each shard (leaf) - this limit will be checked only on the read stage on the leaf nodes and ignored on results merging stage on the root node. For example, cluster consists of 2 shards and each shard contains a table with 100 bytes of data. Then distributed query which suppose to read all the data from both tables with setting `maxbytesto_read=150` will fail as in total it will be 200 bytes. While query with `maxbytestoreadleaf=150` will succeed since leaf nodes will read 100 bytes at max. What to do when the volume of data read exceeds one of the leaf limits: throw or break. By default, throw. A maximum number of unique keys received from aggregation. This setting lets you limit memory consumption when aggregating. What to do when the number of unique keys for aggregation exceeds the limit: throw, break, or any. By default, throw. Using the any value lets you run an approximation of GROUP BY. The quality of this approximation depends on the statistical nature of the data. Enables or disables execution of `GROUP BY` clauses in external memory. See . Possible values: Maximum volume of RAM (in bytes) that can be used by the single operation. 0 `GROUP BY` in external memory disabled. Default value: `0`. Cloud default value: half the memory amount per replica. Enables or disables execution of `ORDER BY` clauses in external memory. See Maximum volume of RAM (in bytes) that can be used by the single operation. Recommended value is half of available system memory 0 `ORDER BY` in external memory disabled. Default value: 0. Cloud default value: half the memory amount per replica. A maximum number of rows before sorting. This allows you to limit memory consumption when sorting. A maximum number of bytes before sorting. What to do if the number of rows received before sorting exceeds one of the limits: throw or break. By default, throw. Limit on the number of rows in the result. Also checked for subqueries, and on remote servers when running parts of a distributed query. No limit is applied when value is `0`. Default value: `0`. Cloud default value: `0`. Limit on the number of bytes in the result. The same as the previous setting. What to do if the volume of the result exceeds one of the limits: throw or break. Using break is similar to using LIMIT. `Break` interrupts execution only at the block level. This means that amount of returned rows is greater than , multiple of and depends on . Default value: `throw`. Cloud default value: `throw`. Example: ``` sql SET maxthreads = 3, maxblock_size = 3333; SET maxresultrows = 3334, resultoverflowmode = 'break'; SELECT * FROM numbers_mt(100000) FORMAT Null; ``` Result: ``` text 6666 rows in set. ... ``` Maximum query execution time in seconds. At this time, it is not checked for one of the sorting stages, or when merging and finalizing aggregate functions. The `maxexecutiontime` parameter can be a bit tricky to understand. It operates based on interpolation relative to the current query execution speed (this behaviour is controlled by ). ClickHouse will interrupt a query if the projected execution time exceeds the specified `maxexecutiontime`. By default, the timeoutbeforecheckingexecutionspeed is set to 10 seconds. This means that after 10 seconds of query execution, ClickHouse will begin estimating the total execution

If, for example, `maxexecutiontime` is set to 3600 seconds (1 hour), ClickHouse will terminate the query if the estimated time exceeds this 3600-second limit. If you set `timeoutbeforecheckingexecutionspeed `to 0, ClickHouse will use clock time as the basis for `maxexecutiontime`. What to do if the query is run longer than `maxexecutiontime` or the estimated running time is longer than `maxestimatedexecution_time`: `throw` or `break`. By default, `throw`. Similar semantic to `maxexecutiontime` but only apply on leaf node for distributed or remote queries. For example, if we want to limit execution time on leaf node to `10s` but no limit on the initial node, instead of having `maxexecutiontime` in the nested subquery settings: ``` sql SELECT count() FROM cluster(cluster, view(SELECT * FROM t SETTINGS maxexecutiontime = 10)); ``` We can use `maxexecutiontime_leaf` as the query settings: ``` sql SELECT count() FROM cluster(cluster, view(SELECT * FROM t)) SETTINGS maxexecutiontime_leaf = 10; ``` What to do when the query in leaf node run longer than `maxexecutiontime_leaf`: `throw` or `break`. By default, `throw`. Minimal execution speed in rows per second. Checked on every data block when timeoutbeforecheckingexecutionspeed expires. If the execution speed is lower, an exception is thrown. A minimum number of execution bytes per second. Checked on every data block when timeoutbeforecheckingexecutionspeed expires. If the execution speed is lower, an exception is thrown. A maximum number of execution rows per second. Checked on every data block when timeoutbeforecheckingexecutionspeed expires. If the execution speed is high, the execution speed will be reduced. A maximum number of execution bytes per second. Checked on every data block when timeoutbeforecheckingexecutionspeed expires. If the execution speed is high, the execution speed will be reduced. Checks that execution speed is not too slow (no less than minexecutionspeed), after the specified time in seconds has expired. Maximum query estimate execution time in seconds. Checked on every data block when timeoutbeforecheckingexecutionspeed expires. A maximum number of columns that can be read from a table in a single query. If a query requires reading a greater number of columns, it throws an exception. A maximum number of temporary columns that must be kept in RAM at the same time when running a query, including constant columns. If there are more temporary columns than this, it throws an exception. The same thing as maxtemporarycolumns, but without counting constant columns. Note that constant columns are formed fairly often when running a query, but they require approximately zero computing resources. Maximum nesting depth of subqueries. If subqueries are deeper, an exception is thrown. By default, 100. Maximum pipeline depth. Corresponds to the number of transformations that each data block goes through during query processing. Counted within the limits of a single server. If the pipeline depth is greater, an exception is thrown. By default, 1000. Maximum nesting depth of a query syntactic tree. If exceeded, an exception is thrown. At this time, it isnt checked during parsing, but only after parsing the query. That is, a syntactic tree that is too deep can be created during parsing, but the query will fail. By default, 1000. A maximum number of elements in a query syntactic tree. If exceeded, an exception is thrown. In the same way as the previous setting, it is checked only after parsing the query. By default, 50,000. A maximum number of rows for a data set in the IN clause created from a subquery. A maximum number of bytes (uncompressed data) used by a set in the IN clause created from a subquery. What to do when the amount of data exceeds one of the limits: throw or break. By default, throw. A maximum number of different rows when using

A maximum number of bytes used by a hash table when using DISTINCT. What to do when the amount of data exceeds one of the limits: throw or break. By default, throw. A maximum number of rows that can be passed to a remote server or saved in a temporary table when using GLOBAL IN. A maximum number of bytes (uncompressed data) that can be passed to a remote server or saved in a temporary table when using GLOBAL IN. What to do when the amount of data exceeds one of the limits: throw or break. By default, throw. Limits the number of rows in the hash table that is used when joining tables. This settings applies to operations and the table engine. If a query contains multiple joins, ClickHouse checks this setting for every intermediate result. ClickHouse can proceed with different actions when the limit is reached. Use the setting to choose the action. Possible values: Positive integer. 0 Unlimited number of rows. Default value: 0. Limits the size in bytes of the hash table used when joining tables. This setting applies to operations and . If the query contains joins, ClickHouse checks this setting for every intermediate result. ClickHouse can proceed with different actions when the limit is reached. Use settings to choose the action. Possible values: Positive integer. 0 Memory control is disabled. Default value: 0. Defines what action ClickHouse performs when any of the following join limits is reached: Possible values: `THROW` ClickHouse throws an exception and breaks operation. `BREAK` ClickHouse breaks operation and does not throw an exception. Default value: `THROW`. See Also Limits the maximum number of partitions in a single inserted block. Positive integer. 0 Unlimited number of partitions. Default value: 100. Details When inserting data, ClickHouse calculates the number of partitions in the inserted block. If the number of partitions is more than `maxpartitionsperinsertblock`, ClickHouse either logs a warning or throws an exception based on `throwonmaxpartitionsperinsertblock`. Exceptions have the following text: Too many partitions for a single INSERT block (`partitionscount` partitions, limit is + toString(maxpartitions) + ). The limit is controlled by the maxpartitionsperinsertblock setting. A large number of partitions is a common misconception. It will lead to severe negative performance impact, including slow server startup, slow INSERT queries and slow SELECT queries. Recommended total number of partitions for a table is under 1000..10000. Please note, that partitioning is not intended to speed up SELECT queries (ORDER BY key is sufficient to make range queries fast). Partitions are intended for data manipulation (DROP PARTITION, etc). Allows you to control behaviour when `maxpartitionsperinsertblock` is reached. `true` - When an insert block reaches `maxpartitionsperinsertblock`, an exception is raised. `false` - Logs a warning when `maxpartitionsperinsertblock` is reached. Default value: `true` The maximum amount of data consumed by temporary files on disk in bytes for all concurrently running user queries. Zero means unlimited. Default value: 0. The maximum amount of data consumed by temporary files on disk in bytes for all concurrently running queries. Zero means unlimited. Default value: 0. Maximum number of simultaneous sessions per authenticated user to the ClickHouse server. Example: ``` xml <profiles> <singlesessionprofile> <maxsessionsforuser>1</maxsessionsforuser> </singlesessionprofile> <twosessionsprofile> <maxsessionsforuser>2</maxsessionsforuser> </twosessionsprofile> <unlimitedsessionsprofile> <maxsessionsforuser>0</maxsessionsforuser> </unlimitedsessionsprofile> </profiles> <users> <!-- User Alice can connect to a ClickHouse server no more than once at a time. --> <Alice> <profile>singlesessionuser</profile> </Alice> <!-- User Bob can use 2 simultaneous sessions. --> <Bob> <profile>twosessionsprofile</profile> </Bob> <!-- User Charles can use arbitrarily many of simultaneous sessions. --> <Charles> <profile>unlimitedsessionsprofile</profile> </Charles> </users> ``` Default value: 0 (Infinite count of simultaneous sessions).