MySQL 慢查询日志 Slow Query Log

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

    同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分。MySQL有几种不同的日志文件,通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等。这些日志可以帮助我们定位mysqld内部发生的事件,数据库性能故障,记录数据的变更历史,用户恢复数据库等等。本文主要描述通用查询日志。

 

1、MySQL日志文件系统的组成
   a、错误日志:记录启动、运行或停止mysqld时出现的问题。
   b、通用日志:记录建立的客户端连接和执行的语句。
   c、更新日志:记录更改数据的语句。该日志在MySQL 5.1中已不再使用。
   d、二进制日志:记录所有更改数据的语句。还用于复制。
   e、慢查询日志:记录所有执行时间超过long_query_time秒的所有查询或不使用索引的查询。
   f、Innodb日志:innodb redo log
   
   缺省情况下,所有日志创建于mysqld数据目录中。
   可以通过刷新日志,来强制mysqld来关闭和重新打开日志文件(或者在某些情况下切换到一个新的日志)。
   当你执行一个FLUSH LOGS语句或执行mysqladmin flush-logs或mysqladmin refresh时,则日志被老化。
   对于存在MySQL复制的情形下,从复制服务器将维护更多日志文件,被称为接替日志。

 

2、慢查询日志
   慢查询日志是将mysql服务器中影响数据库性能的相关SQL语句记录到日志文件,通过对这些特殊的SQL语句分析,改进以达到提高数据库性能的目的。
   通过使用--slow_query_log[={0|1}]选项来启用慢查询日志。所有执行时间超过long_query_time秒的SQL语句都会被记录到慢查询日志。
   缺省情况下hostname-slow.log为慢查询日志文件安名,存放到数据目录,同时缺省情况下未开启慢查询日志。
   缺省情况下数据库相关管理型SQL(比如OPTIMIZE TABLE、ANALYZE TABLE和ALTER TABLE)不会被记录到日志。
   对于管理型SQL可以通过--log-slow-admin-statements开启记录管理型慢SQL。
   mysqld在语句执行完并且所有锁释放后记入慢查询日志。记录顺序可以与执行顺序不相同。获得初使表锁定的时间不算作执行时间。
   
   可以使用mysqldumpslow命令获得日志中显示的查询摘要来处理慢查询日志。
   用查询缓存处理的查询不加到慢查询日志中,表有零行或一行而不能从索引中受益的查询也不写入慢查询日志。
   MySQL服务器按以下顺序记录SQL是否写入到慢查询日志
     a. The query must either not be an administrative statement, or --log-slow-adminstatements must have been specified.
     b. The query must have taken at least long_query_time seconds, or log_queries_not_using_indexes must be enabled and the query used no indexes for row lookups.
     c. The query must have examined at least min_examined_row_limit rows.
     d. The query must not be suppressed according to the log_throttle_queries_not_using_indexes setting.

 

3、慢查询日志演示

long_query_time     :  设定慢查询的阀值,超出次设定值的SQL即被记录到慢查询日志,缺省值为10sslow_query_log      :  指定是否开启慢查询日志log_slow_queries    :  指定是否开启慢查询日志(该参数要被slow_query_log取代,做兼容性保留)slow_query_log_file :  指定慢日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.logmin_examined_row_limit:查询检查返回少于该参数指定行的SQL不被记录到慢查询日志log_queries_not_using_indexes: 不使用索引的慢查询日志是否记录到索引    --当前版本root@localhost[(none)]> show variables like 'version';+---------------+------------+| Variable_name | Value      |+---------------+------------+| version       | 5.5.39-log |+---------------+------------+root@localhost[(none)]> show variables like '%slow%';+---------------------+---------------------------------+| Variable_name       | Value                           |+---------------------+---------------------------------+| log_slow_queries    | OFF                             || slow_launch_time    | 2                               || slow_query_log      | OFF                             || slow_query_log_file | /var/lib/mysql/suse11b-slow.log |+---------------------+---------------------------------+root@localhost[tempdb]> set global log_slow_queries=1;Query OK, 0 rows affected, 1 warning (0.00 sec)root@localhost[(none)]> show warnings;+---------+------+-------------------------------------------------------------------------------------------------------------------+| Level   | Code | Message                                                                                                           |+---------+------+-------------------------------------------------------------------------------------------------------------------+| Warning | 1287 | '@@log_slow_queries' is deprecated and will be removed in a future release. Please use '@@slow_query_log' instead |+---------+------+-------------------------------------------------------------------------------------------------------------------+--从下面的查询中可知,2个系统变量log_slow_queries,slow_query_log同时被置为onroot@localhost[(none)]> show variables like '%slow%';+---------------------+---------------------------------+| Variable_name       | Value                           |+---------------------+---------------------------------+| log_slow_queries    | ON                              || slow_launch_time    | 2                               || slow_query_log      | ON                              || slow_query_log_file | /var/lib/mysql/suse11b-slow.log |+---------------------+---------------------------------+root@localhost[tempdb]> show variables like '%long_query_time%';+-----------------+-----------+| Variable_name   | Value     |+-----------------+-----------+| long_query_time | 10.000000 |+-----------------+-----------+--为便于演示,我们将全局和session级别long_query_time设置为1root@localhost[tempdb]> set global long_query_time=1;Query OK, 0 rows affected (0.00 sec)root@localhost[tempdb]> set session long_query_time=1;Query OK, 0 rows affected (0.00 sec)--Author : Leshami--Blog   : http://blog.csdn.net/leshamiroot@localhost[tempdb]> create table tb_slow as select * from information_schema.columns;Query OK, 829 rows affected (0.10 sec)Records: 829  Duplicates: 0  Warnings: 0root@localhost[tempdb]> insert into tb_slow select * from tb_slow;Query OK, 829 rows affected (0.05 sec)Records: 829  Duplicates: 0  Warnings: 0       .....为便于演示,我们插入一些数据,中间重复过程省略root@localhost[tempdb]> insert into tb_slow select * from tb_slow;Query OK, 26528 rows affected (4.40 sec)Records: 26528  Duplicates: 0  Warnings: 0root@localhost[tempdb]> system tail  /var/lib/mysql/suse11b-slow.log/usr/sbin/mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sockTime                 Id Command    Argument# Time: 141004 22:05:48# User@Host: root[root] @ localhost []# Query_time: 4.396858  Lock_time: 0.000140 Rows_sent: 0  Rows_examined: 53056use tempdb;SET timestamp=1412431548;insert into tb_slow select * from tb_slow;    ....再次插入一些记录....root@localhost[tempdb]> insert into tb_slow select * from tb_slow;Query OK, 212224 rows affected (37.51 sec)Records: 212224  Duplicates: 0  Warnings: 0root@localhost[tempdb]> select table_schema,table_name,count(*) from tb_slow    -> group by table_schema,table_name order by 3,2;+--------------------+----------------------------------------------+----------+| table_schema       | table_name                                   | count(*) |+--------------------+----------------------------------------------+----------+| information_schema | COLLATION_CHARACTER_SET_APPLICABILITY        |     1024 || performance_schema | cond_instances                               |     1024 |                  ...........| mysql              | user                                         |    21504 |+--------------------+----------------------------------------------+----------+83 rows in set (1.58 sec)                  root@localhost[tempdb]> system tail  /var/lib/mysql/suse11b-slow.log# User@Host: root[root] @ localhost []# Query_time: 37.514172  Lock_time: 0.000123 Rows_sent: 0  Rows_examined: 424448SET timestamp=1412431806;insert into tb_slow select * from tb_slow;# Time: 141004 22:10:47# User@Host: root[root] @ localhost []# Query_time: 1.573293  Lock_time: 0.000183 Rows_sent: 83  Rows_examined: 424614SET timestamp=1412431847;select table_schema,table_name,count(*) from tb_slow  --这条SQL被记录下来了,其查询时间为1.573293sgroup by table_schema,table_name order by 3,2;root@localhost[tempdb]> show variables like '%log_queries_not_using_indexes';+-------------------------------+-------+| Variable_name                 | Value |+-------------------------------+-------+| log_queries_not_using_indexes | OFF   |+-------------------------------+-------+root@localhost[tempdb]> set global log_queries_not_using_indexes=1;Query OK, 0 rows affected (0.00 sec)--查看表tb_slow索引信息,表tb_slow无任何索引root@localhost[tempdb]> show index from tb_slow;Empty set (0.00 sec)root@localhost[tempdb]> select count(*) from tb_slow;+----------+| count(*) |+----------+|   424448 |+----------+1 row in set (0.20 sec)root@localhost[tempdb]> system tail -n3 /var/lib/mysql/suse11b-slow.log# Query_time: 0.199840  Lock_time: 0.000152 Rows_sent: 1  Rows_examined: 424448SET timestamp=1412432188;select count(*) from tb_slow;   --此次查询时间为0.199840,被记录的原因是因为没有走索引,因为表本身没有索引

4、格式化慢查询日志

结构化慢查询日志就是把慢查询日志中的重要信息按照便于阅读以及按照特定的排序方式来提取SQL。这种方式有点类似于Oracle中有个tkprof来格式化oracle的trace文件。对于前面的慢查询日志我们使用mysqldumpslow来提取如下:suse11b:~ # mysqldumpslow -s at,al /var/lib/mysql/suse11b-slow.logReading mysql slow query log from /var/lib/mysql/suse11b-slow.logCount: 4  Time=16.87s (67s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost  insert into tb_slow select * from tb_slowCount: 1  Time=0.20s (0s)  Lock=0.00s (0s)  Rows=1.0 (1), root[root]@localhost  select count(*) from tb_slowCount: 1  Time=1.57s (1s)  Lock=0.00s (0s)  Rows=83.0 (83), root[root]@localhost  select table_schema,table_name,count(*) from tb_slow  group by table_schema,table_name order by N,N#以下是按照最大耗用时间排最后,只显示2条的方式格式化日志文件suse11b:~ # mysqldumpslow -r -t 2 /var/lib/mysql/suse11b-slow.logReading mysql slow query log from /var/lib/mysql/suse11b-slow.logCount: 1  Time=1.57s (1s)  Lock=0.00s (0s)  Rows=83.0 (83), root[root]@localhost  select table_schema,table_name,count(*) from tb_slow  group by table_schema,table_name order by N,NCount: 4  Time=16.87s (67s)  Lock=0.00s (0s)  Rows=0.0 (0), root[root]@localhost  insert into tb_slow select * from tb_slow  #获取mysqldumpslow的帮助信息suse11b:~ # mysqldumpslow --helpUsage: 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        #query的次数                 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


鹏城DBA总群

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值