Mysql数据库优化-mysqldumpslow命令

mysql没有那么多性能视图可以查询SQL执行情况,但是也提供mysqldumpslow命令来解析日志来获取数据库SQL语句执行情况,为运维、开发人员提供了获取需要具体优化SQL语句的一个方法。

mysqldumpslow
命令大概形式: slowlog是需要分析的日志
mysqldumpslow -s c -t 10 {$PATH}/slow-log
主要参数:

A: -s, 是sort的意思,表示按照何种方式排序,c、t、l、r分别是按照记录次数、时间、查询时间、返回的记录数来排序,ac、at、al、ar,表示相应的倒序;
B: -t, 是top n的意思,即为返回前面多少条的数据;
C: -g, 是grep的意思,后边可以写一个正则匹配模式,大小写不敏感的。


用法举例:

mysqldumpslow -t 10 slow.log      ---耗时最长的10个SQL

Reading mysql slow query log from slow.log

Count: 2  Time=455.96s (911s)  Lock=0.00s (0s)  Rows=7502.5 (15005), 

  call procedure_loop_insert_with_var(N)

Count: 15  Time=246.77s (3701s)  Lock=0.00s (0s)  Rows=101.4 (1521), 2users@2hosts

  call procedure_loop_insert_with_var1(N)

Count: 3  Time=3.05s (9s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert where id>=N

Count: 1  Time=2.43s (2s)  Lock=0.00s (0s)  Rows=100000.0 (100000), root[root]@localhost

  select fun_loop_insert1(id) from loop_insert where id<=N

Count: 1  Time=2.01s (2s)  Lock=0.00s (0s)  Rows=90000.0 (90000), root[root]@localhost

  select fun_loop_insert1(id) from loop_insert where id>=N and id <N

Count: 2  Time=1.95s (3s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert where id>N

Count: 2  Time=1.92s (3s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert

Count: 2  Time=0.45s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert1

Count: 26  Time=0.33s (8s)  Lock=0.00s (0s)  Rows=1.0 (26), root[root]@localhost

  select count(*) from loop_insert

Count: 1  Time=0.27s (0s)  Lock=0.01s (0s)  Rows=9999.0 (9999), root[root]@localhost

  select fun_loop_insert1(id) from loop_insert where id<N

 mysqldumpslow -s c -t 10 slow.log    -- 执行次数最多的10个sql语句

Reading mysql slow query log from slow.log

Count: 26  Time=0.33s (8s)  Lock=0.00s (0s)  Rows=1.0 (26), root[root]@localhost

  select count(*) from loop_insert

Count: 15  Time=246.77s (3701s)  Lock=0.00s (0s)  Rows=101.4 (1521), 2users@2hosts

  call procedure_loop_insert_with_var1(N)

Count: 3  Time=0.17s (0s)  Lock=0.01s (0s)  Rows=0.0 (0), root[root]@localhost

  truncate table loop_insert

Count: 3  Time=0.13s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

Count: 3  Time=3.05s (9s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert where id>=N

Count: 2  Time=455.96s (911s)  Lock=0.00s (0s)  Rows=7502.5 (15005), zgpdba[zgpdba]@centos01

  call procedure_loop_insert_with_var(N)

Count: 2  Time=1.92s (3s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert

Count: 2  Time=0.45s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert1

Count: 2  Time=1.95s (3s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

  delete from loop_insert where id>N

Count: 1  Time=0.27s (0s)  Lock=0.01s (0s)  Rows=9999.0 (9999), root[root]@localhost

  select fun_loop_insert1(id) from loop_insert where id<N

mysqldumpslow -s r -t 10 slow.log   -- 获取条数最多的10个sql 

Reading mysql slow query log from slow.log

Count: 1  Time=2.43s (2s)  Lock=0.00s (0s)  Rows=100000.0 (100000), root[root]@localhost

  select fun_loop_insert1(id) from loop_insert where id<=N

Count: 1  Time=2.01s (2s)  Lock=0.00s (0s)  Rows=90000.0 (90000), root[root]@localhost

  select fun_loop_insert1(id) from loop_insert where id>=N and id <N

Count: 2  Time=455.96s (911s)  Lock=0.00s (0s)  Rows=7502.5 (15005), zgpdba[zgpdba]@centos01

  call procedure_loop_insert_with_var(N)

Count: 1  Time=0.27s (0s)  Lock=0.01s (0s)  Rows=9999.0 (9999), root[root]@localhost

  select fun_loop_insert1(id) from loop_insert where id<N

Count: 15  Time=246.77s (3701s)  Lock=0.00s (0s)  Rows=101.4 (1521), 2users@2hosts

  call procedure_loop_insert_with_var1(N)

Count: 1  Time=0.26s (0s)  Lock=0.02s (0s)  Rows=329.0 (329), root[root]@localhost

  SELECT table_schema, table_name, data_free/N/N AS data_free_MB FROM information_schema.tables

Count: 26  Time=0.33s (8s)  Lock=0.00s (0s)  Rows=1.0 (26), root[root]@localhost

  select count(*) from loop_insert

Count: 3  Time=0.17s (0s)  Lock=0.01s (0s)  Rows=0.0 (0), root[root]@localhost

  truncate table loop_insert

Count: 3  Time=0.13s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost

Count: 2  Time=1.92s (3s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost
————————————————
版权声明:本文为CSDN博主「小尖一步」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhougongping/article/details/122377129

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`mysqldumpslow` 是一个用于分析 MySQL 慢查询日志的命令行工具。下面是 `mysqldumpslow` 命令的基本使用方式: 1. 打开终端或命令提示符,输入以下命令: ``` mysqldumpslow [options] [log_file] ``` 2. `options` 是一些可选参数,可以根据需要进行配置。常用的选项包括: - `-s`:指定排序方式,可以是 `t` (按时间排序)、`l` (按锁定时间排序)、`r` (按返回结果排序)、`c` (按查询计数排序)、`a` (按平均查询时间排序)。 - `-t`:指定返回查询结果的数量。 - `-g`:指定匹配查询语句的正则表达式。 - `-r`:反向排序。 3. `log_file` 是慢查询日志文件的路径。如果不指定该参数,则默认使用 MySQL 配置文件中指定的慢查询日志路径。 以下是几个示例: - 分析默认慢查询日志文件,并按照执行时间排序: ``` mysqldumpslow -s t ``` - 分析指定慢查询日志文件,并按照平均查询时间排序,返回前10条结果: ``` mysqldumpslow -s a -t 10 /path/to/slow-query.log ``` - 分析慢查询日志文件,匹配包含特定关键字的查询语句,并按照执行时间反向排序: ``` mysqldumpslow -s t -g 'SELECT.*FROM table_name' -r ``` 注意:`mysqldumpslow` 命令需要有读取慢查询日志文件的权限。另外,确保已经启用了慢查询日志功能,并且相关日志已经记录下来。 希望这个回答能够帮到你!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值