SQL Trace (SHOW TRACE) is a performance analysis tool provided by OceanBase Database. It interactively displays the trace of the previously executed SQL statement, including the call chain and the time consumed at each stage, to facilitate performance analysis and tuning.
Purpose of SQL Trace
Performance tuning
- Complex query optimization: When SQL execution is slow, tracing can quickly identify issues (such as full table scans or improper
JOINorder). - Resource contention analysis: Identify latency-causing problems such as lock waits and concurrency conflicts.
- Index effectiveness validation: Determine whether a query hits an index and whether redundant indexes exist.
Problem diagnosis
- Slow query location: Use
ELAPSE_TIMEto find the subtask with the longest duration. - Missing index: Abnormally high
inner_execute_readtime may indicate that an index is not being used. - Distributed task exception: Check whether the time spent on
do_local_das_taskis balanced, to avoid single-point performance bottlenecks.
System stability assurance
- Preventive maintenance: Regularly trace key SQL statements to proactively identify potential performance risks.
- Fault review: When an exception occurs in the production environment, use trace logs to quickly reproduce the issue.
Use SQL Trace
The SHOW TRACE feature is disabled by default and can be controlled by the ob_enable_show_trace session variable.
Procedure
Step 1: Enable SQL Trace for the current session
Example:
SET ob_enable_show_trace = 1;
Step 2: Execute the target SQL statement
Example:
Create table
t1.CREATE TABLE t1(c1 INT, c2 INT, c3 INT);Insert data into the table.
INSERT INTO t1 VALUES(1,1,1),(2,2,2);Query the data in table
t1.SELECT/*+PARALLEL(2)*/ COUNT(*) FROM t1;The result is as follows:
+----------+ | COUNT(*) | +----------+ | 2 | +----------+ 1 row in set
Step 3: View the trace information
Use the SHOW TRACE; statement to display the trace information of the last SQL statement.
Example:
SHOW TRACE;
The result is as follows:
+-------------------------------------------------------+---------------------+-------------+
| OPERATION | START_TIME | ELAPSE_TIME |
+-------------------------------------------------------+---------------------+-------------+
| com_query_process | 2023/03/07 16:27:43 | 5241 µs |
| └── mpquery_single_stmt | 2023/03/07 16:27:43 | 5208 µs |
| ├── sql_compile | 2023/03/07 16:27:43 | 4149 µs |
| │ ├── pc_get_plan | 2023/03/07 16:27:43 | 108 µs |
| │ └── hard_parse | 2023/03/07 16:27:43 | 3952 µs |
| │ ├── parse | 2023/03/07 16:27:43 | 97 µs |
| │ ├── resolve | 2023/03/07 16:27:43 | 492 µs |
| │ ├── rewrite | 2023/03/07 16:27:43 | 598 µs |
| │ ├── optimize | 2023/03/07 16:27:43 | 2022 µs |
| │ │ ├── inner_execute_read | 2023/03/07 16:27:43 | 485 µs |
| │ │ │ ├── sql_compile | 2023/03/07 16:27:43 | 140 µs |
| │ │ │ │ └── pc_get_plan | 2023/03/07 16:27:43 | 81 µs |
| │ │ │ ├── open | 2023/03/07 16:27:43 | 43 µs |
| │ │ │ ├── get_das_id | 2023/03/07 16:27:43 | 17 µs |
| │ │ │ └── do_local_das_task | 2023/03/07 16:27:43 | 107 µs |
| │ │ └── close | 2023/03/07 16:27:43 | 66 µs |
| │ │ ├── close_das_task | 2023/03/07 16:27:43 | 17 µs |
| │ │ └── end_transaction | 2023/03/07 16:27:43 | 6 µs |
| │ ├── code_generate | 2023/03/07 16:27:43 | 197 µs |
| │ └── pc_add_plan | 2023/03/07 16:27:43 | 74 µs |
| └── sql_execute | 2023/03/07 16:27:43 | 979 µs |
| ├── open | 2023/03/07 16:27:43 | 19 µs |
| ├── response_result | 2023/03/07 16:27:43 | 768 µs |
| │ ├── get_das_id | 2023/03/07 16:27:43 | 0 µs |
| │ └── do_local_das_task | 2023/03/07 16:27:43 | 358 µs |
| └── close | 2023/03/07 16:27:43 | 145 µs |
| └── close_das_task | 2023/03/07 16:27:43 | 19 µs |
+-------------------------------------------------------+---------------------+-------------+
27 rows in set
Key columns:
Column |
Description |
|---|---|
| OPERATION | The phase of the SQL execution, such as parse, optimize, or do_local_das_task. |
| START_TIME | The start time of the phase, which can be used to analyze the timeline. |
| ELAPSE_TIME | The time consumed in the phase, in microseconds (µs). This column is used to locate performance bottlenecks. |
References
- For more information about SQL tuning, see Overview of SQL tuning.
- For more information about enabling or disabling SQL Trace, see ob_enable_show_trace.