Configure Trigger Rules for Slow Queries
In the TiDB Cloud console, you can view slow queries on the Slow Query tab of the Diagnosis page.
By default, SQL queries that take more than 300 milliseconds are considered as slow queries. To configure the trigger rules for slow queries, you can modify the tidb_slow_log_rules system variable.
tidb_slow_log_rules supports multi-dimensional metric combinations. It is suitable for "targeted sampling" and "problem reproduction" of slow queries, enabling you to filter target statements based on specific metric combinations.
Examples
Standard format (
SESSIONscope):SET SESSION tidb_slow_log_rules = 'Query_time: 0.5, Is_internal: false';Invalid
SESSIONrule (SESSIONscope does not supportConn_ID):SET SESSION tidb_slow_log_rules = 'Conn_ID: 12, Query_time: 0.5, Is_internal: false';
Unified rule syntax and type constraints
- Rule capacity and separation: each supported scope can contain a maximum of 10 rules. Rules are separated by
;. - Condition format: each condition uses the format
field_name:value. Multiple conditions within a single rule are separated by,. - Field names are case-insensitive. Underscores and other characters in field names are preserved.
TiDB Cloud Essential and TiDB Cloud Premium support only SESSION rules for tidb_slow_log_rules. Therefore, Conn_ID, which is available only in GLOBAL rules, is not supported.
- Matching semantics:
- Numeric fields except
Conn_IDare matched using>=.Conn_ID, string fields, and boolean fields are matched using equality (=). - Matching for
DBandResource_groupis case-insensitive. - Explicit operators such as
>,<, and!=are not supported.
- Numeric fields except
Type constraints are as follows:
- Numeric types (
int64,uint64,float64) require values greater than or equal to0. Negative values result in a parsing error.int64: the maximum value is2^63-1.uint64: the maximum value is2^64-1.float64: values must be finite and non-negative. The maximum value is approximately1.79e308.NaNand infinite values such asInfand-Infare invalid and result in an error.
bool: supportstrue/false,1/0, andt/f(case-insensitive).string: currently does not support strings containing the separators,(condition separator) or;(rule separator), even with quotes (single or double). Escaping is not supported.- Duplicate fields: if the same field is specified multiple times in a single rule, the last occurrence takes effect.
Supported fields
The fields in the following table follow the general matching and type rules described in Unified rule syntax and type constraints, unless otherwise noted.
Effective behavior and matching order
- Setting
tidb_slow_log_rulesoverwrites the existing rules in the specified scope instead of appending new rules. - Setting
tidb_slow_log_rulesto an empty string clears the rules in the specified scope. - Multiple rules are combined with
OR, while multiple field conditions within a single rule are combined withAND. - If you still want to use SQL execution time as a condition for writing slow query logs, use
Query_timein the rule and note that the unit is seconds.
TiDB Cloud Essential and TiDB Cloud Premium support only SESSION rules for tidb_slow_log_rules.
- If the current session has any
SESSIONrules, slow query log output is determined by the rule matching results. SHOW VARIABLES LIKE 'tidb_slow_log_rules'andSELECT @@SESSION.tidb_slow_log_rulesreturn theSESSIONrule text, or an empty string if unset.
Recommendations
tidb_slow_log_rulesis designed to replace the single-threshold approach. It supports combinations of multi-dimensional metric conditions, enabling more flexible and fine-grained control over slow query logging.In a well-provisioned test environment with 1 TiDB node (16 CPU cores, 48 GiB memory) and 3 TiKV nodes (each with 16 CPU cores and 48 GiB memory), repeated sysbench tests show that performance impact remains small when multi-dimensional slow query log rules generate millions of slow log entries within 30 minutes. However, when the log volume reaches tens of millions, TPS drops significantly and latency increases noticeably. Therefore, if business workload is high or CPU and memory resources are close to their limits, configure
tidb_slow_log_rulescarefully to avoid log flooding caused by overly broad rules.