MySQL慢查詢日誌的使用

一. 設置方法

使用慢查詢日誌裏捕獲

啓用之前需要先進行一些設置

方法一:全局變量設置

設置慢查詢日誌的日誌文件位置

set global slow_query_log_file = "D:/slow_log/slow_log.log" ;

設置是否對未使用索引的SQL進行記錄

set global log_queries_not_using_indexes = on;

設置只要SQL執行時間超過n秒的就記錄

set global long_query_time = 0.001 ;

此處設置的0.001秒,便於測試,一般情況比這個大

啓用mysql慢查詢日誌

set global slow_query_log = on;

方法二:配置文件設置

修改配置文件my.cnf,在[mysqld]下的下方加入

[mysqld]
slow_query_log = ON
log_queries_not_using_indexes = ON;
slow_query_log_file = /usr/local/mysql/data/slow.log
long_query_time = 1

查看設置後的參數

show variables like 'slow_query%';
show variables like 'long_query__time';

二. 慢查詢日誌記錄的內容

Time                 Id Command    Argument
# Time: 2019-01-08T04:12:09.269315Z 
# User@Host: h5_test[h5_test] @ localhost [::1]  Id:    12  
# Query_time: 0.000831  Lock_time: 0.000198 Rows_sent: 1  Rows_examined: 3  
use mc_productdb;
SET timestamp=1546920729;
SELECT t.customer_id,t.title,t.content 
FROM (
SELECT customer_id  FROM product_comment WHERE  product_id =199726 AND audit_status = 1 LIMIT 0,15
)a JOIN product_comment t 
ON a.customer_id = t.comment_id;

Time:執行查詢的日期時間
User@Host:執行查詢的用戶和客戶端IP
Id:是執行查詢的線程Id
Query_time:SQL執行所消耗的時間
Lock_time:執行查詢對記錄鎖定的時間
Rows_sent:查詢返回的行數
Rows_examined:爲了返回查詢的數據所讀取的行數

三. 如何分析慢查詢日誌

Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]

Parse and summarize the MySQL slow query log. Options are

  --verbose    verbose
  --debug      debug
  --help       write this text to standard output

  -v           verbose
  -d           debug
  -s ORDER     what to sort by (al, at, ar, c, l, r, t), 'at' is default
                al: average lock time
                ar: average rows sent
                at: average query time
                 c: count
                 l: lock time
                 r: rows sent
                 t: query time
  -r           reverse the sort order (largest last instead of first)
  -t NUM       just show the top n queries
  -a           don't abstract all numbers to N and strings to 'S'
  -n NUM       abstract numbers with at least n digits within names
  -g PATTERN   grep: only consider stmts that include this string
  -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),
               default is '*', i.e. match all
  -i NAME      name of server instance (if using mysql.server startup script)
  -l           don't subtract lock time from total time

由於慢查詢日誌中會含有大量的重複的SQL,爲了方便,可以通過mysql提供的命令行工具 mysqldumpslow 來分析日誌

$ mysqldumpslow.pl  slow_log.log

Reading mysql slow query log from slow_log.log
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), 0users@0hosts
  C:\Program Files\MySQL\MySQL Server N.N\bin\mysqld.exe, Version: N.N.N-log (MySQL Community Server (GPL)). started with:
  TCP Port: N, Named Pipe: MySQL
  # Time: N-N-08T04:N:N.269315Z
  # User@Host: h5_test[h5_test] @ localhost [::N]  Id:    N
  # Query_time: N.N  Lock_time: N.N Rows_sent: N  Rows_examined: N
  use mc_productdb;
  SET timestamp=N;
  SELECT t.customer_id,t.title,t.content
  FROM (
  SELECT customer_id  FROM product_comment WHERE  product_id =N AND audit_status = N LIMIT N,N
  )a JOIN product_comment t
  ON a.customer_id = t.comment_id

與慢查詢日誌中記錄的數據是相似的,只是多出了一行Count,這一行記錄的是這條SQL在記錄慢查詢日誌期間的執行次數,如果一個SQL多次被執行,用這個命令分析時,只會出現一個SQL日誌,Count裏的數值代表執行次數,其他數字爲了合併表示用N代替

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章