分析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 : 源代碼行