mysql中如何使用showprofiles分析sql性能

这篇文章主要介绍了MySQL中如何使用show profiles分析sql性能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

苏尼特左网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。创新互联建站2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站

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 等信息

感谢你能够认真阅读完这篇文章,希望小编分享的“mysql中如何使用show profiles分析sql性能”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


名称栏目:mysql中如何使用showprofiles分析sql性能
当前地址:http://bzwzjz.com/article/gocdep.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设公司 成都网站建设 成都网站制作 重庆网站设计 企业网站建设 泸州网站建设 网站设计 成都网站建设 成都网站建设 定制级高端网站建设 定制网站建设多少钱 重庆外贸网站建设 成都网站设计 移动手机网站制作 定制网站建设 高端网站设计 成都企业网站建设公司 成都网站建设公司 营销型网站建设 成都网站制作 温江网站设计 营销网站建设