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
相關文章
相關標籤/搜索