mysqldumpslow使用總結

mysqldumpslow使用總結

緣起: 實際生產環境中MySQL出現查詢慢的問題,爲了分析慢查詢,開啓慢查詢日誌,並對日誌進行分析。
爲了避免在生成環境中產生誤操作,把記錄的慢查詢日誌copy回來,到自己的電腦中進行分析。
分析結果:

[root@dras-test local]#mysqldumpslow -a -s t -t 2 /opt/slow_query_log.txt 

Reading mysql slow query log from /opt/slow_query_log.txt
Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), LibSvr[LibSvr]@[10.1.202.57]
  # Schema: information_schema  Last_errno: 0  Killed: 0
  # Query_time: 11.257168  Lock_time: 0.000141  Rows_sent: 366777  Rows_examined: 366777  Rows_affected: 0
  # Bytes_sent: 43251512
  SET timestamp=1492111317;
  SELECT * FROM `INNODB_BUFFER_PAGE_LRU`

Count: 1  Time=0.00s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), LibSvr[LibSvr]@[10.1.122.132]
  # Schema: el  Last_errno: 0  Killed: 0
  # Query_time: 4.471143  Lock_time: 0.000097  Rows_sent: 1  Rows_examined: 8018065  Rows_affected: 0
  # Bytes_sent: 1098
  SET timestamp=1490682921;
  SELECT `UserID`,`FileName`,`AdjunctGuid`,`LiteratureGuid`,`NoteGuid`,`AdjunctType`,`Md5`,`ID`,`ServerModifyTime`,`FileSize` FROM `el_user_adjunct_info` WHERE (`AdjunctGuid` = '4528cef4139846678cddf0d00170af9f.caj')

問題出現了,仔細看的同學已經看出了端倪,Time、Lock、Rows統計數據都爲0。

mysqldumpslow是一個perl腳本,其中使用正則表達式對log文件進行逐行匹配,匹配完成後提取出匹配的值然後替換整行爲空。

s/^# Query_time: ([0-9.]+)\s+Lock_time: ([0-9.]+)\s+Rows_sent: ([0-9.]+).*\n//;

經單獨測試,此表達式沒問題,可以正確匹配。但是在mysqldumpslow中卻未匹配成功,經過一番查找、對比,發現從生產環境中copy下來的日誌格式和mysqldumpslow中要解析的格式不一致,日誌中多了一行

   18 # User@Host: LibSvr[LibSvr] @  [10.1.122.131]  Id:    10
   19 # Schema: el  Last_errno: 0  Killed: 0
   20 # Query_time: 5.993656  Lock_time: 0.000078  Rows_sent: 1  Rows_examined: 8018014  Rows_affected: 0
   21 # Bytes_sent: 1086
   22 SET timestamp=1490682881;
   23 SELECT `UserID`,`FileName`,`AdjunctGuid`,`LiteratureGuid`,`NoteGuid`,`AdjunctType`,`Md5`,`ID`,`ServerModifyTime`,`FileSize` FROM `el_user_adjunct_info` WHERE (`AdjunctGuid` = 'dbf1fc940ddd452d8d2af439438a      cb07.caj');

其中,第19行是多出來的,mysqldumpslow爲匹配和替換成空,因此逐行匹配時就匹配錯了行。
於是修改腳本,添加對第19行的匹配和替換:

105     s/^#? Schema: \w+  Last_errno: \d+  Killed: \d+.*\n//;
106 
107     s/^# Query_time: ([0-9.]+)\s+Lock_time: ([0-9.]+)\s+Rows_sent: ([0-9.]+).*\n//;

在匹配Query_time:行的表達式上邊,添加第105行匹配Schema行並替換成空。

然後再進行分析,終於正常了。
問題原因:生成環境和我本地的MySQL版本不一致,其生成的日誌格式有差異。

mysqldumpslow語法很簡單:

[root@dras-test local]# mysqldumpslow --help
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

示例:

mysqldumpslow -s r -t 10 /database/mysql/slow-log.txt
得到返回記錄集最多的10個查詢。

mysqldumpslow -s t -t 10 -g “left join” /database/mysql/slow-log.txt
得到按照時間排序的前10條裏面含有左連接的查詢語句。

mysqldumpslow -a -s t -t 2 /opt/slow_query_log.txt
-a 參數,說明不合並類似的SQL語句,顯示具體的SQL語句中的數字和字符串。

正確的結果如下:

[root@dras-test local]# mysqldumpslow -a -s t -t 2 /opt/slow_query_log.txt 

Reading mysql slow query log from /opt/slow_query_log.txt
Count: 60  Time=903.45s (54206s)  Lock=0.00s (0s)  Rows=13271072.8 (796264367), LibSvr[LibSvr]@2hosts
  SELECT * FROM `el_user_litera_info`

Count: 60  Time=335.23s (20113s)  Lock=0.00s (0s)  Rows=17128739.1 (1027724344), LibSvr[LibSvr]@2hosts
  SELECT * FROM `el_user_category_litera`

[root@dras-test local]# mysqldumpslow -a -s t -t 5 /opt/slow_query_log.txt 

Reading mysql slow query log from /opt/slow_query_log.txt
Count: 60  Time=903.45s (54206s)  Lock=0.00s (0s)  Rows=13271072.8 (796264367), LibSvr[LibSvr]@2hosts
  SELECT * FROM `el_user_litera_info`

Count: 60  Time=335.23s (20113s)  Lock=0.00s (0s)  Rows=17128739.1 (1027724344), LibSvr[LibSvr]@2hosts
  SELECT * FROM `el_user_category_litera`

Count: 60  Time=277.45s (16646s)  Lock=0.00s (0s)  Rows=13271097.1 (796265825), LibSvr[LibSvr]@2hosts
  SELECT * FROM `el_user_litera_reader_info`

Count: 60  Time=153.27s (9196s)  Lock=0.00s (0s)  Rows=8943019.9 (536581195), LibSvr[LibSvr]@2hosts
  SELECT * FROM `el_user_adjunct_info`

Count: 60  Time=91.95s (5516s)  Lock=0.00s (0s)  Rows=2036609.1 (122196547), LibSvr[LibSvr]@2hosts
  SELECT * FROM `el_user_note_content`
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章