MySQL日志之慢查询日志(slow-log)


mysql> show status like 'slow_queries';    #慢查询数
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries  | 0     |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show global variables like 'slow%';   #全局 慢查询变量
+---------------------+------------------------------------------+
| Variable_name       | Value                                    |
+---------------------+------------------------------------------+
| slow_launch_time    | 2    # 表示如果建立线程花费了比这个值更长的时间,slow_launch_threads 计数器将增加                                    |
| slow_query_log      | OFF                                      |
| slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
+---------------------+------------------------------------------+
3 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)
mysql> set global long_query_time=0.01;     #设置慢查询时间为1   global下次连接生效
Query OK, 0 rows affected (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)


mysql> set long_query_time=0.01;           #当前生效
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 0.010000 |
+-----------------+----------+
1 row in set (0.00 sec)

 开启慢查询

mysql> set slow_query_log=on;
ERROR 1229 (HY000): Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL
mysql> set global slow_query_log=on;
Query OK, 0 rows affected (0.04 sec)

是否记录未使用索引的SQL

mysql> show global variables like '%log_queries_not_using_indexes%'; 
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF   |
+-------------------------------+-------+
1 row in set (0.00 sec)

mysql> set log_queries_not_using_indexes=on;
ERROR 1229 (HY000): Variable 'log_queries_not_using_indexes' is a GLOBAL variable and should be set with SET GLOBAL
mysql> set global log_queries_not_using_indexes=on;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like '%log_queries_not_using_indexes%'; 
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON    |
+-------------------------------+-------+
1 row in set (0.00 sec)

再次连接

mysql> show global variables like 'slow%';
+---------------------+------------------------------------------+
| Variable_name       | Value                                    |
+---------------------+------------------------------------------+
| slow_launch_time    | 2                                        |
| slow_query_log      | ON                                       |
| slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
+---------------------+------------------------------------------+
3 rows in set (0.00 sec)

慢查询不是只有查询数据才算,只要是操作数据库超过时间显限制都算

[root@localhost data]# /usr/local/mysql/bin/mysqldumpslow localhost-slow.log 

Reading mysql slow query log from localhost-slow.log
Count: 4  Time=9.49s (37s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@2hosts
  INSERT INTO curriculum SELECT * FROM curriculum

Count: 1  Time=5.39s (5s)  Lock=0.00s (0s)  Rows=27136.0 (27136), root[root]@[10.16.134.131]
  select * FROM curriculum where curr_id<>N

[root@localhost data]# /usr/local/mysql/bin/mysqldumpslow localhost-slow.log 

Reading mysql slow query log from localhost-slow.log
Count: 4  Time=9.49s (37s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@2hosts
  INSERT INTO curriculum SELECT * FROM curriculum

Count: 1  Time=5.39s (5s)  Lock=0.00s (0s)  Rows=27136.0 (27136), root[root]@[10.16.134.131]
  select * FROM curriculum where curr_id<>N

[root@localhost data]# /usr/local/mysql/bin/mysqldumpslow localhost-slow.log 

Reading mysql slow query log from localhost-slow.log
Count: 4  Time=9.49s (37s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@2hosts
  INSERT INTO curriculum SELECT * FROM curriculum

Count: 1  Time=5.39s (5s)  Lock=0.00s (0s)  Rows=27136.0 (27136), root[root]@[10.16.134.131]
  select * FROM curriculum where curr_id<>N

Count: 1  Time=0.06s (0s)  Lock=0.00s (0s)  Rows=3.0 (3), root[root]@localhost
  select * from sakila.payment_copy LIMIT N,N

Count: 1  Time=0.05s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
  set global slow_query_log=on

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM