跳到主要内容
跳到主要内容

CREATE QUOTA

创建一个可以分配给用户或角色的 配额

语法:

CREATE QUOTA [IF NOT EXISTS | OR REPLACE] name [ON CLUSTER cluster_name]
    [IN access_storage_type]
    [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}
        {MAX { {queries | query_selects | query_inserts | errors | result_rows | result_bytes | read_rows | read_bytes | written_bytes | execution_time | failed_sequential_authentications} = number } [,...] |
         NO LIMITS | TRACKING ONLY} [,...]]
    [TO {role [,...] | ALL | ALL EXCEPT role [,...]}]

user_nameip_addressclient_keyclient_key, user_nameclient_key, ip_address 对应于 system.quotas 表中的字段。

参数 queriesquery_selectsquery_insertserrorsresult_rowsresult_bytesread_rowsread_byteswritten_bytesexecution_timefailed_sequential_authentications 对应于 system.quotas_usage 表中的字段。

ON CLUSTER 子句允许在集群上创建配额,见 分布式 DDL

示例

限制当前用户在 15 个月内最多可执行 123 个查询:

CREATE QUOTA qA FOR INTERVAL 15 month MAX queries = 123 TO CURRENT_USER;

对于默认用户,限制 30 分钟内最大执行时间为半秒,限制最多可执行 321 个查询和最多 10 个错误,限于 5 个季度:

CREATE QUOTA qB FOR INTERVAL 30 minute MAX execution_time = 0.5, FOR INTERVAL 5 quarter MAX queries = 321, errors = 10 TO default;

更多示例,使用 XML 配置(不支持 ClickHouse Cloud),可以在 配额指南 中找到。