profiling 需每次連線重新啟用mysql> SET profiling = 1;
mysql> SHOW TABLES;
mysql> SELECT COUNT(*) FROM t;
mysql> SHOW PROFILES;
+----------+------------+------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------+
| 1 | 0.02717000 | show tables |
| 2 | 0.74770100 | select count(*) from t |
+----------+------------+------------------------------+
mysql> SHOW PROFILE FOR QUERY 2;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| (initialization) | 0.000004 |
| checking query cache for query | 0.000044 |
| Opening tables | 0.000012 |
| System lock | 0.000017 |
| Table lock | 0.000030 |
| init | 0.000013 |
| optimizing | 0.000008 |
| statistics | 0.000013 |
| preparing | 0.000011 |
| executing | 0.000006 |
| Sending data | 0.747313 | -- 核心耗時
| end | 0.000014 |
| query end | 0.000006 |
| storing result in query cache | 0.000006 |
| freeing items | 0.000012 |
| closing tables | 0.000009 |
| logging slow query | 0.000183 |
+--------------------------------+----------+
mysql> SHOW PROFILE CPU, BLOCK IO, MEMORY, SWAPS FOR QUERY 4;
mysql> SHOW PROFILE CPU, BLOCK IO, MEMORY, SWAPS, CONTEXT SWITCHES, SOURCE FOR QUERY 5;
| 欄位 | 含義 |
|---|---|
| Duration | 階段耗時(秒) |
| CPU_user / CPU_system | 用戶態/核心態 CPU 時間 |
| Context_voluntary / Context_involuntary | 自願/非自願上下文切換次數 |
| Block_ops_in / Block_ops_out | 區塊讀/寫操作數 |
| Swaps | 交換次數 |
| Source_function / Source_file / Source_line | MySQL 原始碼函數/檔案/行號 |
Copying to tmp table 1.26 秒Converting HEAP to MyISAM 0.38 秒Copying to tmp table on disk 1.42 秒Sending data 31.1 秒(佔 90%+)Updating 44.5 秒(CPU 用戶態 27.9 秒、核心態 2.8 秒)tmp_table_size 落盤,調大參數或優化 SQL 避免臨時表MySQL 5.7+ 已棄用 SHOW PROFILE,改用 Performance Schema(events_statements_history_long、events_stages_history_long)進行更完整的效能分析。