久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

mysql中如何使用show profiles分析sql性能

137次閱讀
沒有評論

共計 3782 個字符,預計需要花費 10 分鐘才能閱讀完成。

這篇文章主要介紹了 mysql 中如何使用 show profiles 分析 sql 性能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。

Show profiles 是 5.0.37 之后添加的,要想使用此功能,要確保版本在 5.0.37 之后。
 
查看一下我的版本
Select  version();
+———————+
| version()           |
+———————+
| 5.0.82-community-nt |
+———————+
  www.2cto.com  
1 row in set (0.00 sec)
 
版本是支持 show profiles 功能的。接下來進入 mysql 性能跟蹤診斷的世界
 
查看是否打開了 profiles 功能, 默認是關閉的
 
mysql use test;
 
Database changed
 
mysql show profiles;
 
Empty set (0.00 sec)
 
顯示為空,說明 profiles 功能是關閉的。下面開啟
 
mysql set profiling=1;
 
Query OK, 0 rows affected (0.00 sec)
 
執行下面的查詢
  www.2cto.com  
mysql explain select distinct player_idfrom task limit 20;
 
mysql select distinct player_id from task ;
 
然后執行 show profiles
 
mysql show profiles;
 
+———-+————+——————————————————+
 
| Query_ID | Duration   | Query                                               |
 
+———-+————+——————————————————+
 
|       1 | 0.00035225 | explain select distinct player_id from task limit 20 |
 
|       2 | 1.91772775 | select distinct player_id from task                  |
 
+———-+————+——————————————————+
 
此時可以看到執行 select distinct player_id from task 用了 1.91772775 秒的時間
 
根據 query_id 查看某個查詢的詳細時間耗費
 
mysql show profile for query 2;
  www.2cto.com  
+———————-+———-+
 
| Status               | Duration |
 
+———————-+———-+
 
| starting             | 0.000052 |
 
| Opening tables       | 0.000009 |
 
| System lock          | 0.000003 |
 
| Table lock           | 0.000007 |
 
| init                 | 0.000013 |
 
| optimizing           | 0.000003 |
 
| statistics           | 0.000009 |
 
| preparing            | 0.000008 |
 
| Creating tmp table   | 0.000074 |
 
| executing            | 0.000002 |
 
| Copying to tmp table |1.916551 |
  www.2cto.com  
| Sending data         | 0.000667 |
 
| end                  | 0.000004 |
 
| removing tmp table   | 0.000065 |
 
| end                  | 0.000002 |
 
| end                  | 0.000002 |
 
| query end            | 0.000003 |
 
| freeing items        | 0.000245 |
 
| closing tables       | 0.000006 |
 
| logging slow query   | 0.000002 |
 
| cleaning up          | 0.000003 |
 
+———————-+———-+
 
可以看到紅色字體部分耗費了大量時間,這是因為 distinct 查看會用到臨時表
 
那么可不可以查看占用 cpu、io 等信息呢
 
 mysql show profile block io,cpu for query2;
 
+———————-+———-+———-+————+————–+——
 
———+
 
| Status               | Duration | CPU_user |CPU_system | Block_ops_in | Block
 
_ops_out |
 
+———————-+———-+———-+————+————–+——
  www.2cto.com  
———+
 
| starting             | 0.000052 |     NULL |       NULL |         NULL |
 
   NULL |
 
| Opening tables       | 0.000009 |     NULL |       NULL |         NULL |
 
   NULL |
 
| System lock          | 0.000003 |     NULL |       NULL |         NULL |
 
   NULL |
 
| Table lock           | 0.000007 |     NULL |       NULL |         NULL |
 
   NULL |
 
| init                 | 0.000013 |     NULL |       NULL |         NULL |
 
   NULL |
 
| optimizing           | 0.000003 |     NULL |       NULL |         NULL |
 
   NULL |
 
| statistics           | 0.000009 |     NULL |       NULL |         NULL |
 
   NULL |  www.2cto.com  
 
| preparing            | 0.000008 |     NULL |       NULL |        NULL |
 
   NULL |
 
| Creating tmp table   | 0.000074 |     NULL |       NULL |         NULL |
 
   NULL |
 
| executing            | 0.000002 |     NULL |       NULL |         NULL |
 
   NULL |
 
| Copying to tmp table | 1.916551 |     NULL |       NULL |        NULL |
 
   NULL |
 
| Sending data         | 0.000667 |     NULL |       NULL |         NULL |
 
   NULL |
 
| end                  | 0.000004 |     NULL |       NULL |         NULL |
 
   NULL |
 
| removing tmp table   | 0.000065 |     NULL |       NULL |         NULL |
 
   NULL |
 
| end                  | 0.000002 |     NULL |       NULL |         NULL |
 
   NULL |
 
| end                  | 0.000002 |     NULL |       NULL |         NULL |
 
   NULL |
 
| query end            | 0.000003 |     NULL |       NULL |         NULL |
 
   NULL |
 
| freeing items        | 0.000245 |     NULL |       NULL |         NULL |
 
   NULL |
 
| closing tables       | 0.000006 |     NULL |       NULL |         NULL |
 
   NULL |
  www.2cto.com  
| logging slow query   | 0.000002 |     NULL |       NULL |         NULL |
 
   NULL |
 
| cleaning up          | 0.000003 |     NULL |       NULL |         NULL |
 
   NULL |
 
+———————-+———-+———-+————+————–+——
另外還可以看到 memory,swaps,context switches,source 等信息
 

感謝你能夠認真閱讀完這篇文章,希望丸趣 TV 小編分享的“mysql 中如何使用 show profiles 分析 sql 性能”這篇文章對大家有幫助,同時也希望大家多多支持丸趣 TV,關注丸趣 TV 行業資訊頻道,更多相關知識等著你來學習!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-15發表,共計3782字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 盐山县| 宜都市| 文登市| 松阳县| 左权县| 宿迁市| 石嘴山市| 昆明市| 龙门县| 文成县| 南陵县| 桦甸市| 崇信县| 民丰县| 全椒县| 巨鹿县| 乐至县| 荆门市| 冕宁县| 湟中县| 乳山市| 防城港市| 高雄县| 集安市| 乌审旗| 友谊县| 金乡县| 德江县| 紫阳县| 华池县| 崇明县| 扎兰屯市| 靖宇县| 奇台县| 射洪县| 湘阴县| 柳江县| 湛江市| 西乌珠穆沁旗| 望谟县| 通州区|