mysql性能分析工具profiling_Mysql系列(十)—— 性能分析工具profiling
explain是從mysql怎樣解析執(zhí)行sql的角度分析sql優(yōu)劣。profiling是從sql執(zhí)行時(shí)資源使用情況的角度來分析sql。
分析SQL執(zhí)行帶來的開銷是優(yōu)化SQL的重要手段。在MySQL數(shù)據(jù)庫(kù)中,可以通過配置profiling參數(shù)來啟用SQL剖析。該參數(shù)可以在全局和session級(jí)別來設(shè)置。對(duì)于全局級(jí)別則作用于整個(gè)MySQL實(shí)例,而session級(jí)別緊影響當(dāng)前session。該參數(shù)開啟后,后續(xù)執(zhí)行的SQL語(yǔ)句都將記錄其資源開銷,諸如IO,上下文切換,CPU,Memory等等。根據(jù)這些開銷進(jìn)一步分析當(dāng)前SQL瓶頸從而進(jìn)行優(yōu)化與調(diào)整。本文描述了如何使用MySQL profile,不涉及具體的樣例分析。
1、有關(guān)profile的描述
查看profiling系統(tǒng)變量
mysql> show variables like '%profil%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES | #只讀變量,用于控制是否由系統(tǒng)變量開啟或禁用profiling;
| profiling | OFF | #開啟或關(guān)閉SQL語(yǔ)句剖析功能;
| profiling_history_size | 15 | #設(shè)置保留profiling的數(shù)目,缺省為15,范圍為0至100,為0時(shí)將禁用profiling;
+------------------------+-------+
3 rows in set (0.00 sec)
獲取profile的幫助
mysql> help profile;
Name: 'SHOW PROFILE'
Description:
Syntax:
SHOW PROFILE [type [, type] ... ]
[FOR QUERY n]
[LIMIT row_count [OFFSET offset]]
type:
ALL #顯示所有的開銷信息;
| BLOCK IO #顯示塊IO相關(guān)開銷;
| CONTEXT SWITCHES #上下文切換相關(guān)開銷;
| CPU #顯示CPU相關(guān)開銷信息;
| IPC #顯示發(fā)送和接收相關(guān)開銷信息;
| MEMORY #顯示內(nèi)存相關(guān)開銷信息;
| PAGE FAULTS #顯示頁(yè)面錯(cuò)誤相關(guān)開銷信息;
| SOURCE #顯示和Source_function,Source_file,Source_line相關(guān)的開銷信息;
| SWAPS #顯示交換次數(shù)相關(guān)開銷的信息;
2、開啟porfiling
啟用session級(jí)別的profiling
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 |
+------------------------+-------+
3 rows in set (0.00 sec)
發(fā)布SQL查詢
mysql> select * from zabbix.history_uint where clock<1488466329 limit 1;
查看當(dāng)前session所有已產(chǎn)生的profile
mysql> show profiles;
+----------+------------+------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------------------------------------------+
| 1 | 0.00044625 | show variables like '%profil%' |
| 2 | 6.43230200 | select * from zabbix.history_uint where clock<1488466329 limit 1 |
+----------+------------+------------------------------------------------------------------+
2 rows in set, 1 warning (0.00 sec)
我們看到有2個(gè)warning,之前一個(gè),現(xiàn)在一個(gè)
mysql> show warnings;
+---------+------+--------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'SHOW PROFILES' is deprecated and will be removed in a future release. Please use Performance Schema instead |
+---------+------+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
告警是說SHOW PROFILES命令將來會(huì)被Performance Schema替換掉。
3、獲取SQL語(yǔ)句的開銷信息
開啟profiling后,我們可以通過show profile等方式查看,其實(shí)這些開銷信息被記錄到information_schema.profiling表中。注show profile之類的語(yǔ)句不會(huì)被profiling,即自身不會(huì)產(chǎn)生Profiling。
我們下面的這個(gè)show profile查看的是show warnings產(chǎn)生的相應(yīng)開銷。
mysq> show profile;
+----------------+----------+
| Status | Duration |
+----------------+----------+
| starting | 0.000141 |
| query end | 0.000058 |
| closing tables | 0.000014 |
| freeing items | 0.001802 |
| cleaning up | 0.000272 |
+----------------+----------+
如下面的查詢show warnings被添加到profiles
mysql> show profiles;
+----------+------------+------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+------------------------------------------------------------------+
| 1 | 0.00041150 | show variables like '%profil%' |
| 2 | 6.41118075 | select * from zabbix.history_uint where clock<1488466329 limit 1 |
| 3 | 0.00003900 | show warnings |
+----------+------------+------------------------------------------------------------------+
3 rows in set, 1 warning (0.00 sec)
獲取指定查詢的開銷(Druation表示持續(xù)時(shí)間)
mysql> show profile for query 2;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000043 |
| checking permissions | 0.000004 |
| Opening tables | 0.000014 |
| init | 0.000019 |
| System lock | 0.000004 |
| optimizing | 0.000009 |
| statistics | 0.000062 |
| preparing | 0.000012 |
| executing | 0.000002 |
| Sending data | 6.410967 |
| end | 0.000009 |
| query end | 0.000005 |
| closing tables | 0.000010 |
| freeing items | 0.000011 |
| cleaning up | 0.000011 |
+----------------------+----------+
15 rows in set, 1 warning (0.00 sec)
Sending data:這個(gè)狀態(tài)的名稱很具有誤導(dǎo)性,所謂的“Sending data”并不是單純的發(fā)送數(shù)據(jù),而是包括“收集 + 發(fā)送 數(shù)據(jù)”。
query end:表示語(yǔ)句執(zhí)行完畢了,但是還有一些后續(xù)工作沒做完時(shí)的狀態(tài)。
freeing items:釋放查詢緩存里面的空間,如果是DML操作,所以相應(yīng)的緩存里的記錄就無效了,所以需要有這一步做處理。
查看所有開銷
mysql> show profile all for query 2 ;
查看特定部分的開銷,如下為CPU部分的開銷
mysql> show profile cpu for query 2 ;
如下為MEMORY部分的開銷
mysql> show profile memory for query 2 ;
同時(shí)查看不同資源開銷
mysql> show profile block io,cpu for query 2;
4、INFORMATION_SCHEMA.PROFILING
上面已經(jīng)看到了,show profile命令即將移除,所以可以直接去information_schema.profiling表查看,靈活度更大,其表結(jié)構(gòu)信息如下:
mysql> desc information_schema.profiling;
+---------------------+--------------+------+-----+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+----------+-------+
| QUERY_ID | int(20) | NO | | 0 | |
| SEQ | int(20) | NO | | 0 | |
| STATE | varchar(30) | NO | | | |
| DURATION | decimal(9,6) | NO | | 0.000000 | |
| CPU_USER | decimal(9,6) | YES | | NULL | |
| CPU_SYSTEM | decimal(9,6) | YES | | NULL | |
| CONTEXT_VOLUNTARY | int(20) | YES | | NULL | |
| CONTEXT_INVOLUNTARY | int(20) | YES | | NULL | |
| BLOCK_OPS_IN | int(20) | YES | | NULL | |
| BLOCK_OPS_OUT | int(20) | YES | | NULL | |
| MESSAGES_SENT | int(20) | YES | | NULL | |
| MESSAGES_RECEIVED | int(20) | YES | | NULL | |
| PAGE_FAULTS_MAJOR | int(20) | YES | | NULL | |
| PAGE_FAULTS_MINOR | int(20) | YES | | NULL | |
| SWAPS | int(20) | YES | | NULL | |
| SOURCE_FUNCTION | varchar(30) | YES | | NULL | |
| SOURCE_FILE | varchar(20) | YES | | NULL | |
| SOURCE_LINE | int(20) | YES | | NULL | |
+---------------------+--------------+------+-----+----------+-------+
18 rows in set (0.00 sec)
下面的SQL語(yǔ)句用于查詢query_id為2的SQL開銷,且按最大耗用時(shí)間倒序排列
mysql> set @query_id=2;
SELECT STATE, SUM(DURATION) AS Total_R,
ROUND(
100 * SUM(DURATION) /
(SELECT SUM(DURATION)
FROM INFORMATION_SCHEMA.PROFILING
WHERE QUERY_ID = @query_id
), 2) AS Pct_R,
COUNT(*) AS Calls,
SUM(DURATION) / COUNT(*) AS "R/Call"
FROM INFORMATION_SCHEMA.PROFILING
WHERE QUERY_ID = @query_id
GROUP BY STATE
ORDER BY Total_R DESC;
+----------------------+----------+--------+-------+--------------+
| STATE | Total_R | Pct_R | Calls | R/Call |
+----------------------+----------+--------+-------+--------------+
| Sending data | 6.410967 | 100.00 | 1 | 6.4109670000 |
| statistics | 0.000062 | 0.00 | 1 | 0.0000620000 |
| starting | 0.000043 | 0.00 | 1 | 0.0000430000 |
| init | 0.000019 | 0.00 | 1 | 0.0000190000 |
| Opening tables | 0.000014 | 0.00 | 1 | 0.0000140000 |
| preparing | 0.000012 | 0.00 | 1 | 0.0000120000 |
| cleaning up | 0.000011 | 0.00 | 1 | 0.0000110000 |
| freeing items | 0.000011 | 0.00 | 1 | 0.0000110000 |
| closing tables | 0.000010 | 0.00 | 1 | 0.0000100000 |
| optimizing | 0.000009 | 0.00 | 1 | 0.0000090000 |
| end | 0.000009 | 0.00 | 1 | 0.0000090000 |
| query end | 0.000005 | 0.00 | 1 | 0.0000050000 |
| System lock | 0.000004 | 0.00 | 1 | 0.0000040000 |
| checking permissions | 0.000004 | 0.00 | 1 | 0.0000040000 |
| executing | 0.000002 | 0.00 | 1 | 0.0000020000 |
+----------------------+----------+--------+-------+--------------+
15 rows in set (0.01 sec)
停止profile,可以設(shè)置profiling參數(shù),或者在session退出之后,profiling會(huì)被自動(dòng)關(guān)閉。
從以上可以看出,profiling即是將mysql處理sql的各個(gè)階段的資源消耗查詢統(tǒng)計(jì)出來。
總結(jié)
以上是生活随笔為你收集整理的mysql性能分析工具profiling_Mysql系列(十)—— 性能分析工具profiling的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 批量drop_MySql批量
- 下一篇: sql update 日期减6个月_【干