PolarDB-X provides the SQL throttling feature. You can use this feature to specify limits for the execution of SQL statements that cause issues on database nodes. The issues include request spikes, consumption of a large number of resources, and modifications in the SQL access model. This feature ensures that your PolarDB-X instance runs in a continuous and stable manner. This topic describes how to use the SQL throttling feature.
Supported versions
The SQL throttling feature is supported only for PolarDB-X Enterprise Edition instances whose compute node version is 5.4.9-16167266 or later. Slow SQL throttling trigger is supported only for PolarDB-X Enterprise Edition instances whose compute node version is 5.4.11-16251897 or later.
For information about the instance version naming rules, see Release notes.
For information about how to view the version of an instance, see View and update the version of an instance.
Create a throttling rule
Syntax
CREATE CCL_RULE [ IF NOT EXISTS ] `ccl_rule_name`
ON `database`.`table`
TO '<username>'@'<host>'
FOR { UPDATE | SELECT | INSERT | DELETE }
[ filter_options ]
with_options
filter_options:
[ FILTER BY KEYWORD('KEYWORD1', 'KEYWORD2',…) ]Parameters:
Parameter | Required | Description | |
Parameters used to identify the SQL statements to be limited for throttling |
| Yes | The name of the throttling rule. Note We recommend that you use backticks (`) to enclose the rule name to distinguish the rule name from SQL keywords. |
| Yes | The name of the database and the name of the table. Asterisks (*) are supported as wildcard characters. Note We recommend that you use backticks (`) to enclose each database name and table name to distinguish the name from SQL keywords. | |
| Yes | The username of the account. You can use a percent sign (%) as a wildcard character in the | |
| Yes | The SQL query type. The value can be UPDATE, SELECT, INSERT, or DELETE. Note Each throttling rule applies only to one SQL query type. | |
| No | The filter conditions. Valid values:
| |
Parameters used to determine throttling actions |
| Yes | The WITH clause supports the following parameters to determine throttling actions:
Note
|
The parameters that are included in the WITH clause take effect only on SQL statements that meet all conditions that are included in the parameters that are used to identify the SQL statements to be limited for throttling.
Examples
Create a throttling rule named selectrule to enforce a maximum concurrency of 10 for SELECT queries that are executed by the 'ccltest'@'%'user and contain the cclmatched keyword.
CREATE CCL_RULE IF NOT EXISTS `selectrule` ON *.* TO 'ccltest'@'%'
FOR SELECT
FILTER BY KEYWORD('cclmatched')
WITH MAX_CONCURRENCY=10;SELECT queries that are executed by the ccltest account from any IP and contain the cclmatched keyword have a maximum concurrency of 10.
Throttling results
After the system detects an SQL statement that matches the throttling rule, the SQL statement enters the following statements based on the WITH options specified in the throttling rule:
RUN
If the concurrency of SQL statements that match the rule does not reach the value specified by MAX_CONCURRENCY, the SQL statement is executed as expected.
WAIT
If the concurrency reaches the maximum concurrency but the number of queued SQL statements has not reached the value specified by WAIT_QUEUE_SIZE, the SQL statement enters the WAIT state and then enters the RUN or WAIT_TIMEOUT state.
You can execute the following statement to query the queued SQL statements that match the throttling rule:
SHOW FULL PROCESSLIST;Sample result:
+----+---------------+-----------------+----------+-------------------------------+------+-------+-----------------------+-----------------+ | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | SQL_TEMPLATE_ID | +----+---------------+-----------------+----------+-------------------------------+------+-------+-----------------------+-----------------+ | 2 | polardbx_root | ***.*.*.*:62787 | polardbx | Query | 0 | | show full processlist | NULL | | 1 | polardbx_root | ***.*.*.*:62775 | polardbx | Query(Waiting-selectrulereal) | 12 | | select 1 | 9037e5e2 | +----+---------------+-----------------+----------+-------------------------------+------+-------+-----------------------+-----------------+ 2 rows in set (0.08 sec)NoteThe
select 1SQL statement matches theselectrulerealthrottling rule and is waiting to be processed.WAIT_TIMEOUT
A queued SQL statement fails to be executed and returns an error if its wait time exceeds the duration specified by
WAIT_TIMEOUT.For example, if you set the WAIT_TIMEOUT option to 10s for a throttling rule and the
SELECT sleep(11)statement that matches the throttling rule stays in the queue for 10s, the following error is returned:ERROR 3009 (HY000): [11a07e23fd800000][30.225.XXX.XX:8527][polardbx]Exceeding the max concurrency 0 of ccl rule selectrulereal after waiting for 10060 msKILL
If the system detects an SQL statement that matches the throttling rule when the maximum concurrency and the maximum queue length are reached, the client receives an error. The error message indicates that the maximum concurrency is reached and contains the name of the throttling rule that the SQL statement matches.
For example, if you execute the
SELECT 1;statement when the maximum concurrency and the maximum queue length has reached and the statement matches the throttling rule, the following error is returned:ERROR 3009 (HY000): [11a07c4425c00000][**.***.***.**:8527][polardbx]Exceeding the max concurrency 0 of ccl rule selectrulerealNoteSELECT 1;fails to be executed beacause the maximum concurrency specified byselectrulerealis exceeded.
Query a throttling rule
Query a specific throttling rule
To view specific rules, specify their names in the following statement and separate the names with commas (,).
Syntax:
SHOW CCL_RULE `ccl_rule_name1` [, `ccl_rule_name2` ]Example:
SHOW CCL_RULE `selectrulereal` Sample result:
NO.: 1
RULE_NAME: selectrulereal
RUNNING: 2
WAITING: 29
KILLED: 0
MATCH_HIT_CACHE: 21374
TOTAL_MATCH: 21406
ACTIVE_NODE_COUNT: 2
MAX_CONCURRENCY_PER_NODE: 1
WAIT_QUEUE_SIZE_PER_NODE: 100
WAIT_TIMEOUT: 600
FAST_MATCH: 1
SQL_TYPE: SELECT
USER: ccltest@%
TABLE: *.*
KEYWORDS: ["SELECT"]
TEMPLATEID: NULL
CREATED_TIME: 2020-11-26 17:04:08Fields:
Field | Description |
NO. | The priority of the rule. A smaller value indicates a higher priority. |
RULE_NAME | The name of the throttling rule. |
RUNNING | The number of SQL statements that match the throttling rule and that are executed as expected. |
WAITING | The number of queued requests that are sent by using SQL statements that match the throttling rules. |
KILLED | The number of SQL statements that match the throttling rule and that are terminated. |
MATCH_HIT_CACHE | The number of SQL statements that match the throttling rule and whose template IDs are found in the cache keys. |
TOTAL_MATCH | The total number of times for which the throttling rule is matched. |
ACTIVE_NODE_COUNT | The number of compute nodes for which SQL throttling is enabled. |
MAX_CONCURRENCY_PER_NODE | The maximum number of concurrent SQL statements on each compute node. |
WAIT_QUEUE_SIZE_PER_NODE | The maximum length of the waiting queue on each compute node. |
WAIT_TIMEOUT | The maximum duration for which an SQL statement can be retained in the waiting queue. |
FAST_MATCH | Specifies whether to enable cache to accelerate the matching process. |
SQL_TYPE | The type of SQL statements. |
USER | The username. |
TABLE | The name of the database. |
KEYWORDS | The list of keywords. |
TEMPLATEID | The ID of the SQL template. |
CREATED_TIME | The local time when the throttling rule was created. The time is in the |
View all throttling rules
SHOW CCL_RULES;Delete a throttling rule
After a throttling rule is deleted, the throttling rule immediately becomes invalid and the queued SQL statements can be executed as expected.
Delete a specific throttling rule
To delete specific throttling rules, specify their names in the following statement and separate the names with commas (
,).Syntax:
DROP CCL_RULE [ IF EXISTS ] `ccl_rule_name1` [, `ccl_rule_name2`, ...]Example:
DROP CCL_RULE IF EXISTS `selectrulereal`;Delete all throttling rules:
CLEAR CCL_RULES;
Slow SQL throttling trigger
Enable the slow SQL throttling trigger
The slow SQL throttling trigger is a mechanism designed to automatically create throttling rules for SQL queries that exceed a specified execution time threshold.
Syntax:
SLOW_SQL_CCL GO ['SQL_TYPE'[MAX_CONCURRENCY] [SLOW_SQL_TIME] [MAX_CCL_RULE]]Example:
SLOW_SQL_CCL GO 'SELECT' 2 5 2;Parameters:
SQL_TYPE: the SQL query type. Valid values: ALL, SELECT, UPDATE, and INSERT. Default value: SELECT.
MAX_CONCURRENCY: the maximum number of concurrent SQL queries allowed for the automatically created throttling rule. By default, the value is half the number of CPU cores.
SLOW_SQL_TIME: the threshold execution time of slow SQL queries. Default value: the value of the system parameter SLOW_SQL_TIME. Unit: milliseconds.
MAX_CCL_RULE: the maximum number of throttling rules that can be created. Default value: 1000.
How the slow SQL throttling trigger works:
Scans all sessions across the instance to identify the template IDs of all slow SQL queries of the specified type.
Creates a slow SQL throttling trigger. The trigger is named in the _SYSTEM_SLOW_SQL_CCL_TRIGGER_{SQL_TYPE}_format.
Transmits the template IDs of all slow SQL queries to the trigger. The trigger then creates a throttling rule for the identified template IDs
Terminates all SQL queries with the identified template IDs that exceed the
MAX_CONCURRENCYlimit.
View the slow SQL throttling trigger
SLOW_SQL_CCL SHOW;Fields in the result:
Field | value |
NO. | 1 |
SCHEMA | __cdc__ |
TEMPLATE_ID | 1394f5db |
SQL | SELECT * FROM test |
RULE_NAME | AUTO__SYSTEM_SLOW_SQL_CCL_TRIGGER_SELECT____cdc___1394f5db_9e1c7f6d |
RUNNING | 0 |
WAITING | 0 |
KILLED | 0 |
TOTAL_MATCH | 0 |
ACTIVE_NODE_COUNT | 4 |
MAX_CONCURRENCY_PER_NODE | 2 |
CREATED_TIME | 2025-04-23 00:00:00 |
Adjust the slow SQL threshold
You can adjust the slow SQL threshold by using one of the following methods, listed in descending order of priority:
Specify SLOW_SQL_TIME in the
SLOW_SQL_CCL GOstatement.Specify SLOW_SQL_TIME before executing SLOW_SQL_CCL GO.
Example:
SET @slow_sql_time=2000; SLOW_SQL_CCL GO;Specify SLOW_SQL_TIME in the PolarDB-X console.
Disable the slow SQL throttling trigger
When you disable the slow SQL throttling trigger created by using the SLOW_SQL_CCL GO statement, the throttling rules created by the trigger are also deleted.
SLOW_SQL_CCL BACK;