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
配置文件
>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/
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)
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;
添加一条数据
INSERT INTO `user2`(`name`, `age`) VALUES ('测试0', 12); #第一次添加数据
重复添加,直到数据达到百万级以上
insert into user2(name,age) select name,age from user2;
>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;
>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 排序方式
-t 是top n的意思,返回多少条数据。
-g 可以跟上正则匹配模式,大小写不敏感。
>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为查询次数
>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)
>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