zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

【MySQL】MySQL的优化(三)

mysql 优化
2023-06-13 09:17:26 时间

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 开关; 

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

show databases;
 
use mydb13_optimize;
 
show tables;
 
select * from user where id < 2;
 
select count(*) from user;

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

show profiles;

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

时间:

show profile for query 8;

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

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

的耗费时间  :

show profile cpu for query 133;  

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

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

的耗费时间  :