使用MySQL的PROFILING調試功能

來源:互聯網
上載者:User

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';)

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.