慢sql監控

1、開啓慢sql日誌

1.1 windows

window的mysql配置,編輯C:\ProgramData\MySQL\MySQL Server 5.7\my.ini,添加如下

#是否開啓慢查詢日誌,1表示開啓,0表示關閉
slow_query_log = 1
#慢查詢日誌存儲路徑
slow_query_log_file = D:/mysql/log/mysqlslowquery.log
#慢查詢閾值,超過1秒,記錄日誌
long_query_time = 1

重啓mysql

1.2 linux

配置文件

>vim /etc/my.cnf

#是否開啓慢查詢日誌,1表示開啓,0表示關閉
slow_query_log = 1
#慢查詢日誌存儲路徑
slow_query_log_file = /opt/mysql/mysqlslowquery.log
#慢查詢閾值,超過1秒,記錄日誌
long_query_time = 1

因爲mysql的linux用戶爲mysql,需要將權限授權給mysql

>chown -R mysql:mysql mysql/

1.3 查看是否開啓成功

mysql> show variables like "%slow_query_log%";
+---------------------+-------------------------------+
| Variable_name       | Value                         |
+---------------------+-------------------------------+
| slow_query_log      | ON                            |
| slow_query_log_file | /opt/mysql/mysqlslowquery.log |
+---------------------+-------------------------------+
2 rows in set (0.24 sec)

2、示例

2.1 創建表

CREATE TABLE `user2` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  `name` varchar(255) NOT NULL COMMENT '姓名',
  `age` int(11) NOT NULL COMMENT '年齡',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

2.2 添加數據

添加一條數據

INSERT INTO `user2`(`name`, `age`) VALUES ('測試0', 12); #第一次添加數據

重複添加,直到數據達到百萬級以上

insert into user2(name,age) select name,age from user2;

2.3 生成慢sql日誌

>select count(*) from user;

消耗時間超過1秒,日誌記錄

# Time: 2020-05-13T12:04:34.134555Z
# User@Host: root[root] @ localhost [::1]  Id:     3
# Query_time: 1.213271  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 5242880
SET timestamp=1589371474;
select count(*) from user;

慢sql

>update user2 set `name`=CONCAT(`name`,id);
>select * from user2 where name="測試10";
>select * from user2 where age=16;

 

 

3、日誌分析工具mysqldumpslow

3.1 命令

>mysqldumpslow -h
Option h requires an argument
ERROR: bad option

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 排序方式

  • c:訪問計數
  • l:鎖定時間
  • r:返回記錄
  • al:平均鎖定時間
  • ar:平均訪問記錄數
  • at:平均查詢時間

-t 是top n的意思,返回多少條數據。

-g 可以跟上正則匹配模式,大小寫不敏感。

3.2 示例

3.2.1 查詢返回記錄(返回數據條數)最多的3個sql

>mysqldumpslow -s r -t 3 /opt/mysql/mysqlslowquery.log

Reading mysql slow query log from /opt/mysql/mysqlslowquery.log
Count: 5  Time=2.01s (10s)  Lock=0.00s (0s)  Rows=2.0 (10), root[root]@[111.200.40.10]
  select * from user2 where name="S"

Count: 8  Time=1.20s (9s)  Lock=0.00s (0s)  Rows=1.0 (8), root[root]@[111.200.40.10]
  select count(*) from user2

Count: 2  Time=129.66s (259s)  Lock=21.64s (43s)  Rows=0.0 (0), root[root]@[111.200.40.10]
  update user2 set `name`=CONCAT(`name`,id)

count爲查詢次數

3.2.2 查詢平均訪問次數最多的3條sql

>mysqldumpslow -s ar -t 3 /opt/mysql/mysqlslowquery.log

Reading mysql slow query log from /opt/mysql/mysqlslowquery.log
Count: 6  Time=2.01s (12s)  Lock=0.00s (0s)  Rows=2.7 (16), root[root]@[111.200.40.10]
  select * from user2 where name="S"

Count: 8  Time=1.20s (9s)  Lock=0.00s (0s)  Rows=1.0 (8), root[root]@[111.200.40.10]
  select count(*) from user2

Count: 2  Time=129.66s (259s)  Lock=21.64s (43s)  Rows=0.0 (0), root[root]@[111.200.40.10]
  update user2 set `name`=CONCAT(`name`,id)

3.2.3 查詢平均訪問次數最多,並且裏面含有name字符的3條sql

>mysqldumpslow -s ar -t 3 -g 'name' /opt/mysql/mysqlslowquery.log

Reading mysql slow query log from /opt/mysql/mysqlslowquery.log
Count: 6  Time=2.01s (12s)  Lock=0.00s (0s)  Rows=2.7 (16), root[root]@[111.200.40.10]
  select * from user2 where name="S"

Count: 2  Time=129.66s (259s)  Lock=21.64s (43s)  Rows=0.0 (0), root[root]@[111.200.40.10]
  update user2 set `name`=CONCAT(`name`,id)

Count: 1  Time=17.12s (17s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@[111.200.40.10]
  insert into user2(name,age) select name,age from user2

 

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