MySQL慢查詢分析工具之mysqldumpslow
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 |
-
Display a help message and exit.
-
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. -
Consider only queries that match the ( grep-style) pattern.
-
Host name of MySQL server for
*-slow.log
file name. The value can contain a wildcard. The default is*
(match all). -
Name of server instance (if using mysql.server startup script).
-
Do not subtract lock time from total time.
-
Abstract numbers with at least
N
digits within names. -
Reverse the sort order.
-
How to sort the output. The value of
sort_type
should be chosen from the following list:-
t
,at
: Sort by query time or average query time -
l
,al
: Sort by lock time or average lock time -
r
,ar
: Sort by rows sent or average rows sent -
c
: Sort by count
By default, mysqldumpslow sorts by average query time (equivalent to
-s at
). -
-
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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 慢查詢分析調優工具~mysqldumpslowMySql
- Mysql 慢日誌分析工具MysqldumpslowMySql
- 【mysql】explain命令分析慢查詢MySqlAI
- MySQL 慢查詢分析工具~pt-query-digest 詳解MySql
- MySQL慢查詢MySql
- MySQL 慢查詢MySql
- 遇到慢查詢怎麼辦?一文解讀MySQL 8.0查詢分析工具MySql
- 慢查詢分析調優工具~show profile
- mysql之 slow log 慢查詢日誌MySql
- 如何使用 MySQL 慢查詢日誌進行效能優化 - Profiling、mysqldumpslow 例項詳解MySql優化
- mysql慢查詢和錯誤日誌分析MySql
- mongodb慢查詢分析MongoDB
- 關於MySQL 通用查詢日誌和慢查詢日誌分析MySql
- MySQL:慢查詢日誌MySql
- MySQL 慢查詢優化MySql優化
- 對 MySQL 慢查詢日誌的簡單分析MySql
- MySQL information_schema.columns表查詢慢原因分析MySqlORM
- MySQL 查詢效能分析之 ExplainMySqlAI
- MySQL 慢查詢那點事MySql
- [Mysql]慢查詢最佳化MySql
- 詳解MySQL--慢查詢MySql
- Mysql 慢查詢優化實踐MySql優化
- 慢查詢日誌開啟分析
- 資料庫系列:MySQL慢查詢分析和效能最佳化資料庫MySql
- mysqldumpslow工具MySql
- 慢查詢
- mysql查詢效率慢的SQL語句MySql
- mysql慢查詢,死鎖解決方案MySql
- MySQL Slow Query log(慢查詢日誌)MySql
- 《MySQL慢查詢優化》之SQL語句及索引優化MySql優化索引
- mysql伺服器查詢慢原因分析與解決方法小結MySql伺服器
- 在mysql查詢效率慢的SQL語句MySql
- MySQL索引原理及慢查詢最佳化MySql索引
- Mysql慢查詢日誌檔案轉ExcelMySqlExcel
- MySQL慢查詢日誌相關設定MySql
- 深入mysql慢查詢設定的詳解MySql
- mysql5.7.10開啟慢查詢詳解MySql
- 盤點MySQL慢查詢的12個原因MySql