TRACE

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

语法图

TraceStmt:

TraceStmt

TraceableStmt:

TraceableStmt

示例

  1. mysql> trace format='row' select * from mysql.user;
  2. ++--++
  3. | operation | startTS | duration |
  4. ++--++
  5. | session.getTxnFuture | 10:33:34.647148 | 3.847µs |
  6. | ├─session.Execute | 10:33:34.647146 | 536.233µs |
  7. | ├─session.ParseSQL | 10:33:34.647182 | 19.868µs |
  8. | ├─executor.Compile | 10:33:34.647219 | 295.688µs |
  9. | ├─session.runStmt | 10:33:34.647533 | 116.229µs |
  10. | ├─session.CommitTxn | 10:33:34.647631 | 5.44µs |
  11. | ├─recordSet.Next | 10:33:34.647707 | 833.103µs |
  12. | ├─tableReader.Next | 10:33:34.647709 | 806.783µs |
  13. | ├─recordSet.Next | 10:33:34.648572 | 19.367µs |
  14. | └─tableReader.Next | 10:33:34.648575 | 1.783µs |
  15. ++--++
  16. 10 rows in set (0.00 sec)
  17. mysql> CREATE TABLE t1 (id int not null primary key auto_increment);
  18. Query OK, 0 rows affected (0.11 sec)
  19. mysql> INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
  20. Query OK, 5 rows affected (0.02 sec)
  21. Records: 5 Duplicates: 0 Warnings: 0
  22. mysql> TRACE FORMAT='json' SELECT * FROM t1 WHERE id = 2\G
  23. operation: [
  24. {"ID":{"Trace":"60d20d005593de87","Span":"44e5b309242ffe2f","Parent":"79d146dac9a29a7e"},
  25. "Annotations":[
  26. {"Key":"Name","Value":"c2Vzc2lvbi5nZXRUeG5GdXR1cmU="},
  27. {"Key":"_schema:name","Value":null},
  28. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE2MTQ3ODYtMDY6MDA="},
  29. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE2MjA0MDYtMDY6MDA="},
  30. {"Key":"_schema:Timespan","Value":null}
  31. ],
  32. "Sub":[
  33. {"ID":{"Trace":"60d20d005593de87","Span":"4dbf8f2ca373b4b0","Parent":"79d146dac9a29a7e"},
  34. "Annotations":[
  35. {"Key":"Name","Value":"c2Vzc2lvbi5QYXJzZVNRTA=="},
  36. {"Key":"_schema:name","Value":null},
  37. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE2NjE1MTQtMDY6MDA="},
  38. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE3MDYxNjgtMDY6MDA="},
  39. {"Key":"_schema:Timespan","Value":null}
  40. ],
  41. "Sub":null},
  42. {"ID":{"Trace":"60d20d005593de87","Span":"6b6d6916df809604","Parent":"79d146dac9a29a7e"},
  43. "Annotations":[
  44. {"Key":"Name","Value":"ZXhlY3V0b3IuQ29tcGlsZQ=="},
  45. {"Key":"_schema:name","Value":null},
  46. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE3NTcyODUtMDY6MDA="},
  47. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE3MzE0MjYtMDY6MDA="},
  48. {"Key":"_schema:Timespan","Value":null}
  49. ],
  50. "Sub":null},
  51. {"ID":{"Trace":"60d20d005593de87","Span":"3f1bcdd402a72911","Parent":"79d146dac9a29a7e"},
  52. "Annotations":[
  53. {"Key":"Name","Value":"c2Vzc2lvbi5Db21taXRUeG4="},
  54. {"Key":"_schema:name","Value":null},
  55. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE3OTgyNjItMDY6MDA="},
  56. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE4MDU1NzYtMDY6MDA="},
  57. {"Key":"_schema:Timespan","Value":null}
  58. ],
  59. "Sub":null},
  60. {"ID":{"Trace":"60d20d005593de87","Span":"58c1f7d66dc5afbc","Parent":"79d146dac9a29a7e"},
  61. "Annotations":[
  62. {"Key":"Name","Value":"c2Vzc2lvbi5ydW5TdG10"},
  63. {"Key":"_schema:name","Value":null},
  64. {"Key":"Msg","Value":"eyJzcWwiOiJTRUxFQ1QgKiBGUk9NIHQxIFdIRVJFIGlkID0gMiJ9"},
  65. {"Key":"Time","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE3ODA1NjgtMDY6MDA="},
  66. {"Key":"_schema:log","Value":null},
  67. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE4MTk5MzMtMDY6MDA="},
  68. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE3NzcyNDItMDY6MDA="},
  69. {"Key":"_schema:Timespan","Value":null}
  70. ],
  71. "Sub":null},
  72. {"ID":{"Trace":"60d20d005593de87","Span":"6bd8cc440fb31ed7","Parent":"79d146dac9a29a7e"},
  73. "Annotations":[
  74. {"Key":"Name","Value":"c2Vzc2lvbi5FeGVjdXRl"},
  75. {"Key":"_schema:name","Value":null},
  76. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE2MTEwODktMDY6MDA="},
  77. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE4NTU0My0wNjowMA=="},
  78. {"Key":"_schema:Timespan","Value":null}
  79. ],
  80. "Sub":null},
  81. {"ID":{"Trace":"60d20d005593de87","Span":"61d0b809f6cc018b","Parent":"79d146dac9a29a7e"},
  82. "Annotations":[
  83. {"Key":"Name","Value":"cmVjb3JkU2V0Lk5leHQ="},
  84. {"Key":"_schema:name","Value":null},
  85. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDE4NzQ1NTYtMDY6MDA="},
  86. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDIyOTg4NjYtMDY6MDA="},
  87. {"Key":"_schema:Timespan","Value":null}
  88. ],
  89. "Sub":null},
  90. {"ID":{"Trace":"60d20d005593de87","Span":"2bd2c3d47ccb1133","Parent":"79d146dac9a29a7e"},
  91. "Annotations":[
  92. {"Key":"Name","Value":"cmVjb3JkU2V0Lk5leHQ="},
  93. {"Key":"_schema:name","Value":null},
  94. {"Key":"Span.Start","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDIzMjY0ODgtMDY6MDA="},
  95. {"Key":"Span.End","Value":"MjAxOS0wNC0xN1QxMDozOToxMC45NDIzMjkwMDMtMDY6MDA="},
  96. {"Key":"_schema:Timespan","Value":null}
  97. ],
  98. "Sub":null}
  99. ]
  100. }
  101. ]
  102. 1 row in set (0.00 sec)

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

TiDB Trace Viewer-1

TiDB Trace Viewer-2

MySQL 兼容性

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

另请参阅