You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: identify-slow-queries.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,6 +188,30 @@ Usage example:
188
188
SELECT/*+ WRITE_SLOW_LOG */count(*) FROM t t1, t t2 WHEREt1.a=t2.b;
189
189
```
190
190
191
+
## `tidb_slow_log_rules` Usage
192
+
[`tidb_slow_log_rules`](/system-variables.md#tidb_slow_log_rules-introduced-in-v900) is used to define trigger rules for slow query logs, supporting multi-dimensional metric combinations. It is suitable for "targeted sampling" and "problem reproduction" of slow logs, allowing you to filter target statements based on specific metric combinations.
193
+
The triggering behavior of slow query logs depends on the configuration of `tidb_slow_log_rules`:
194
+
- If `tidb_slow_log_rules` is not set, slow query log triggering still relies on [`tidb_slow_log_threshold`](/system-variables.md#tidb_slow_log_threshold) (in milliseconds).
195
+
- If `tidb_slow_log_rules` is set, the configured rules take precedence, and [`tidb_slow_log_threshold`](/system-variables.md#tidb_slow_log_threshold) will be ignored.
196
+
197
+
- Rule Capacity and Separation: `SESSION` and `GLOBAL` each support a maximum of 10 rules. A single session can have up to 20 active rules. Rules are separated by `;`.
198
+
- Field and Scope: Field names are case-insensitive (underscores and other characters should be preserved). `Conn_ID` is not supported for `SESSION` rules; only `GLOBAL` supports `Conn_ID`.
199
+
- String and boolean fields are matched using equality (`=`).
200
+
-`DB` and `Resource_group` matching is case-insensitive.
201
+
- Numeric types (`int64`, `uint64`, `float64`) uniformly require `>= 0`. Negative values will result in a parsing error.
202
+
-`int64`: Maximum value is `2^63-1`.
203
+
-`uint64`: Maximum value is `2^64-1`.
204
+
-`float64`: The general upper limit is approximately `1.79e308`. Currently, parsing is done using Go's `ParseFloat`. While `NaN`/`Inf` can be parsed, they may lead to rules that are always true or always false, so their use is not recommended.
205
+
-`bool`: Supports `true`/`false`, `1`/`0`, and `t`/`f` (case-insensitive).
206
+
-`string`: Currently does not support strings containing the separators `,` (condition separator) or `;` (rule separator), even with quotes (single or double). Escaping is not supported.
207
+
208
+
- Standard format (SESSION scope):
209
+
SET SESSION tidb_slow_log_rules = 'Query_time: 0.5, Is_internal: false';
210
+
- Incorrect format (SESSION scope does not support `Conn_ID`):
211
+
SET SESSION tidb_slow_log_rules = 'Conn_ID: 12, Query_time: 0.5, Is_internal: false';
212
+
SET GLOBAL tidb_slow_log_rules = 'Query_time: 0.5, Is_internal: false';
213
+
SET GLOBAL tidb_slow_log_rules = 'Conn_ID: 11, Query_time: 0.5, Is_internal: false; Conn_ID: 12, Query_time: 0.6, Process_time: 0.3, DB: db1';
214
+
191
215
## Related system variables
192
216
193
217
*[`tidb_slow_log_threshold`](/system-variables.md#tidb_slow_log_threshold): sets the threshold for the slow query log. The SQL statement whose execution time exceeds this threshold is recorded in the slow query log. The default value is 300 ms.
- This variable defines the triggering rules for slow query logs. It supports combining multi-dimensional metrics to provide more flexible and fine-grained logging.
5892
+
- For detailed usage of this system variable, please refer to [`tidb_slow_log_rules` Usage Guide](/identify-slow-queries.md#tidb_slow_log_rules-usage-guide).
5892
5893
5893
5894
> **Tip:**
5894
5895
>
5895
-
> After enabling `tidb_slow_log_rules`, it is recommended to also configure [`tidb_slow_log_max_per_sec`](#tidb_slow_log_max_per_sec-new-in-v900) to limit the slow query log output rate and prevent rule-based slow query logging from being triggered too frequently.
5896
+
> - When enabling `tidb_slow_log_rules` in a production environment, it is recommended to also configure [`tidb_slow_log_max_per_sec`](#tidb_slow_log_max_per_sec-new-in-v900) to avoid excessively frequent slow query log printing.
5897
+
> - It is recommended to start with stricter conditions and gradually relax them based on troubleshooting needs. For more information on performance impact, please refer to [Usage Recommendations](/identify-slow-queries.md#usage-recommendations).
0 commit comments