MySQL調(diào)優(yōu)三部曲(三)PROFILE

分析SQL執(zhí)行帶來的開銷是優(yōu)化SQL的重要手段,MySQL可以通過設(shè)置profiling參數(shù),將SQL語句的資源開銷,如IO、上下文切換、CPU、Memory等記錄下來

查看profiling系統(tǒng)變量

mysql> show variables like '%profil%'; 
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+
  • have_profiling: 當(dāng)前版本是否支持profiling功能
  • profiling: 是否開啟profiling功能
  • profiling_history_size: 保留profiling的數(shù)目,默認(rèn)是15,范圍為0~100,為0時(shí)代表禁用profiling

開啟profiling

啟用session級(jí)別的profile
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
驗(yàn)證修改后的結(jié)果
mysql> show variables like '%profil%'; 
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | ON    |
| profiling_history_size | 15    |
+------------------------+-------+

進(jìn)行profile分析

1. 進(jìn)行Query操作
mysql> select count(*) from ttkk_user;
+----------+
| count(*) |
+----------+
|       55 |
+----------+
2. 查看當(dāng)前session產(chǎn)生的profile
mysql> show profiles;
+----------+------------+--------------------------------+
| Query_ID | Duration   | Query                          |
+----------+------------+--------------------------------+
|        1 | 0.00051550 | show variables like '%profil%' |
|        2 | 0.00016350 | select count(*) from ttkk_user |
+----------+------------+--------------------------------+
3. 獲取指定查詢的開銷
mysql> show profile for query 2;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000053 |
| checking permissions | 0.000007 |
| Opening tables       | 0.000019 |
| init                 | 0.000013 |
| System lock          | 0.000008 |
| optimizing           | 0.000008 |
| executing            | 0.000009 |
| end                  | 0.000004 |
| query end            | 0.000003 |
| closing tables       | 0.000008 |
| freeing items        | 0.000020 |
| cleaning up          | 0.000013 |
+----------------------+----------+
12 rows in set, 1 warning (0.00 sec)
當(dāng)查到最耗時(shí)的線程狀態(tài)時(shí),可以進(jìn)一步選擇all或者cpu、block io等明細(xì)類型來查看mysql在每個(gè)線程狀態(tài)中使用什么資源上耗費(fèi)了過高的時(shí)間
mysql>  show profile block io,cpu for query 2; 
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000053 | 0.000022 |   0.000026 |            0 |             0 |
| checking permissions | 0.000007 | 0.000003 |   0.000004 |            0 |             0 |
| Opening tables       | 0.000019 | 0.000008 |   0.000010 |            0 |             0 |
| init                 | 0.000013 | 0.000006 |   0.000008 |            0 |             0 |
| System lock          | 0.000008 | 0.000004 |   0.000004 |            0 |             0 |
| optimizing           | 0.000008 | 0.000003 |   0.000004 |            0 |             0 |
| executing            | 0.000009 | 0.000004 |   0.000005 |            0 |             0 |
| end                  | 0.000004 | 0.000002 |   0.000002 |            0 |             0 |
| query end            | 0.000003 | 0.000001 |   0.000001 |            0 |             0 |
| closing tables       | 0.000008 | 0.000003 |   0.000005 |            0 |             0 |
| freeing items        | 0.000020 | 0.000010 |   0.000011 |            0 |             0 |
| cleaning up          | 0.000013 | 0.000005 |   0.000007 |            0 |             0 |
+----------------------+----------+----------+------------+--------------+---------------+
12 rows in set, 1 warning (0.00 sec)
一條query每個(gè)階段的資源開銷可以從information_schema.profiling表查詢
mysql> select * from profiling where query_id = 2;
+----------+-----+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+------------------+-------------+
| QUERY_ID | SEQ | STATE                | DURATION | CPU_USER | CPU_SYSTEM | CONTEXT_VOLUNTARY | CONTEXT_INVOLUNTARY | BLOCK_OPS_IN | BLOCK_OPS_OUT | MESSAGES_SENT | MESSAGES_RECEIVED | PAGE_FAULTS_MAJOR | PAGE_FAULTS_MINOR | SWAPS | SOURCE_FUNCTION       | SOURCE_FILE      | SOURCE_LINE |
+----------+-----+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+------------------+-------------+
|        2 |   2 | starting             | 0.000053 | 0.000022 |   0.000026 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | NULL                  | NULL             |        NULL |
|        2 |   3 | checking permissions | 0.000007 | 0.000003 |   0.000004 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | check_access          | sql_parse.cc     |        5350 |
|        2 |   4 | Opening tables       | 0.000019 | 0.000008 |   0.000010 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | open_tables           | sql_base.cc      |        5095 |
|        2 |   5 | init                 | 0.000013 | 0.000006 |   0.000008 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_prepare_select  | sql_select.cc    |        1051 |
|        2 |   6 | System lock          | 0.000008 | 0.000004 |   0.000004 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_lock_tables     | lock.cc          |         304 |
|        2 |   7 | optimizing           | 0.000008 | 0.000003 |   0.000004 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | optimize              | sql_optimizer.cc |         139 |
|        2 |   8 | executing            | 0.000009 | 0.000004 |   0.000005 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | exec                  | sql_executor.cc  |         110 |
|        2 |   9 | end                  | 0.000004 | 0.000002 |   0.000002 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_execute_select  | sql_select.cc    |        1106 |
|        2 |  10 | query end            | 0.000003 | 0.000001 |   0.000001 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_execute_command | sql_parse.cc     |        5049 |
|        2 |  11 | closing tables       | 0.000008 | 0.000003 |   0.000005 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_execute_command | sql_parse.cc     |        5097 |
|        2 |  12 | freeing items        | 0.000020 | 0.000010 |   0.000011 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | mysql_parse           | sql_parse.cc     |        6486 |
|        2 |  13 | cleaning up          | 0.000013 | 0.000005 |   0.000007 |                 0 |                   0 |            0 |             0 |             0 |                 0 |                 0 |                 0 |     0 | dispatch_command      | sql_parse.cc     |        1815 |
+----------+-----+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+------------------+-------------+
information_schema.profiling表主要字段含義
  • state : 當(dāng)前query所在的階段
  • CPU_user : CPU用戶
  • CPU_system : CPU系統(tǒng)
  • Context_voluntary : 上下文主動(dòng)切換
  • Context_involuntary : 上下文被動(dòng)切換
  • Block_ops_in : 阻塞的輸入操作
  • Block_ops_out : 阻塞的輸出操作
  • Messages_sent : 消息發(fā)出
  • Messages_received : 消息接受
  • Page_faults_major : 主分頁錯(cuò)誤
  • Page_faults_minor : 次分頁錯(cuò)誤
  • Swaps : 交換次數(shù)
  • Source_function : 源功能
  • Source_file : 源文件
  • Source_line : 源代碼行
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容