在项目中,我们可以通过开启mysql慢查询,迅速定位到执行速度慢,从而影响系统性能的sql语句,因此我们就可以优化该sql语句,提高系统性能!
一、首先查看mysql版本
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.17-log |
+------------+
1 row in set (0.00 sec)
二、查看mysql的慢查询配置信息
mysql> show variables like '%slow%';
+---------------------------+--------------------+
| Variable_name | Value |
+---------------------------+--------------------+
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | slow_query_log.log |
+---------------------------+--------------------+
5 rows in set (0.00 sec)
mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
为了下面测试学习,先将慢查询时间设置为0.8s
mysql> set long_query_time = 0.8;
Query OK, 0 rows affected (0.00 sec)
三、在mysql的安装主目录中找到my.ini文件
修改或增加下面的参数:
四、重启mysql服务
五、执行一条sql语句(使其查询时间超过0.8s)
mysql> use temp;
Database changed
mysql> select * from emp where empno = '998885';
+--------+--------+----------+-----+------------+---------+--------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+--------+--------+----------+-----+------------+---------+--------+--------+
| 998885 | UXcuTj | SALESMAN | 1 | 2015-09-13 | 2000.00 | 400.00 | 142 |
+--------+--------+----------+-----+------------+---------+--------+--------+
1 row in set (0.86 sec)
该sql查询时间为0.86s,则可以在上面配置好的慢查询日志文件中看到。
文件目录:D:\ProgramData\MySQL\MySQL Server 5.6\data