原文地址:http://my.oschina.net/jiyayun/blog/85010
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;语句后会显示更为详细的信息。
分享到:
相关推荐
NET-Memory-Profiler 4-使用手册,检查内存,是否溢出等,记录,图片查看
前端开源库-v8-profiler-node8基于V8 profiler@5.7.0的V8-profiler-node8解决了节点8.x中的V8 profiler段错误。
yet-another-django-profiler, Django 性能分析中间件和管理命令 yet-another-django-profiler自述文件 然而,另一个 Django 探查器尝试合并多年来创建的它的他 Django 分析实用程序的最佳特性。 ( 有关更多背景信息...
适用于MacOS的NVIDIA nvvp(nvidia-visual-profiler)工具,11.1版本。
gae-java-mini-profiler.zip,google a p p engine java runtime的mini profiler(灵感来自于https://github.com/kamens/gae_mini_profiler上的python gae_mini_profiler和...
rack-perftools_profiler, 使用 perftools.rb 配置 rack 兼容应用程序的中间件 Rack::PerftoolsProfiler使用 perftools.rb 配置机架兼容应用的中间件快速启动假定应用程序使用 Rails 3 ( 你已经在下一节中安装了需求...
statsd-jvm-profiler, 使用StatsD和其他度量后端的简单 JVM statsd-jvm-profiler statsd-jvm-profiler是一个将分析数据发送到StatsD的JVM代理探查器。 灵感来自于 ,它主要用于分析Hadoop作业,但可以用于任何JVM...
Maven BuildTime事件探查器 通常,您会遇到这样的问题:在大型版本中,您需要查找时间消耗在哪里。 这是一个实现,它收集所有阶段和mojo执行的所有信息,并在构建结束时进行汇总输出。...buildtime-profiler-0.
资源来自pypi官网。 资源全名:yet-another-django-profiler-1.0.0.tar.gz
MySQL数据库系统监控 JDBC SQL日志记录工具P6spy P6spy的安装、配置(以Jboss3.2为例) DBMonster的安装和配置 DBMonster命令示例-提取Schema 在以源码方式安装的MySQL系统环境下 MySQL数据库运行情况测试 cd sql-...
clj-async-profiler:基于https:github.comjvm-profiling-toolsasync-profiler构建的可嵌入Clojure分析器
SQL 自带的 SQL Profiler首先是standard版本才有,所以不是免费的,还有就是设置起来太麻烦,后来又搜了SqlExpressTrace,也是要配置。还是这个Express Profiler好,打开就用,很快就能监测到你的Sql是怎么跑的了。
python库。 资源全名:google-cloud-profiler-3.0.5.tar.gz
用了Mysql 之后,发现就没有远程跟踪的工具了..所以本人利用C#开发一个工具类似于mssqlprofiler的跟踪工具.可以远程跟踪执行的语句. 在程序中屏蔽了一些系统语句.所以很清爽,不会错过一个有用的语句,比较稳定.支持...
Laravel开发-profiler-laravel 事件探查器的Laravel实现
django-web-profiler-master.rar
高通gpu 性能分析工具 可用于分析显存 纹理 shader 等 非常方便的开发者工具 有需要的快下载吧
火焰图工具async-profiler-1.7.tar.gz
Unity Profiler数据导出器从统一分析器导出数据并提供一些有用的统计信息的实用程序。 这是第一个工作版本,但我打算很快添加其他功能。 任何愿意捐款的人都非常欢迎,只需提出要求即可。安装只需将脚本复制到统一...
火焰图工具async-profiler-master.zip