TRACE executes an SQL statement across distributed nodes and records per-node timing, row counts, and connection costs. Run SHOW TRACE after TRACE <SQL> to retrieve the results.
UnlikeEXPLAIN, which analyzes the execution plan without running the statement,TRACEexecutes the SQL and captures real runtime metrics.
Syntax
TRACE <SQL>;
SHOW TRACE;Trace a query
The following example traces SELECT 1.
Run the TRACE statement:
mysql> TRACE SELECT 1; +---+ | 1 | +---+ | 1 | +---+ 1 row in set (0.03 sec)The query executes and returns results normally.
Retrieve the trace data:
mysql> SHOW TRACE; +------+----------+----------------+-----------------------------------+---------------+--------------------------+------+-----------+--------+ | ID | TYPE | GROUP_NAME | DBKEY_NAME | TIME_COST(MS) | CONNECTION_TIME_COST(MS) | ROWS | STATEMENT | PARAMS | +------+----------+----------------+-----------------------------------+---------------+--------------------------+------+-----------+--------+ | 0 | Optimize | DRDS | DRDS | 3 | 0.00 | 0 | select 1 | NULL | | 1 | Query | TDDL5_00_GROUP | db218249098_sqa_zmf_tddl5_00_3309 | 7 | 0.15 | 1 | select 1 | NULL | +------+----------+----------------+-----------------------------------+---------------+--------------------------+------+-----------+--------+ 2 rows in set (0.01 sec)
SHOW TRACE output fields
| Field | Description |
|---|---|
ID | Sequential identifier for each trace entry. |
TYPE | Phase of processing: Optimize (query planning by the DRDS layer) or Query (physical execution on a data node). |
GROUP_NAME | Name of the shard group where this phase ran. DRDS indicates the distributed middleware layer; other values identify physical shard groups. |
DBKEY_NAME | Identifier of the specific database instance or node within the group. |
TIME_COST(MS) | Total time spent in this phase, in milliseconds. |
CONNECTION_TIME_COST(MS) | Time spent establishing the connection to the node, in milliseconds. |
ROWS | Number of rows processed or returned in this phase. |
STATEMENT | The SQL statement sent to this node. |
PARAMS | Bound parameter values for the statement, if any. NULL when no parameters are bound. |
What's next
To analyze a query's execution plan without running it, use EXPLAIN.