TRACE

The TRACE statement provides detailed information about query execution. It is intended to be viewed through a Graphical interface exposed by the TiDB server’s status port.

Synopsis

TraceStmt

TRACE - 图1

TracableStmt

TRACE - 图2

  1. TraceStmt ::=
  2. "TRACE" ( "FORMAT" "=" stringLit )? TracableStmt
  3. TracableStmt ::=
  4. ( SelectStmt | DeleteFromStmt | UpdateStmt | InsertIntoStmt | ReplaceIntoStmt | UnionStmt | LoadDataStmt | BeginTransactionStmt | CommitStmt | RollbackStmt | SetStmt )
FormatDescription
rowOutput in a tree format
jsonStructured output in JSON format
logLog based output

Examples

Row

  1. TRACE FORMAT='row' SELECT * FROM mysql.user;
  1. +--------------------------------------------+-----------------+------------+
  2. | operation | startTS | duration |
  3. +--------------------------------------------+-----------------+------------+
  4. | trace | 17:03:31.938237 | 886.086µs |
  5. | ├─session.Execute | 17:03:31.938247 | 507.812µs |
  6. | ├─session.ParseSQL | 17:03:31.938254 | 22.504µs |
  7. | ├─executor.Compile | 17:03:31.938321 | 278.931µs |
  8. | └─session.getTxnFuture | 17:03:31.938337 | 1.515µs |
  9. | └─session.runStmt | 17:03:31.938613 | 109.578µs |
  10. | ├─TableReaderExecutor.Open | 17:03:31.938645 | 50.657µs |
  11. | └─distsql.Select | 17:03:31.938666 | 21.066µs |
  12. | └─RPCClient.SendRequest | 17:03:31.938799 | 158.411µs |
  13. | └─session.CommitTxn | 17:03:31.938705 | 12.06µs |
  14. | └─session.doCommitWitRetry | 17:03:31.938709 | 2.437µs |
  15. | ├─*executor.TableReaderExecutor.Next | 17:03:31.938781 | 224.327µs |
  16. | └─*executor.TableReaderExecutor.Next | 17:03:31.939019 | 6.266µs |
  17. +--------------------------------------------+-----------------+------------+
  18. 13 rows in set (0.00 sec)

JSON

  1. TRACE FORMAT='json' SELECT * FROM mysql.user;

The JSON formatted trace can be pasted into the trace viewer, which is accessed via the TiDB status port:

TiDB Trace Viewer-1

TiDB Trace Viewer-2

Log

  1. TRACE FORMAT='log' SELECT * FROM mysql.user;
  1. +----------------------------+--------------------------------------------------------+------+------------------------------------+
  2. | time | event | tags | spanName |
  3. +----------------------------+--------------------------------------------------------+------+------------------------------------+
  4. | 2024-04-08 08:41:47.358734 | --- start span trace ---- | | trace |
  5. | 2024-04-08 08:41:47.358737 | --- start span session.ExecuteStmt ---- | | session.ExecuteStmt |
  6. | 2024-04-08 08:41:47.358746 | --- start span executor.Compile ---- | | executor.Compile |
  7. | 2024-04-08 08:41:47.358984 | --- start span session.runStmt ---- | | session.runStmt |
  8. | 2024-04-08 08:41:47.359035 | --- start span TableReaderExecutor.Open ---- | | TableReaderExecutor.Open |
  9. | 2024-04-08 08:41:47.359047 | --- start span distsql.Select ---- | | distsql.Select |
  10. | 2024-04-08 08:41:47.359073 | --- start span *executor.TableReaderExecutor.Next ---- | | *executor.TableReaderExecutor.Next |
  11. | 2024-04-08 08:41:47.359077 | table scan table: user, range: [[-inf,+inf]] | | *executor.TableReaderExecutor.Next |
  12. | 2024-04-08 08:41:47.359094 | --- start span regionRequest.SendReqCtx ---- | | regionRequest.SendReqCtx |
  13. | 2024-04-08 08:41:47.359098 | send Cop request to region 16 at store1 | | regionRequest.SendReqCtx |
  14. | 2024-04-08 08:41:47.359237 | --- start span *executor.TableReaderExecutor.Next ---- | | *executor.TableReaderExecutor.Next |
  15. | 2024-04-08 08:41:47.359240 | table scan table: user, range: [[-inf,+inf]] | | *executor.TableReaderExecutor.Next |
  16. | 2024-04-08 08:41:47.359242 | execute done, ReturnRow: 1, ModifyRow: 0 | | trace |
  17. | 2024-04-08 08:41:47.359252 | execute done, modify row: 0 | | trace |
  18. +----------------------------+--------------------------------------------------------+------+------------------------------------+
  19. 14 rows in set (0.0008 sec)

MySQL compatibility

This statement is a TiDB extension to MySQL syntax.

See also