开发者社区> 技术小甜> 正文

mysql查询语句优化工具

简介:
+关注继续查看



把这个profiling功能打开,可以查看sql查询语句的整个过程中各种资源的消耗情况。

mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration   | Query                           |
+----------+------------+---------------------------------+
|        1 | 0.00049600 | show variables like "profiling" |
|        2 | 0.02267400 | show databases                  |
|        3 | 0.04235200 | SELECT DATABASE()               |
|        4 | 0.00012825 | SELECT DATABASE()               |
|        5 | 0.00025125 | show databases                  |
|        6 | 0.00145125 | show tables                     |
|        7 | 0.04207925 | show tables                     |
|        8 | 0.03485225 | select count(*) from host_item  |
+----------+------------+---------------------------------+


mysql> show profile cpu, block io  for query 8;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000634 | 0.000000 |   0.000000 |            0 |             0 |
| checking permissions | 0.000013 | 0.000000 |   0.000000 |            0 |             0 |
| Opening tables       | 0.000051 | 0.000000 |   0.000000 |            0 |             0 |
| System lock          | 0.000014 | 0.000000 |   0.000000 |            0 |             0 |
| init                 | 0.000015 | 0.000000 |   0.000000 |            0 |             0 |
| optimizing           | 0.033657 | 0.000000 |   0.000000 |           40 |             0 |
| statistics           | 0.000029 | 0.000000 |   0.000000 |            0 |             0 |
| preparing            | 0.000012 | 0.000000 |   0.000000 |            0 |             0 |
| executing            | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |
| Sending data         | 0.000313 | 0.000000 |   0.000000 |            0 |             0 |
| end                  | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |
| query end            | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |
| closing tables       | 0.000047 | 0.000000 |   0.000000 |            0 |             0 |
| freeing items        | 0.000042 | 0.000000 |   0.000000 |            0 |             0 |
| logging slow query   | 0.000003 | 0.000000 |   0.000000 |            0 |             0 |
| cleaning up          | 0.000003 | 0.000000 |   0.000000 |            0 |             0 |
+----------------------+----------+----------+------------+--------------+---------------


show profile后面可以跟参数:all或者cpu,block io,page faults等。


也可以在information_schema.profiling数据库表中直接查询:

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;


最后,关闭set profiling=0该功能。





















本文转自chenzudao51CTO博客,原文链接: http://blog.51cto.com/victor2016/1875012,如需转载请自行联系原作者






版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
MySQL优化
在我们的实际场景中经常会遇到sql查询较慢的问题,今天特地写一篇文章来聊聊我对于MySQL调优相关内容的知识,以及从哪些点去进行优化.
53 0
《云原生的 MySQL 托管服务架构及读写分离的优化(PHP)》电子版地址
云原生的 MySQL 托管服务架构及读写分离的优化(PHP)
42 0
数据库 mysql 优化一 听说发第二遍全网程序员都不会单身 下
数据库 mysql 优化一 听说发第二遍全网程序员都不会单身 下
32 0
数据库 mysql 优化一 听说发第二遍全网程序员都不会单身 上
数据库 mysql 优化一 听说发第二遍全网程序员都不会单身 上
16 0
42 张图带你撸完 MySQL 优化 下
42 张图带你撸完 MySQL 优化 下
17 0
42 张图带你撸完 MySQL 优化 中
42 张图带你撸完 MySQL 优化 中
27 0
42 张图带你撸完 MySQL 优化 上
42 张图带你撸完 MySQL 优化 上
30 0
Mysql优化之索引(二)
Mysql优化之索引(二)
16 0
+关注
技术小甜
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
让 MySQL 原生分布式触手可及
立即下载
好的 MySQL 兼容可以做到什么程度
立即下载
云数据库RDS MySQL从入门到高阶
立即下载