版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
Show Profile
show profile 是mysql 提供可以用来分析当前会话中语句执行的资源消耗情况,可以用于SQL的调优的测量。默认情况下处于关闭状态,并且保存最近十五次的运行结果。
如何使用
查看是否开启
show variables like 'profiling';
# 默认是关闭的
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | OFF |
+---------------+-------+
开启profile
set global profiling = 1;
# 再次查看
show variables like "profiling";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | OFF |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
# 这里我们需要重新创建一个链接,才能够发现这个改动
mysql -uroot -proot
# 再次查看
mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | ON |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)
使用创建的50W 数据表运行以下sql
SELECT * FROM emp group by id%10 limit 150000;
SELECT * FROM emp group by id%20 order by 5;
通过show profiles 查看之前使用过的sql 语句
show profiles
-> ;
+----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| 1 | 0.00010600 | select @@version_comment limit 1 |
| 2 | 0.00269600 | show variables like "profiling" |
| 3 | 0.04826525 | show databases |
| 4 | 0.00196950 | SELECT DATABASE() |
| 5 | 0.17802325 | select count(1) from emp |
| 6 | 0.54059625 | SELECT * FROM emp group by id%10 limit 150000 |
| 7 | 0.53929825 | SELECT * FROM emp group by id%10 limit 500000 |
| 8 | 0.54338100 | SELECT * FROM emp group by id%20 order by 5 |
| 9 | 0.00002725 | SET autocommit = 0 |
| 10 | 0.00009475 | INSERT INTO emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) VALUES ((START+i), rand_string(6),'SALESMAN',0001,CURDATE(),2000,400,rand_num()) |
| 11 | 1.62303475 | select * from emp |
| 12 | 2.18617400 | select count(1) from emp |
| 13 | 1.91765300 | select count(1) from emp |
| 14 | 1.65586050 | SELECT * FROM emp group by id%10 limit 500000 |
| 15 | 1.68037550 | SELECT * FROM emp group by id%20 order by 5 |
+----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
8 rows in set, 1 warning (0.00 sec)
这张表中由大概 150W 条数据。 现在我们使用 profiles 来查看一下SQL分析
如何使用:
show profile cpu,block io for query SQLID
# 其他参数
all 显示所有开销
block io 显示io相关开销
context switches 上下文切换相关开销
cpu cpu 相关开销信息
ipc 显示发送和接收相关开销信息
memory 显示内存开销信息
page faults 显示页面错误相关开销信息
source 显示 source_function, source_file, source_line 相关的开销信息
swaps 显示交换次数相关开销的信息
上面的SQL展示 可以看到 11 , 12 , 13 ,14 ,15 ,耗费时间比较长。看一下都是哪里花费了时间把
show profile cpu , block io for query 12;
+----------------------+----------+----------+------------+--------------+---------------+
| Status | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting | 0.000050 | 0.000000 | 0.000000 | NULL | NULL |
| checking permissions | 0.000005 | 0.000000 | 0.000000 | NULL | NULL |
| Opening tables | 0.000011 | 0.000000 | 0.000000 | NULL | NULL |
| init | 0.000013 | 0.000000 | 0.000000 | NULL | NULL |
| System lock | 0.000007 | 0.000000 | 0.000000 | NULL | NULL |
| optimizing | 0.000004 | 0.000000 | 0.000000 | NULL | NULL |
| statistics | 0.000009 | 0.000000 | 0.000000 | NULL | NULL |
| preparing | 0.000013 | 0.000000 | 0.000000 | NULL | NULL |
| executing | 0.000002 | 0.000000 | 0.000000 | NULL | NULL |
| Sending data | 2.185983 | 0.234375 | 0.171875 | NULL | NULL |
| end | 0.000007 | 0.000000 | 0.000000 | NULL | NULL |
| query end | 0.000006 | 0.000000 | 0.000000 | NULL | NULL |
| closing tables | 0.000006 | 0.000000 | 0.000000 | NULL | NULL |
| freeing items | 0.000053 | 0.000000 | 0.000000 | NULL | NULL |
| cleaning up | 0.000007 | 0.000000 | 0.000000 | NULL | NULL |
+----------------------+----------+----------+------------+--------------+---------------+
# 主要时间花费在了 Sending Data
出现以下问题比较严重
converting HEAP to MyISAM 查询结果太大,内存不够用复制到磁盘上了
Creating tmp table 创建临时表 , 拷贝数据到临时表,用完之后在删除
Copying to tmp table on disk 把内存中临时表复制到磁盘危险
locked 锁住