2020-07-09 15:10:35 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/statements/create/quota
2022-04-09 13:29:05 +00:00
sidebar_position: 42
sidebar_label: QUOTA
2022-08-29 16:19:50 +00:00
title: "CREATE QUOTA"
2020-07-09 15:10:35 +00:00
---
2023-03-18 02:45:43 +00:00
Creates a [quota ](../../../guides/sre/user-management/index.md#quotas-management ) that can be assigned to a user or a role.
2020-07-09 15:10:35 +00:00
Syntax:
``` sql
CREATE QUOTA [IF NOT EXISTS | OR REPLACE] name [ON CLUSTER cluster_name]
2023-07-28 03:37:09 +00:00
[IN access_storage_type]
2021-01-28 10:06:44 +00:00
[KEYED BY {user_name | ip_address | client_key | client_key,user_name | client_key,ip_address} | NOT KEYED]
[FOR [RANDOMIZED] INTERVAL number {second | minute | hour | day | week | month | quarter | year}
2021-01-26 08:11:46 +00:00
{MAX { {queries | query_selects | query_inserts | errors | result_rows | result_bytes | read_rows | read_bytes | execution_time} = number } [,...] |
2020-07-09 15:10:35 +00:00
NO LIMITS | TRACKING ONLY} [,...]]
[TO {role [,...] | ALL | ALL EXCEPT role [,...]}]
```
2021-03-10 18:46:14 +00:00
Keys `user_name` , `ip_address` , `client_key` , `client_key, user_name` and `client_key, ip_address` correspond to the fields in the [system.quotas ](../../../operations/system-tables/quotas.md ) table.
2021-01-28 10:06:44 +00:00
2021-01-26 08:11:46 +00:00
Parameters `queries` , `query_selects` , `query_inserts` , `errors` , `result_rows` , `result_bytes` , `read_rows` , `read_bytes` , `execution_time` correspond to the fields in the [system.quotas_usage ](../../../operations/system-tables/quotas_usage.md ) table.
2021-01-28 10:06:44 +00:00
2020-07-09 15:10:35 +00:00
`ON CLUSTER` clause allows creating quotas on a cluster, see [Distributed DDL ](../../../sql-reference/distributed-ddl.md ).
2021-01-26 21:25:34 +00:00
2021-07-29 15:20:55 +00:00
**Examples**
2021-01-23 04:38:49 +00:00
Limit the maximum number of queries for the current user with 123 queries in 15 months constraint:
``` sql
2021-01-28 10:06:44 +00:00
CREATE QUOTA qA FOR INTERVAL 15 month MAX queries = 123 TO CURRENT_USER;
2021-01-23 04:38:49 +00:00
```
2023-06-02 11:30:05 +00:00
For the default user limit the maximum execution time with half a second in 30 minutes, and limit the maximum number of queries with 321 and the maximum number of errors with 10 in 5 quarters:
2021-01-23 04:38:49 +00:00
``` sql
2021-01-28 10:06:44 +00:00
CREATE QUOTA qB FOR INTERVAL 30 minute MAX execution_time = 0.5, FOR INTERVAL 5 quarter MAX queries = 321, errors = 10 TO default;
2021-01-23 04:38:49 +00:00
```