Database Autonomy Service (DAS) provides the SQL throttling feature to control the number of database access requests and the number of concurrent SQL requests. This feature helps ensure the availability of your database service. This topic describes how to use the SQL throttling feature.
Prerequisites
The database instance for which you want to enable SQL throttling runs on one of the following database engines:
ApsaraDB RDS for MySQL 5.6, 5.7, and 8.0
ApsaraDB RDS for PostgreSQL 14 and later
PolarDB for MySQL 5.6, 5.7, and 8.0
PolarDB-X 2.0
Throttling messages
After an SQL throttling rule is created, if the application uses an SQL statement that contains all the specified keywords or matches the template throttling rule, the database system returns corresponding messages based on the instance version when throttling takes effect.
Maximum concurrency | Instance version | Error code | Error message |
0 or positive integer |
| 1317 |
|
Positive integer | ApsaraDB RDS for MySQL 5.7 whose revision version is 20200630 or later | 5041 |
Note When the maximum concurrency is set to a positive integer, the related SQL statements enter the |
ApsaraDB RDS for MySQL 8.0 | 7534 | ||
PolarDB for MySQL 5.7 | 3277 | ||
PolarDB for MySQL 8.0 | 7533 | ||
0 | ApsaraDB RDS for MySQL 5.7 whose revision version is 20200630 or later | 5042 |
Note When the maximum concurrency is set to 0, this error message is directly returned. |
ApsaraDB RDS for MySQL 8.0 | 7535 | ||
PolarDB for MySQL 5.7 | 3278 | ||
PolarDB for MySQL 8.0 | 7534 |
When DAS performs SQL throttling, if the
ccl_max_waiting_count
parameter is set to 0, DAS will set this parameter to the default value of 10. If the value is greater than 0, DAS will directly use the configured value.For ApsaraDB RDS for MySQL 5.7 instances whose revision version is 20200630 or later and ApsaraDB RDS for MySQL 8.0 instances, if you configure the
ccl_wait_timeout
parameter (if supported by the instance version), SQL statements in theConcurrency control waiting
state that exceed this limit will not be throttled.For PolarDB for MySQL 8.0 instances, you can configure the
ccl_mode
parameter (if supported by the instance version) to control the behavior of SQL statements when the concurrency limit is exceeded. Valid values of theccl_mode
parameter:WAIT
(default): waits in queue.REFUSE
: directly returns an error message without waiting.
Scenarios
Scenario | Description |
Sharp rise in the number of concurrent SQL statements of a specific type | Cache penetration or abnormal calls can cause an unexpected rise in the number of concurrent SQL statements. |
Existence of SQL statements that cause data skew | If hot data is frequently queried during a promotional event, the overall system slows down. |
Lack of index tables | If a large number of SQL statements are executed on tables that are not indexed, the overall system slows down. |
Throttling modes
Mode | Description | Supported database |
Throttle by Keyword | Throttles the SQL statements that contain the specified keywords. Note You cannot throttle a specific SQL statement. |
|
Throttle by SQL Template | Throttles the SQL statements that use the SQL templates extracted from the SQL samples that you enter. |
|
Throttles the SQL statements that use the SQL templates with the specified IDs. The SQL template IDs can be obtained from SQL logs, sessions, and results that are returned after EXPLAIN statements are executed. Note The ID of an SQL template is a hexadecimal string that contains eight characters in length. | PolarDB-X 2.0 Enterprise Edition | |
Throttle by Execution Duration | Throttles SQL statements by execution duration. If the execution duration of an SQL statement of the specified type exceeds the specified threshold value, the system fetches the ID of the SQL template used by the SQL statement and adds the ID to the throttling rule. Then, SQL statements that use the SQL template are executed with the specified concurrency. Note You can configure the maximum number of SQL template IDs. After the number of SQL template IDs added to the throttling rule reaches the upper limit, the system stops fetching SQL template IDs. | PolarDB-X 2.0 Enterprise Edition |
Procedure
Log on to the DAS console.
In the left-side navigation pane, choose Intelligent O&M Center > Instance Monitoring.
On the page that appears, find the database instance that you want to manage and click the instance ID. The instance details page appears.
In the left-side navigation pane, click Instance Sessions.
On the Session Management tab, click SQL Throttling.
In the panel that appears, click Create Throttling Rule.
In the Create Throttling Rule dialog box, configure the following parameters based on the database engine.
This parameter is required when Throttling Mode is set to Throttle by Keyword.
The INSERT SQL statements can be throttled on one of the following database engines:
ApsaraDB RDS for MySQL 8.0
PolarDB for MySQL 5.7 and 8.0
PolarDB-X 2.0
SQL statements that are in the
INSERT...SELECT...
format cannot be throttled.This parameter is required when Throttling Mode is set to Throttle by Keyword.
If you specify multiple keywords, throttling is triggered only if an SQL statement contains all of the specified keywords. Multiple keywords are separated by tildes (~) with no sequential restriction.
For example, if the original SQL statement to be throttled is
SELECT min(id), max(id) FROM task_event WHERE gmt_modified < '2020-06-21' AND begin_time > '2020-07-09' AND source IN (527) AND id >= 15673 AND id <= 8015673
, set this parameter toSELECT~min~id~max~id~FROM~task_event~WHERE~gmt_modified~AND~begin_time~AND~source~IN~AND~id~AND~id
.Do not set this parameter to only
SELECT, INSERT, UPDATE, or DELETE
. Keywords are case-sensitive except in some earlier versions of instances.This parameter is required when Throttling Mode is set to Throttle by SQL Template.
For complex SQL statements, DAS optimizes the SQL sample while keeping the SQL template structure unchanged. Therefore, the throttling effect is not affected after optimization.
For example, if you enter an SQL sample
select name from das where name = 'das' and age = 21 limit 20
, DAS converts it to an SQL templateselect name from das where name = ? and age = ? limit ?
, and then rewrittes the template toselect name from das where name = 'param0' and age = 'param1' limit 20
.This parameter is required when Throttling Mode is set to Throttle by SQL Template.
For complex SQL statements, DAS optimizes the SQL sample while keeping the SQL template structure unchanged. Therefore, the throttling effect is not affected after optimization.
For example, if you enter an SQL sample
select name from das where name = 'das' and age = 21 limit 20
, DAS converts it to an SQL templateselect name from das where name = ? and age = ? limit ?
, and then rewrittes the template toselect name from das where name = 'param0' and age = 'param1' limit 20
.The
INSERT
SQL statements can be throttled on one of the following database engines:ApsaraDB RDS for MySQL 8.0
PolarDB for MySQL 5.7 and 8.0
PolarDB-X 2.0
SQL statements that are in the
INSERT...SELECT...
format cannot be throttled.This parameter is required when Throttling Mode is set to Throttle by Keyword.
If you specify multiple keywords, throttling is triggered only if an SQL statement contains all of the specified keywords. Multiple keywords are separated by tildes (~) with no sequential restriction.
For example, if the original SQL statement to be throttled is
SELECT min(id), max(id) FROM task_event WHERE gmt_modified < '2020-06-21' AND begin_time > '2020-07-09' AND source IN (527) AND id >= 15673 AND id <= 8015673
, set this parameter toSELECT~min~id~max~id~FROM~task_event~WHERE~gmt_modified~AND~begin_time~AND~source~IN~AND~id~AND~id
.Do not set this parameter to only
SELECT, INSERT, UPDATE, or DELETE
. Keywords are case-sensitive except in some earlier versions of instances.This parameter is required when Throttling Mode is set to Throttle by SQL Template.
For complex SQL statements, DAS optimizes the SQL sample while keeping the SQL template structure unchanged. Therefore, the throttling effect is not affected after optimization.
For example, if you enter an SQL sample
select name from das where name = 'das' and age = 21 limit 20
, DAS converts it to an SQL templateselect name from das where name = ? and age = ? limit ?
, and then rewrittes the template toselect name from das where name = 'param0' and age = 'param1' limit 20
.The
INSERT
SQL statements can be throttled on one of the following database engines:ApsaraDB RDS for MySQL 8.0
PolarDB for MySQL 5.7 and 8.0
PolarDB-X 2.0
SQL statements that are in the
INSERT...SELECT...
format cannot be throttled.This parameter is required when Throttling Mode is set to Throttle by Keyword.
If you specify multiple keywords, throttling is triggered only if an SQL statement contains all of the specified keywords. Multiple keywords are separated by tildes (~) with no sequential restriction.
For example, if the original SQL statement to be throttled is
SELECT min(id), max(id) FROM task_event WHERE gmt_modified < '2020-06-21' AND begin_time > '2020-07-09' AND source IN (527) AND id >= 15673 AND id <= 8015673
, set this parameter toSELECT~min~id~max~id~FROM~task_event~WHERE~gmt_modified~AND~begin_time~AND~source~IN~AND~id~AND~id
.Do not set this parameter to only
SELECT, INSERT, UPDATE, or DELETE
. Keywords are case-sensitive except in some earlier versions of instances.Click Submit.
After the SQL throttling rule is created, you can view the information about the rule on the Running tab. The following table describes the parameters.
NoteThe parameters may vary slightly based on the database engine and throttling mode.
Parameter
Description
Throttling Mode
The mode that you want to use to throttle SQL statements.
Throttling Rule
Throttle by Keyword: the configured throttling keyword.
Throttle by SQL Template: the template ID corresponding to the throttled sample.
Throttle by Execution Duration: the configured execution duration threshold and the number of SQL statements throttled by template.
Maximum Concurrency
The maximum number of concurrent SQL statements.
Maximum Waiting Queries
The maximum number of waiting SQL statements configured in the throttling rule.
NoteThis parameter is displayed when the database instance is ApsaraDB RDS for PostgreSQL and Throttling Mode is set to Throttle by SQL Template.
Throttling Duration (min)
The duration during which the SQL throttling rule takes effect.
Start Time
The time when the throttling rule takes effect.
Remaining Time (s)
The remaining time before the throttling rule expires.
Sample SQL
The sample of the SQL statement that you want to throttle.
NoteThis parameter is displayed when Throttling Mode is set to Throttle by SQL Template.
Throttling Template ID
The ID of an SQL template used by the sample of the SQL statement that you want to throttle.
NoteThis parameter is displayed when Throttling Mode is set to Throttle by SQL Template.
Status
The running status of the enabled throttling rule.
Database
The database in which SQL statements are throttled.
NoteThis parameter is displayed when Throttling Mode is set to Throttle by SQL Template.
Search Path
The search path to perform SQL throttling.
NoteThis parameter is displayed when the database instance is ApsaraDB RDS for PostgreSQL and Throttling Mode is set to Throttle by SQL Template.
Matched SQL Query
The number of SQL statements matched by the throttling rule.
NoteThis parameter is displayed when Throttling Mode is set to Throttle by SQL Template.
Waiting SQL Query
The number of waiting SQL statements in the throttling rule.
NoteThis parameter is displayed when Throttling Mode is set to Throttle by SQL Template.
Actions
The operations to disable or modify the throttling rule.
ApsaraDB RDS for MySQL
Parameter | Description |
Throttling Mode | The mode that you want to use to throttle SQL statements. Select a throttling mode based on your business requirements. |
SQL Type | The type of SQL statements that you want to throttle. Valid values: SELECT, UPDATE, DELETE, and INSERT. Note |
Database | The database in which SQL statements are throttled. Note This parameter is required when Throttling Mode is set to Throttle by SQL Template. |
Maximum Concurrency | The maximum number of concurrent SQL statements. The minimum value of this parameter is 0. Note Throttling is triggered when the number of SQL statements that are concurrently executed and meet the throttling policy reaches the maximum concurrency value. |
Throttling Duration | The duration during which the SQL throttling rule takes effect. Important The throttling feature is intended only for emergency use. We recommend that you specify the throttling duration based on your business requirements and disable throttling when it is no longer needed. |
SQL Keyword | The keyword that is used to identify the SQL statements that need to be throttled. Note |
SQL Sample | The sample of the SQL statement that you want to throttle. Note |
ApsaraDB RDS for PostgreSQL
Parameter | Description |
Throttling Mode | The mode that you want to use to throttle SQL statements. Only Throttle by SQL Template is supported. |
Database | The database in which SQL statements are throttled. |
Search Path | The search path to perform SQL throttling. Note Separate multiple paths with commas ( |
Maximum Concurrency | The maximum number of concurrent SQL statements. The minimum value of this parameter is 0. Note Throttling is triggered when the number of SQL statements that are concurrently executed and meet the throttling policy reaches the maximum concurrency value. |
Maximum Waiting Queries | The maximum number of waiting SQL statements. |
Throttling Duration | The duration during which the SQL throttling rule takes effect. Important The throttling feature is intended only for emergency use. We recommend that you specify the throttling duration based on your business requirements and disable throttling when it is no longer needed. |
SQL Sample | The sample of the SQL statement that you want to throttle. Note |
PolarDB for MySQL
Parameter | Description |
Throttling Mode | The mode that you want to use to throttle SQL statements. Select a throttling mode based on your business requirements. |
SQL Type | The type of SQL statements that you want to throttle. Valid values: Note |
Maximum Concurrency | The maximum number of concurrent SQL statements. The minimum value of this parameter is 0. Note Throttling is triggered when the number of SQL statements that are concurrently executed and meet the throttling policy reaches the maximum concurrency value. |
Database | The database in which SQL statements are throttled. Note This parameter is required when Throttling Mode is set to Throttle by SQL Template. |
Throttling Duration | The duration during which the SQL throttling rule takes effect. Important The throttling feature is intended only for emergency use. We recommend that you specify the throttling duration based on your business requirements and disable throttling when it is no longer needed. |
SQL Keyword | The keyword that is used to identify the SQL statements that need to be throttled. Note |
SQL Sample | The sample of the SQL statement that you want to throttle. Note |
PolarDB-X 2.0
Parameter | Description |
Throttling Mode | The mode that you want to use to throttle SQL statements. Select a throttling mode based on your business requirements. |
SQL Type | The type of SQL statements that you want to throttle. Valid values: Note |
Maximum Concurrency | The maximum number of concurrent SQL statements. The minimum value of this parameter is 0. Note Throttling is triggered when the number of SQL statements that are concurrently executed and meet the throttling policy reaches the maximum concurrency value. |
Throttling Duration | The duration during which the SQL throttling rule takes effect. Important The throttling feature is intended only for emergency use. We recommend that you specify the throttling duration based on your business requirements and disable throttling when it is no longer needed. |
SQL Keyword | The keyword that is used to identify the SQL statements that need to be throttled. Note |
SQL Template ID | The ID of an SQL template used by an SQL statement that you want to throttle. Separate multiple IDs with commas (,). Note This parameter is required when the database engine is PolarDB-X Enterprise Edition and Throttling Mode is set to Throttle by SQL Template. |
Minimum SQL Execution Duration | The threshold for the SQL execution time. When the execution time of an SQL statement exceeds this threshold, DAS adds the ID of the SQL template to which this SQL statement belongs to the throttling rule. Note This parameter is required when the instance is a PolarDB-X Enterprise Edition instance and Throttling Mode is set to Throttle by Execution Duration. |
Maximum Number of Throttled SQL IDs | The maximum number of SQL template IDs that can be throttled by execution duration. Once this threshold is reached, DAS stops creating throttling rules for other SQL statements whose execution duration threshold is reached. Note This parameter is required when the instance is a PolarDB-X Enterprise Edition instance and Throttling Mode is set to Throttle by Execution Duration. |
Database Account with Throttled Queries | The database account to which the throttled SQL belongs. Note This parameter is required when Throttling Mode is set to Throttle by Keyword or Throttle by SQL Template. |
References
Related API operations
API operation | Description |
Enables SQL throttling. | |
Disables a throttling rule. | |
Disables all throttling rules that are in effect. | |
Queries the throttling rules that are in effect. | |
Queries the throttling rules that are in effect or have been triggered. | |
Generates throttling keywords for SQL statements. |