TRACE

TRACE 语句用于提供查询执行的详细信息,可通过 TiDB 服务器状态端口所公开的图形界面进行查看。

语法图

TraceStmt:

TraceStmt

TraceableStmt:

TraceableStmt

示例

trace format='row' select * from mysql.user;
+--------------------------------------------+-----------------+------------+
| operation                                  | startTS         | duration   |
+--------------------------------------------+-----------------+------------+
| trace                                      | 17:03:31.938237 | 886.086µs  |
|   ├─session.Execute                        | 17:03:31.938247 | 507.812µs  |
|   │ ├─session.ParseSQL                     | 17:03:31.938254 | 22.504µs   |
|   │ ├─executor.Compile                     | 17:03:31.938321 | 278.931µs  |
|   │ │ └─session.getTxnFuture               | 17:03:31.938337 | 1.515µs    |
|   │ └─session.runStmt                      | 17:03:31.938613 | 109.578µs  |
|   │   ├─TableReaderExecutor.Open           | 17:03:31.938645 | 50.657µs   |
|   │   │ └─distsql.Select                   | 17:03:31.938666 | 21.066µs   |
|   │   │   └─RPCClient.SendRequest          | 17:03:31.938799 | 158.411µs  |
|   │   └─session.CommitTxn                  | 17:03:31.938705 | 12.06µs    |
|   │     └─session.doCommitWitRetry         | 17:03:31.938709 | 2.437µs    |
|   ├─*executor.TableReaderExecutor.Next     | 17:03:31.938781 | 224.327µs  |
|   └─*executor.TableReaderExecutor.Next     | 17:03:31.939019 | 6.266µs    |
+--------------------------------------------+-----------------+------------+
13 rows in set (0.00 sec)
CREATE TABLE t1 (id int not null primary key AUTO_INCREMENT);
Query OK, 0 rows affected (0.11 sec)
INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.02 sec)
Records: 5  Duplicates: 0  Warnings: 0
TRACE FORMAT='json' SELECT * FROM t1 WHERE id = 2;
| operation                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [{"ID":{"Trace":"2765b9a0e81e6f19","Span":"46ecd59cc05b24b4","Parent":"0000000000000000"},"Annotations":[{"Key":"Name","Value":"dHJhY2U="},{"Key":"_schema:name","Value":null},{"Key":"Msg","Value":"ZXhlY3V0ZSBkb25lLCBSZXR1cm5Sb3c6IDEsIE1vZGlmeVJvdzogMA=="},{"Key":"Time","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTMxMjErMDg6MDA="},{"Key":"_schema:log","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzMDIrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTMyMjkwNTIrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":[{"ID":{"Trace":"2765b9a0e81e6f19","Span":"790bf8a6e585defa","Parent":"46ecd59cc05b24b4"},"Annotations":[{"Key":"Name","Value":"KmV4ZWN1dG9yLlRhYmxlUmVhZGVyRXhlY3V0b3IuTmV4dA=="},{"Key":"_schema:name","Value":null},{"Key":"Msg","Value":"dGFibGUgc2NhbiB0YWJsZTogdXNlciwgcmFuZ2U6IFtbLWluZiwraW5mXV0="},{"Key":"Time","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI4MjgrMDg6MDA="},{"Key":"_schema:log","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI4MjErMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTMwODI1ODErMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":null},{"ID":{"Trace":"2765b9a0e81e6f19","Span":"6861b0f966baeb2e","Parent":"46ecd59cc05b24b4"},"Annotations":[{"Key":"Name","Value":"KmV4ZWN1dG9yLlRhYmxlUmVhZGVyRXhlY3V0b3IuTmV4dA=="},{"Key":"_schema:name","Value":null},{"Key":"Time","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTMxMDcrMDg6MDA="},{"Key":"Msg","Value":"dGFibGUgc2NhbiB0YWJsZTogdXNlciwgcmFuZ2U6IFtbLWluZiwraW5mXV0="},{"Key":"_schema:log","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTMxKzA4OjAw"},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTMxMDk2MTgrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":null},{"ID":{"Trace":"2765b9a0e81e6f19","Span":"0ea5f408c9fb7aac","Parent":"46ecd59cc05b24b4"},"Annotations":[{"Key":"Name","Value":"c2Vzc2lvbi5FeGVjdXRl"},{"Key":"_schema:name","Value":null},{"Key":"Msg","Value":"ZXhlY3V0ZTogc2VsZWN0ICogZnJvbSBteXNxbC51c2Vy"},{"Key":"Time","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzMSswODowMA=="},{"Key":"_schema:log","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzMDcrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3OTY0NjkrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":[{"ID":{"Trace":"2765b9a0e81e6f19","Span":"40469c89e055ac62","Parent":"0ea5f408c9fb7aac"},"Annotations":[{"Key":"Name","Value":"ZXhlY3V0b3IuQ29tcGlsZQ=="},{"Key":"_schema:name","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzNTMrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI2NDQxOTMrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":[{"ID":{"Trace":"2765b9a0e81e6f19","Span":"6986898a1054b4a8","Parent":"40469c89e055ac62"},"Annotations":[{"Key":"Name","Value":"c2Vzc2lvbi5nZXRUeG5GdXR1cmU="},{"Key":"_schema:name","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzNzQrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzNzU5MzkrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":null}]},{"ID":{"Trace":"2765b9a0e81e6f19","Span":"1c1bae77dd0b8ee3","Parent":"0ea5f408c9fb7aac"},"Annotations":[{"Key":"Name","Value":"c2Vzc2lvbi5ydW5TdG10"},{"Key":"_schema:name","Value":null},{"Key":"Msg","Value":"eyJzcWwiOiJzZWxlY3QgKiBmcm9tIG15c3FsLnVzZXIifQ=="},{"Key":"Time","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI2NTkrMDg6MDA="},{"Key":"_schema:log","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI2NTcrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3NzQxNjErMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":[{"ID":{"Trace":"2765b9a0e81e6f19","Span":"74d18c632970a406","Parent":"1c1bae77dd0b8ee3"},"Annotations":[{"Key":"Name","Value":"VGFibGVSZWFkZXJFeGVjdXRvci5PcGVu"},{"Key":"_schema:name","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI2ODkrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3NDQ0NDMrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}] |
| ,"Sub":[{"ID":{"Trace":"2765b9a0e81e6f19","Span":"4fdb738d1891ea9f","Parent":"74d18c632970a406"},"Annotations":[{"Key":"Name","Value":"ZGlzdHNxbC5TZWxlY3Q="},{"Key":"_schema:name","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3MTMrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3MzU3MjErMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":[{"ID":{"Trace":"2765b9a0e81e6f19","Span":"29d71ba0becaa63e","Parent":"4fdb738d1891ea9f"},"Annotations":[{"Key":"Name","Value":"UlBDQ2xpZW50LlNlbmRSZXF1ZXN0"},{"Key":"_schema:name","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI4NTgrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTMwMzQwMTIrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":null}]}]},{"ID":{"Trace":"2765b9a0e81e6f19","Span":"21b485f846699ad3","Parent":"1c1bae77dd0b8ee3"},"Annotations":[{"Key":"Name","Value":"c2Vzc2lvbi5Db21taXRUeG4="},{"Key":"_schema:name","Value":null},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3NjgyNzcrMDg6MDA="},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3NTUrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":[{"ID":{"Trace":"2765b9a0e81e6f19","Span":"481bec0817b86a53","Parent":"21b485f846699ad3"},"Annotations":[{"Key":"Name","Value":"c2Vzc2lvbi5kb0NvbW1pdFdpdFJldHJ5"},{"Key":"_schema:name","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3NTkrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTI3NjE1NTErMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":null}]}]},{"ID":{"Trace":"2765b9a0e81e6f19","Span":"714676cb2046434e","Parent":"0ea5f408c9fb7aac"},"Annotations":[{"Key":"Name","Value":"c2Vzc2lvbi5QYXJzZVNRTA=="},{"Key":"_schema:name","Value":null},{"Key":"Span.Start","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzMTIrMDg6MDA="},{"Key":"Span.End","Value":"MjAyMC0wNS0yMlQxNzo1MDowNC40MTIzMjY4MTUrMDg6MDA="},{"Key":"_schema:Timespan","Value":null}],"Sub":null}]}]}] |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

可将 JSON 格式的跟踪文件粘贴到跟踪查看器中。查看器可通过 TiDB 状态端口访问:

TiDB Trace Viewer-1

TiDB Trace Viewer-2

MySQL 兼容性

TRACE 语句是 TiDB 对 MySQL 语法的扩展。

另请参阅