mysql> show variables like 'slow_query%'; +---------------------+------------------------------------------------------+ | Variable_name | Value | +---------------------+------------------------------------------------------+ | slow_query_log | OFF | | slow_query_log_file | E:\mysql-5.7.15-winx64\data\DESKTOP-B76J065-slow.log | +---------------------+------------------------------------------------------+ 2 rows in set, 1 warning (0.00 sec)
-- 开启慢查询日志 mysql> set global slow_query_log=1; Query OK, 0 rows affected (0.01 sec)
-- 设置慢查询日志路径 mysql> set global slow_query_log_file='E:\\slow_query.log'; Query OK, 0 rows affected (0.01 sec)
mysql> show variables like 'slow_query%'; +---------------------+-------------------+ | Variable_name | Value | +---------------------+-------------------+ | slow_query_log | ON | | slow_query_log_file | E:\slow_query.log | +---------------------+-------------------+ 2 rows in set, 1 warning (0.00 sec)
注意:使用set global slow_query_log=1开启慢查询日志只对当前数据库生效,MySQL重启后则会失效。如果要永久生效,就必须修改配置文件my.cnf(windows下为my.ini),或者在使用命令行启动mysql的时候,在--slow-query-log参数中指定,其它系统变量也是如此。
mysql> show variables like 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set, 1 warning (0.01 sec)
mysql> set global long_query_time=1; Query OK, 0 rows affected (0.00 sec)
-- 注意show variables查看的是当前会话的变量值 mysql> show variables like 'long_query_time'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set, 1 warning (0.00 sec)
mysql> show global variables like 'long_query_time'; +-----------------+----------+ | Variable_name | Value | +-----------------+----------+ | long_query_time | 1.000000 | +-----------------+----------+ 1 row in set, 1 warning (0.00 sec)
注意:使用show variables like 'long_query_time'查看是当前连接会话的变量值。因为long_query_time变量在GLOBAL和SESSION中都有,如果不加访问域则使用默认的SESSION访问域,所以要想看到全局的long_query_time变量需要用如下语句show global variables like 'long_query_time'。
show variables完整语法为:show [global|session] variables [like ‘pattern‘ | where expr]
mysql> show variables like 'log_slow_admin_statements'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | log_slow_admin_statements | OFF | +---------------------------+-------+ 1 row in set (0.00 sec)
mysql> show global status like 'slow_queries'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Slow_queries | 0 | +---------------+-------+ 1 row in set (0.00 sec)
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 是表示按照何种方式排序 -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 top n的意思,即为返回前面多少条的数据; -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 后边可以写一个正则匹配模式,大小写不敏感的 -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 --help
使用示例:
1 2 3 4 5 6 7 8 9 10 11
## 得到返回记录集最多的10个SQL。 mysqldumpslow -s r -t 10 /database/mysql/mysql06_slow.log ## 得到访问次数最多的10个SQL mysqldumpslow -s c -t 10 /database/mysql/mysql06_slow.log ## 得到按照时间排序的前10条里面含有左连接的查询语句。 mysqldumpslow -s t -t 10 -g “left join” /database/mysql/mysql06_slow.log ## 另外建议在使用这些命令时结合 | 和more 使用 ,否则有可能出现刷屏的情况。 mysqldumpslow -s r -t 20 /mysqldata/mysql/mysql06-slow.log | more