日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MYSQL监控-自带工具Query Profiler的使用

發布時間:2025/1/21 数据库 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MYSQL监控-自带工具Query Profiler的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Query Profiler是MYSQL自帶的一種query診斷分析工具,通過它可以分析出一條SQL語句的性能瓶頸在什么地方。通常我們是使用的explain,以及slow query log都無法做到精確分析,但是Query Profiler卻可以定位出一條SQL語句執行的各種資源消耗情況,比如CPU,IO等,以及該SQL執行所耗費的時間等。不過該工具只有在MYSQL 5.0.37以及以上版本中才有實現。
默認的情況下,MYSQL的該功能沒有打開,需要自己手動啟動。可以通過如下方法查看當前mysql服務器是否開啟了該功能。
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name????????? | Value |
+------------------------+-------+
| profiling????????????? | OFF?? |
| profiling_history_size | 15??? |
+------------------------+-------+
2 rows in set (0.03 sec)
profiling參數值為OFF,說明沒有打開該功能。
profiling_history_size參數值為15表示,記錄最近15次的查詢歷史。該值可以修改。
下邊說說如何打開profiling功能:
MYSQL提示符下執行如下命令:
?mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
然后再次檢驗下執行的效果:
mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name????????? | Value |
+------------------------+-------+
| profiling????????????? | ON??? |
| profiling_history_size | 15??? |
+------------------------+-------+
2 rows in set (0.00 sec)
profiling值為ON說明已經啟動該功能。
下邊說說如何使用show profiles;
1.首先執行一查詢語句:
mysql> select * from user;
+------+-----------+
| id?? | name????? |
+------+-----------+
|?? 22 | abc?????? |
|? 223 | dabc????? |
| 2232 | dddabc??? |
|?? 45 | asdsagd?? |
|?? 23 | ddddddddd |
|?? 22 | ddd?????? |
|?? 28 | sssddd??? |
+------+-----------+
7 rows in set (0.06 sec)
2.通過show profiles命令查看系統中多個query的概要信息:
mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration?? | Query???????????????????????????? |
+----------+------------+-----------------------------------+
|??????? 1 | 0.00013600 | set profiling=1?????????????????? |
|??????? 2 | 0.00092300 | show variables like '%profiling%' |
|??????? 3 | 0.08506075 | show databases??????????????????? |
|??????? 4 | 0.02698550 | SELECT DATABASE()???????????????? |
|??????? 5 | 0.07408475 | show tables?????????????????????? |
|??????? 6 | 0.05769725 | select * from user??????????????? |
+----------+------------+-----------------------------------+
6 rows in set (0.01 sec)
其中Query_ID表示查詢ID,也就是個編號,Duration表示對應的query語句執行的時間,單位是秒,query表示具體的query語句。我們可以看到剛才我們最后執行的select * from user語句執行的時間是0.05769725,單位是秒,也就是57ms
3.獲取單個query的詳細profile信息,可以通過如下語句:
mysql> show profile cpu,block io for query 6;
+--------------------+----------+----------+------------+--------------+---------------+
| Status???????????? | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+--------------------+----------+----------+------------+--------------+---------------+
| starting?????????? | 0.014438 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| Opening tables???? | 0.042860 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| System lock??????? | 0.000007 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| Table lock???????? | 0.000012 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| init?????????????? | 0.000019 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| optimizing???????? | 0.000005 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| statistics???????? | 0.000018 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| preparing????????? | 0.000011 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| executing????????? | 0.000004 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| Sending data?????? | 0.000232 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| end??????????????? | 0.000007 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| query end????????? | 0.000005 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| freeing items????? | 0.000073 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| logging slow query | 0.000003 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
| cleaning up??????? | 0.000004 |???? NULL |?????? NULL |???????? NULL |????????? NULL |
+--------------------+----------+----------+------------+--------------+---------------+
15 rows in set (0.02 sec)
總結:Query Profiler對于SQL性能分析和診斷費用有用。另外,該命令還有如下參數可以選擇:
  • ALL - displays all information?
  • BLOCK IO - displays counts for block input and output operations?
  • CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches?
  • IPC - displays counts for messages sent and received?
  • MEMORY - is not currently implemented?
  • PAGE FAULTS - displays counts for major and minor page faults?
  • SOURCE - displays the names of functions from the source code, together with the name and line number of the file in which the function occurs?
  • SWAPS - displays swap counts?

使用SQLyog等工具可以直接看到Profiler頁面,輸入 Show profiles;語句后會顯示更為詳細的信息。

轉載于:https://my.oschina.net/jiyayun/blog/85010

總結

以上是生活随笔為你收集整理的MYSQL监控-自带工具Query Profiler的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。