show profil命令是mysql提供可以用来分析当前会话中语句执行的资源消耗情况。可以用于sql的调优的测量。
前言:show profil命令是mysql提供可以用来分析当前会话中语句执行的资源消耗情况。可以用于sql的调优的测量。
一、参数的开启和关闭设置
1.1 参数的查看
默认情况下,参数处于关闭状态,并保存最近15次的运行结果
mysql> show variables like 'profiling%';
+------------------------+-------+
| variable_name | value |
+------------------------+-------+
| profiling | off |
| profiling_history_size | 15 |
+------------------------+-------+
2 rows in set
1.2 参数的开启和关闭(参数为会话级参数,只对当前会话有效)
开启操作如下:
mysql> set profiling=1;或 set profiling=on;
mysql> set profiling=on;
query ok, 0 rows affected
mysql> show variables like 'profiling%';
+------------------------+-------+
| variable_name | value |
+------------------------+-------+
| profiling | on |
| profiling_history_size | 15 |
+------------------------+-------+
2 rows in set
关闭的操作:
mysql> set profiling=0;或 set profiling=off;
二、操作步骤
2.1 进行开启操作: set profiling=on;
2.2 运行相应的sql语句;
2.3 查看总体结果:show profiles;
2.4 查看详细的结果:show profile for query n,这里的n就是对应show profiles输出中的query_id;
mysql> show profiles;
+----------+------------+------------------------------------+
| query_id | duration | query |
+----------+------------+------------------------------------+
| 1 | 0.00077425 | select count(*) from tab_user_info |
| 2 | 0.0013575 | select count(*) from tab_tel_area |
| 3 | 9.7e-5 | select count(*) from tab_tel_area |
| 4 | 0.005193 | show variables like 'profiling%' |
+----------+------------+------------------------------------+
4 rows in set
mysql> show profile for query 2;
+--------------------------------+----------+
| status | duration |
+--------------------------------+----------+
| starting | 2e-5 |
| checking query cache for query | 4.7e-5 |
| opening tables | 0.001163 |
| system lock | 4e-6 |
| table lock | 4.1e-5 |
| init | 1.6e-5 |
| optimizing | 6e-6 |
| executing | 1.4e-5 |
| end | 5e-6 |
| query end | 3e-6 |
| freeing items | 3.1e-5 |
| storing result in query cache | 5e-6 |
| logging slow query | 3e-6 |
| cleaning up | 2e-6 |
+--------------------------------+----------+
14 rows in set
说明:报告给出了查询执行的每个步骤及花费的时间,当语句是很简单的一次执行的时候,可以很清楚的看出语句每个顺序花费的时间,但是当语句是嵌套循环等操作的时候,看这个报告就会变得很痛苦,因此整理了以下语句对相同类型的操作进行汇总,脚本如下:
mysql> set @query_id=1;
mysql> 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;
总结:这个工具又让我联想到了oracle的autotrace工具,如果有相应的执行计划一起带出来,,那么对语句的调优帮助将更大;
--------------------------------------分割线 --------------------------------------
ubuntu 14.04下安装mysql
《mysql权威指南(原书第2版)》清晰中文扫描版 pdf
ubuntu 14.04 lts 安装 lnmp nginx\php5 (php-fpm)\mysql
ubuntu 14.04下搭建mysql主从服务器
ubuntu 12.04 lts 构建高可用分布式 mysql 集群
ubuntu 12.04下源代码安装mysql5.6以及python-mysqldb
mysql-5.5.38通用二进制安装
--------------------------------------分割线 --------------------------------------
本文永久更新链接地址: