MySQL慢查詢分析工具之mysqldumpslow

lhrbest發表於2020-04-29


mysqldumpslow工具


https://dev.mysql.com/doc/refman/5.7/en/mysqldumpslow.html

mysqldumpslow -s at -t 5 mysql_slowlog.log
perl "D:\Program Files\MySQL\mysql-8.0.15-winx64\bin\mysqldumpslow.pl" -s at -t 5 C:\Users\chinasoft_lhrxxt\Desktop\mysql_slowlog.log




開啟慢查詢

檢視慢查詢相關引數

slow_query_log:慢查詢是否開啟

slow_query_log_file:慢查詢檔案位置

long_query_time:超過多長時間記錄

log_queries_not_using_indexes:沒有使用索引的sql

mysql> show variables like 'slow_query%';

+---------------------+----------------------------+

| Variable_name       | Value                      |

+---------------------+----------------------------+

| slow_query_log      | ON                         |

| slow_query_log_file | /var/lib/mysql/db-slow.log |

+---------------------+----------------------------+

2 rows in set (0.01 sec)

mysql> show variables like 'long_query_time';     

+-----------------+----------+

| Variable_name   | Value    |

+-----------------+----------+

| long_query_time | 2.000000 |

+-----------------+----------+

1 row in set (0.00 sec)

mysql> show variables like 'log_queries_not%';

+-------------------------------+-------+

| Variable_name                 | Value |

+-------------------------------+-------+

| log_queries_not_using_indexes | OFF   |

+-------------------------------+-------+

1 row in set (0.00 sec)

設定方法

方法1:全域性變數設定

mysql> set global slow_query_log='ON'; 

mysql> set global slow_query_log_file='/var/lib/mysql/db-slow.log';

mysql> set global long_query_time=2;

mysql> set global log_queries_not_using_indexes=1;

方法2:配置檔案設定

修改配置檔案my.cnf在[mysqld]下加入如下內容

slow_query_log = ON

slow_query_log_file = /var/lib/mysql/db-slow.log 

long_query_time = 2

log_queries_not_using_indexes=1

修改後重啟mysql服務

測試

mysql> select sleep(3); 

[root@db ~]# tail -f /var/lib/mysql/db-slow.log

# Time: 2018-12-25T08:00:48.199670Z

# User@Host: root[root] @ localhost []  Id: 2443797

# Query_time: 3.000198  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

SET timestamp=1545724848;

select sleep(3);

[root@db ~]# 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

常用引數

-s 排序方式 後面跟排序列 

al 平均鎖定時間

ar 平均返回記錄時間

at 平均查詢時間(預設)

c  計數

l 鎖定時間

r 返回記錄

t 查詢時間

-r 反向排序,最大的在最後

-t 後面跟數字,返回行數

-a 不將所有的數字抽象為N,字串抽象為S

-n 後面跟數字,在名稱中至少有n個數字抽象為數字

-g 正則後邊可以寫一個正則匹配模式,大小寫不敏感的

案例:

1.根據計數返回5條記錄

mysqldumpslow -s c -t 5 /var/lib/mysql/db-slow.log

2.根據返回記錄返回5條記錄

mysqldumpslow -s r -t 5 /var/lib/mysql/db-slow.log

3.根據查詢時間返回5條記錄

mysqldumpslow -s t -t 5 /var/lib/mysql/db-slow.log

4.根據計數統計,不抽象數字及字串

mysqldumpslow -s c -a -t 5 /var/lib/mysql/db-slow.log


得到返回記錄最多的20個sql

mysqldumpslow -s r -t 20 sqlslow.log


得到平均訪問次數最多的20條sql

mysqldumpslow -s ar -t 20 sqlslow.log


得到平均訪問次數最多,並且裡面含有ttt字元的20條sql

mysqldumpslow -s ar -t 20 -g "ttt" sqldlow.log


注: 

1、如果出現 -bash: mysqldumpslow: command not found 錯誤,請執行

ln -s /usr/local/mysql/bin/mysqldumpslow /usr/bin


2、如果出現如下錯誤,Died at /usr/bin/mysqldumpslow line 161, <> chunk 405659.說明你要分析的sql日誌太大了,請拆分後再分析

拆分的命令為:

tail -100000 mysql-slow.log>mysql-slow.20180725.log

-- 取出執行次數最多的前5條SQL

[root@localhost log]# /software/bin/mysqldumpslow -s c -t 5 slow_query.log |more

Reading mysql slow query log from slow_query.log
Count: 2  Time=43.54s (87s)  Lock=0.00s (0s)  Rows=0.0 (0), system[system]@localhost
  lock table t10 write

Count: 1  Time=14.53s (14s)  Lock=0.00s (0s)  Rows=73088.0 (73088), system[system]@localhost
  SELECT /*!N SQL_NO_CACHE */ * FROM `t_idb_big`

Count: 1  Time=12.22s (12s)  Lock=0.00s (0s)  Rows=0.0 (0), system[system]@localhost
  INSERT INTO `t_idb_big` VALUES ('S','S','S','S',N,'S','S','S',NULL,NULL,N,N,NULL,NULL,'S','S','S','S','S',N)

--取出耗時最長的前10條慢SQL

mysqldumpslow -s t -t 10 slow.log




https://dev.mysql.com/doc/refman/5.7/en/mysqldumpslow.html


The MySQL slow query log contains information about queries that take a long time to execute (see  Section 5.4.5, “The Slow Query Log”). mysqldumpslow parses MySQL slow query log files and summarizes their contents.

Normally,  mysqldumpslow groups queries that are similar except for the particular values of number and string data values. It  abstracts these values to  N and  'S' when displaying summary output. To modify value abstracting behavior, use the  -a and  -n options.

Invoke  mysqldumpslow like this:

shell> mysqldumpslow [options] [log_file ...]

Example of usage:

shell> mysqldumpslowReading mysql slow query log from /usr/local/mysql/data/mysqld57-slow.log
Count: 1  Time=4.32s (4s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 insert into t2 select * from t1
Count: 3  Time=2.53s (7s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 insert into t2 select * from t1 limit N
Count: 3  Time=2.13s (6s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
 insert into t1 select * from t1

mysqldumpslow supports the following options.

Table 4.23 mysqldumpslow Options

Option Name Description
-a Do not abstract all numbers to N and strings to 'S'
-n Abstract numbers with at least the specified digits
--debug Write debugging information
-g Only consider statements that match the pattern
--help Display help message and exit
-h Host name of the server in the log file name
-i Name of the server instance
-l Do not subtract lock time from total time
-r Reverse the sort order
-s How to sort output
-t Display only first num queries
--verbose Verbose mode

  • --help

    Display a help message and exit.

  • -a

    Do not abstract all numbers to  N and strings to  'S'.

  • --debug-d

    Run in debug mode.

    This option is available only if MySQL was built using  WITH_DEBUG. MySQL release binaries provided by Oracle are  not built using this option.

  • -g  pattern

    Consider only queries that match the ( grep-style) pattern.

  • -h  host_name

    Host name of MySQL server for  *-slow.log file name. The value can contain a wildcard. The default is  * (match all).

  • -i  name

    Name of server instance (if using  mysql.server startup script).

  • -l

    Do not subtract lock time from total time.

  • -n  N

    Abstract numbers with at least  N digits within names.

  • -r

    Reverse the sort order.

  • -s  sort_type

    How to sort the output. The value of  sort_type should be chosen from the following list:

    • tat: Sort by query time or average query time

    • lal: Sort by lock time or average lock time

    • rar: Sort by rows sent or average rows sent

    • c: Sort by count

    By default,  mysqldumpslow sorts by average query time (equivalent to  -s at).

  • -t  N

    Display only the first  N queries in the output.

  • --verbose-v

    Verbose mode. Print more information about what the program does.







About Me

........................................................................................................................

● 本文作者:小麥苗,部分內容整理自網路,若有侵權請聯絡小麥苗刪除

● 本文在itpub、部落格園、CSDN和個人微 信公眾號( DB寶)上有同步更新

● 本文itpub地址: http://blog.itpub.net/26736162

● 本文部落格園地址: http://www.cnblogs.com/lhrbest

● 本文CSDN地址: https://blog.csdn.net/lihuarongaini

● 本文pdf版、個人簡介及小麥苗雲盤地址: http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答: http://blog.itpub.net/26736162/viewspace-2134706/

● DBA寶典今日頭條號地址: http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

........................................................................................................................

● QQ群號: 230161599 、618766405

● 微 信群:可加我微 信,我拉大家進群,非誠勿擾

● 聯絡我請加QQ好友 646634621 ,註明新增緣由

● 於 2020-04-01 06:00 ~ 2020-04-30 24:00 在西安完成

● 最新修改時間:2020-04-01 06:00 ~ 2020-04-30 24:00

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

........................................................................................................................

小麥苗的微店https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

小麥苗出版的資料庫類叢書http://blog.itpub.net/26736162/viewspace-2142121/

小麥苗OCP、OCM、高可用網路班http://blog.itpub.net/26736162/viewspace-2148098/

小麥苗騰訊課堂主頁https://lhr.ke.qq.com/

........................................................................................................................

使用 微 信客戶端掃描下面的二維碼來關注小麥苗的微 信公眾號( DB寶)及QQ群(DBA寶典)、新增小麥苗微 信, 學習最實用的資料庫技術。

........................................................................................................................

歡迎與我聯絡

 

 



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2689188/,如需轉載,請註明出處,否則將追究法律責任。

相關文章