TRACE

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

语法图

TraceStmt:

TraceStmt

TraceableStmt:

TraceableStmt

示例

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

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

TiDB Trace Viewer-1

TiDB Trace Viewer-2

MySQL 兼容性

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

另请参阅