MySQL5.0.37版本以上支援PROFILING調試功能,讓您可以瞭解SQL語句消耗資源的詳細資料。因為它需要調用系統的getrusage()函數,所以只是在Linux/Unix類平台上才能使用,而不能在Windows平台上使用。而且,PROFILING是針對處理進程(process)而不是線程(thread)的,伺服器上的其他應用,可能會影響您的調試結果,因此,這個工具適合開發過程中的調試,如果要在生產環境中調試使用,則要注意它的局限性。
參考資料:http://dev.mysql.com/doc/refman/5.6/en/show-profile.html
PROFILING以及PROFILE和PROFILES的格式如下:
mysql> ? SHOW PROFILES;
--------------------------------------------------------------------------------
SHOW PROFILE [type [, type] … ]
[FOR QUERY n]
[LIMIT row_count [OFFSET offset]]
type:
ALL
| BLOCK IO
| CONTEXT SWITCHES
| CPU
| IPC
| MEMORY
| PAGE FAULTS
| SOURCE
| SWAPS
--------------------------------------------------------------------------------
預設情況下,這個調試功能是關閉的,您可以查看MySQL資料庫的變數參數:
mysql> SELECT @@PROFILING;
+-------------+
| @@PROFILING |
+-------------+
| 0 |
+-------------+
mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+
需要使用時,可以通過SQL命令開啟調試功能:
mysql> SET PROFILING=1;
然後,開始執行需要測試的SQL語句,MySQL資料庫將會記錄想關的調試資訊,例如:
mysql> SELECT * FROM mysql.user;
mysql> SHOW PROFILE;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000041 |
| Waiting for query cache lock | 0.000007 |
| checking query cache for query | 0.000044 |
| checking permissions | 0.000011 |
| Opening tables | 0.000024 |
| System lock | 0.000015 |
| init | 0.000038 |
| optimizing | 0.000009 |
| statistics | 0.000014 |
| preparing | 0.000012 |
| executing | 0.000006 |
| Sending data | 0.000087 |
| end | 0.000008 |
| query end | 0.000006 |
| closing tables | 0.000010 |
| freeing items | 0.000017 |
| logging slow query | 0.000007 |
| cleaning up | 0.000007 |
+--------------------------------+----------+
18 rows in set (0.00 sec)
也可以顯示當前所有已經記錄的PROFILES,例如:
mysql> SHOW PROFILES;
+----------+------------+--------------------+
| Query_ID | Duration | Query |
+----------+------------+--------------------+
| 1 | 0.07194300 | SHOW DATABASES |
| 2 | 0.01126250 | SELECT DATABASE() |
| 3 | 0.00059125 | SHOW TABLES |
| 4 | 0.96143900 | SELECT * FROM |
+----------+------------+--------------------+
4 rows in set (0.00 sec)
注意:這個List的長度由MySQL的資料庫變數參數profiling_history_size決定,預設為15。
(命令查詢資料庫變數參數:mysql> show variables like 'profiling_history_size';)