开启MYSQL慢查询日志,监控优化SQL效率
一、安装登录MYSQL
1、下载地址:MYSQL官网(直接next安装即可)
2、安装完成后点击如下图所示输入密码进行登录操作

二、查看是否开启了慢查询日志
输入命令【show variables like 'slow_query_log'】,页面会显示是否开启了慢查询日志,slow_query_log =ON,代表已经开启。如果未开启,则需要执行该命令set global slow_query_log=on;

三、开启索引的监控
通过设置log_queries_not_using_indexes开启为使用索引的监控,输入命令show variables like 'log_queries_not_using_indexes';log_queries_not_using_indexes=OFF,代表未开启,需要执行该命令set global log_queries_not_using_indexes=on;

四、设置监控阀值
通过long_query_time设置监控阀值,也就是超过多少秒就记录,单位是秒,此处设置为0.1也就是100毫秒。输入命令show variables like 'long_query_time';系统设置的为1秒,如果需要修改为0.1秒,则需要执行如下命令set global long_query_time=0.1;

五、设置set global为全局生效
1.set global 只是全局session生效,重启后失效,如果需要以上配置永久生效,需要做如下配置
MySQL在Windows系统中的配置文件一般是是my.ini找到[mysqld]下面加上
log-slow-queries = F:\MySQL\log\mysqlslowquery.log
long_query_time = 2
2.执行命令show variables like 'slow_query_log_file' \G

3.显示的日志记录在/apps/logs/mysql/slow3306.log目录
4.测试结果

5.查询日志记录
