Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

发布时间:2022-06-28 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Mysql中如何查看Sql语句的执行时间

一、初始SQL准备

  • 初始化表
-- 用户表
create table t_users(
                        id int primary key auto_increment,
-- 用户名
                        username varchar(20),
-- 密码
                        password varchar(20),
-- 真实姓名
                        real_name varchar(50),
-- 性别 1表示男  0表示女
                        sex int,
-- 出生年月日
                        birth date,
-- 手机号
                        mobile varchar(11),
-- 上传后的头像路径
                        head_pic varchar(200)
);
  • 初始化数据
--添加用户数据
insert into t_users values(null,'whj','123456','王恒杰',1,NOW(),'12345678901','boy.jpg');
insert into t_users values(null,'dzw','123456','邓正武',1,NOW(),'12345678901','boy.jpg');
insert into t_users values(null,'yfj','123456','杨福君',1,NOW(),'12345678901','girl.jpg');
insert into t_users values(null,'zx','123456','张西',1,NOW(),'12345678901','girl.jpg');
insert into t_users values(null,'zxj','123456','周宣君',0,NOW(),'12345678901','boy.jpg');
insert into t_users values(null,'lfk','123456','刘福昆',1,NOW(),'12345678901','boy.jpg');
  • 表结构

    Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

  • 相关数据

Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

二、Mysql查看Sql语句的执行时间

1、show profiles;

最开始输入show profiles.此时没有数据

Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

2、show variables;

  • show varables:查看profiling是否开启,即Value为ON

Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

直接使用show variables命令会将所有变量展示出来,太多了,我们不方便查看

就可以使用模糊查询了,用like将profiling挑选出来

一般没有开启都为off

 show variables like 'profiling';

Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

3、set profilling=1,开启profiling

show variables like 'profiling';

Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

这样我们的Mysql就可以查看Sql语句的执行时间

三、不同查询的执行时间

select * from t_users;     
select id,username,password,real_name,sex,birth,mobile,head_pic from t_users;
 
select * from t_users where username like 'whj';

Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

  • 三种查询的时间对比

Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)

结论:

sql 在使用SELECT语句或者使用其它语句时,直接使用*和把所有的字段弄上去查询,性能有很大差距,所以我们平时写查询最好用字段写上

脚本宝典总结

以上是脚本宝典为你收集整理的Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)全部内容,希望文章能够帮你解决Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: