现场数据库cpu使用率过高,定位问题需要分析历史的慢sql
由于没有安装pt-digest-query工具(不通外网有依赖装不了)就用mysql自带的mysqldumpslow分析
[root@localhost home]# /usr/local/mysql/bin/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
由于没有制定时间段分析的参数,要分析某一时间段的日志,就只能先截断日志,然后再分析了
截取一天的slow log
awk '/# Time: 2021-02-04T08/,/# Time: 2021-02-04T20/' slow.log > slow202010204.log
返回查询时间最多20条语句
/usr/local/mysql/bin/mysqldumpslow -t 20 -s t slow202010204.log
返回检索记录数最多20条语句
/usr/local/mysql/bin/mysqldumpslow -t 20 -s r slow202010204.log
返回按查询时间所有的语句
/usr/local/mysql/bin/mysqldumpslow -s t slow202010204.log
把分析的日志重定向文件中
/usr/local/mysql/bin/mysqldumpslow -s t slow202010204.log > slowlog20200204.sql
接下来就可以分析相应的慢sql了
另外还可以通过sys库的视图查询(mysql自启动以来统计的sql语句)
查询按检索数据最多的全表扫描前20的sql语句:
SELECT
QUERY,
db,
exec_count,
total_latency,
rows_examined
FROM
sys.x$statements_with_full_table_scans
ORDER BY
rows_examined DESC
LIMIT 20
查询按排序做多的20条语句:
SELECT
QUERY,
db,
exec_count,
total_latency,
rows_sorted
FROM
x$statements_with_sorting
ORDER BY
rows_sorted DESC
LIMIT 20
查询按总延迟做多的20条语句:
SELECT
QUERY,
db,
exec_count,
total_latency,
rows_examined
FROM
x$statement_analysis
ORDER BY
total_latency DESC
LIMIT 20
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。