0
点赞
收藏
分享

微信扫一扫

【MySQL】MySQL的优化(三)


show profile分析SQL

Mysql从5.0.37版本开始增加了对 show profiles 和 show profile 语句的支持。show profiles 能够

做SQL优化时帮助我们了解时间都耗费到哪里去了。

通过 have_profiling 参数,能够看到当前MySQL是否支持profile:

select @@have_profiling; 
set profiling=1; -- 开启profiling 开关;

【MySQL】MySQL的优化(三)_java

 通过profile,我们能够更清楚地了解SQL执行的过程。首先,我们可以执行一系列的操作

show databases;

use mydb13_optimize;

show tables;

select * from user where id < 2;

select count(*) from user;

执行完上述命令之后,再执行show profiles 指令, 来查看SQL语句执行的耗时:

show profiles;

【MySQL】MySQL的优化(三)_mysql_02

通过show  profile for  query  query_id 语句可以查看到该SQL执行过程中每个线程的状态和消耗的

时间:

show profile for query 8;

 

【MySQL】MySQL的优化(三)_MySQL_03

在获取到最消耗时间的线程状态后,MySQL支持进一步选择all、cpu、block io 、context switch、

page faults等明细类型类查看MySQL在使用什么资源上耗费了过高的时间。例如,选择查看CPU

的耗费时间  :

show profile cpu for query 133;

 

【MySQL】MySQL的优化(三)_MySQL_04

在获取到最消耗时间的线程状态后,MySQL支持进一步选择all、cpu、block io 、context switch、

page faults等明细类型类查看MySQL在使用什么资源上耗费了过高的时间。例如,选择查看CPU

的耗费时间  : 

【MySQL】MySQL的优化(三)_MySQL_05

举报

相关推荐

0 条评论