mysql 慢查询日志分析


mysql慢查询:


慢查询相关的变量

slow_query_log:该参数控制着慢查询的状态, 1表示开启状态 ,0 表示关闭状态

slow_query_log_file:慢查询日志路径

long_query_time:最大查询阀值,查询的时间超过这个值就视为慢查询并且将其记录到慢查询日志中,慢查询日志路径

通过slow_query_log_file 这个变量设置

log_queries_not_using_indexes:没有使用到索引的查询语句是否记录到慢查询日志中。

log_slow_slave_statements:

log_slow_admin_statements:

与慢查询相关的状态变量:

Slow_queries:慢查询日志文件中查询语句的条数


慢查询日志格式:

# Time: 140919  4:34:28  sql语句执行的时间

# User@Host: root[root] @  [192.168.57.108]  Id: 19260444 用户名 和服务器Ip 执行线程Id 

# Query_time: 9.556629  Lock_time: 0.000035 Rows_sent: 2606718  Rows_examined: 2606718 

 查询时间                等待锁的时间            查询到的记录数       索引扫描或表扫描数

SET timestamp=1411072468;   #  写入慢查询日志的时间 

SELECT /*!40001 SQL_NO_CACHE */ * FROM `cm_players`; #  sql 语句


慢查询日志分析:

系统自带的日志分析工具: mysqldumpslow 

[root@ldap mysql]# 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



常用的参数有-s  ,-t  , -g  

-s: 表示排序.可以根据sql的执行次数 c,总的耗时 t,总的锁等待时间 l ,总的返回行数 r ,

默认为降序(c,t,l,r),前面加个一个a就表示升序排序(ac,at,al,ar)

-t:显示前几行  

-g:类似于模糊查询


查找包含left  join 的sql并且按照总的执行次数排序显示前2条sql语句

 [root@ldap mysql]# mysqldumpslow  -s c  -t  2  -g left join   ldap-slow.log


Reading mysql slow query log from join ldap-slow.log

Can‘t open join: No such file or directory at /usr/bin/mysqldumpslow line 91.

Count: 11294  Time=4.51s (50925s)  Lock=0.15s (1659s)  Rows=11.1 (125610), EventDev[EventDev]@2hosts

  select A.*,AES_DECRYPT(B.playerMobile ,"S") as playerMobile,B.submitBy,B.gameAccount,AES_DECRYPT(B.playerRealName,"S") as playerRealName,B.isVIP,B.gameId,B.gameAreaId,B.gameServerId from cm_sms_send_log A left join cm_events B using(eventId) where A.IsSend=N and A.SmsType !=N order by A.SendId limit N


Count: 858  Time=14.66s (12579s)  Lock=0.00s (0s)  Rows=26.9 (23072), EventDev[EventDev]@[192.168.119.45]

  select a.*,b.cateName,c.tagName,

  (select count(*) from cm_event_reasons aa where aa.isDelete=N and aa.ccId like concat(a.ccId,‘S‘)) as subCnt  

  from cm_event_reasons a left join cm_event_cates b on a.cateId=b.cateId 

  left join cm_event_tags c on a.tagId=c.tagId 

  where a.isDelete=N  and  N  order by reasonId  limit N,N

 


参考链接:

http://www.ccvita.com/410.html

http://blog.sina.com.cn/s/blog_53b13d950100vmc5.html

多种慢查询分析工具:

http://blog.csdn.net/jkh753/article/details/11590273



本文出自 “SQLServer MySQL” 博客,请务必保留此出处http://dwchaoyue.blog.51cto.com/2826417/1555269

mysql 慢查询日志分析

上一篇:mysql的GUI工具


下一篇:SQL Error 1227: Access denied; you need the SUPER privilege for this operation