zoukankan      html  css  js  c++  java
  • MySQL 慢查询日志(Slow Query Log)

        同大多数关系型数据库一样,日志文件是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、慢查询日志演示

    1. long_query_time : 设定慢查询的阀值,超出次设定值的SQL即被记录到慢查询日志,缺省值为10s
    2. slow_query_log : 指定是否开启慢查询日志
    3. log_slow_queries : 指定是否开启慢查询日志(该参数要被slow_query_log取代,做兼容性保留)
    4. slow_query_log_file : 指定慢日志文件存放位置,可以为空,系统会给一个缺省的文件host_name-slow.log
    5. min_examined_row_limit:查询检查返回少于该参数指定行的SQL不被记录到慢查询日志
    6. log_queries_not_using_indexes: 不使用索引的慢查询日志是否记录到索引
    7. --当前版本
    8. root@localhost[(none)]> show variables like 'version';
    9. +---------------+------------+
    10. | Variable_name | Value |
    11. +---------------+------------+
    12. | version | 5.5.39-log |
    13. +---------------+------------+
    14. root@localhost[(none)]> show variables like '%slow%';
    15. +---------------------+---------------------------------+
    16. | Variable_name | Value |
    17. +---------------------+---------------------------------+
    18. | log_slow_queries | OFF |
    19. | slow_launch_time | 2 |
    20. | slow_query_log | OFF |
    21. | slow_query_log_file | /var/lib/mysql/suse11b-slow.log |
    22. +---------------------+---------------------------------+
    23. root@localhost[tempdb]> set global log_slow_queries=1;
    24. Query OK, 0 rows affected, 1 warning (0.00 sec)
    25. root@localhost[(none)]> show warnings;
    26. +---------+------+-------------------------------------------------------------------------------------------------------------------+
    27. | Level | Code | Message |
    28. +---------+------+-------------------------------------------------------------------------------------------------------------------+
    29. | Warning | 1287 | '@@log_slow_queries' is deprecated and will be removed in a future release. Please use '@@slow_query_log' instead |
    30. +---------+------+-------------------------------------------------------------------------------------------------------------------+
    31. --从下面的查询中可知,2个系统变量log_slow_queries,slow_query_log同时被置为on
    32. root@localhost[(none)]> show variables like '%slow%';
    33. +---------------------+---------------------------------+
    34. | Variable_name | Value |
    35. +---------------------+---------------------------------+
    36. | log_slow_queries | ON |
    37. | slow_launch_time | 2 |
    38. | slow_query_log | ON |
    39. | slow_query_log_file | /var/lib/mysql/suse11b-slow.log |
    40. +---------------------+---------------------------------+
    41. root@localhost[tempdb]> show variables like '%long_query_time%';
    42. +-----------------+-----------+
    43. | Variable_name | Value |
    44. +-----------------+-----------+
    45. | long_query_time | 10.000000 |
    46. +-----------------+-----------+
    47. --为便于演示,我们将全局和session级别long_query_time设置为1
    48. root@localhost[tempdb]> set global long_query_time=1;
    49. Query OK, 0 rows affected (0.00 sec)
    50. root@localhost[tempdb]> set session long_query_time=1;
    51. Query OK, 0 rows affected (0.00 sec)
    52. --Author : Leshami
    53. --Blog : http://blog.csdn.net/leshami
    54. root@localhost[tempdb]> create table tb_slow as select * from information_schema.columns;
    55. Query OK, 829 rows affected (0.10 sec)
    56. Records: 829 Duplicates: 0 Warnings: 0
    57. root@localhost[tempdb]> insert into tb_slow select * from tb_slow;
    58. Query OK, 829 rows affected (0.05 sec)
    59. Records: 829 Duplicates: 0 Warnings: 0
    60. .....为便于演示,我们插入一些数据,中间重复过程省略
    61. root@localhost[tempdb]> insert into tb_slow select * from tb_slow;
    62. Query OK, 26528 rows affected (4.40 sec)
    63. Records: 26528 Duplicates: 0 Warnings: 0
    64. root@localhost[tempdb]> system tail /var/lib/mysql/suse11b-slow.log
    65. /usr/sbin/mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with:
    66. Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock
    67. Time Id Command Argument
    68. # Time: 141004 22:05:48
    69. # User@Host: root[root] @ localhost []
    70. # Query_time: 4.396858 Lock_time: 0.000140 Rows_sent: 0 Rows_examined: 53056
    71. use tempdb;
    72. SET timestamp=1412431548;
    73. insert into tb_slow select * from tb_slow;
    74. ....再次插入一些记录....
    75. root@localhost[tempdb]> insert into tb_slow select * from tb_slow;
    76. Query OK, 212224 rows affected (37.51 sec)
    77. Records: 212224 Duplicates: 0 Warnings: 0
    78. root@localhost[tempdb]> select table_schema,table_name,count(*) from tb_slow
    79. -> group by table_schema,table_name order by 3,2;
    80. +--------------------+----------------------------------------------+----------+
    81. | table_schema | table_name | count(*) |
    82. +--------------------+----------------------------------------------+----------+
    83. | information_schema | COLLATION_CHARACTER_SET_APPLICABILITY | 1024 |
    84. | performance_schema | cond_instances | 1024 |
    85. ...........
    86. | mysql | user | 21504 |
    87. +--------------------+----------------------------------------------+----------+
    88. 83 rows in set (1.58 sec)
    89. root@localhost[tempdb]> system tail /var/lib/mysql/suse11b-slow.log
    90. # User@Host: root[root] @ localhost []
    91. # Query_time: 37.514172 Lock_time: 0.000123 Rows_sent: 0 Rows_examined: 424448
    92. SET timestamp=1412431806;
    93. insert into tb_slow select * from tb_slow;
    94. # Time: 141004 22:10:47
    95. # User@Host: root[root] @ localhost []
    96. # Query_time: 1.573293 Lock_time: 0.000183 Rows_sent: 83 Rows_examined: 424614
    97. SET timestamp=1412431847;
    98. select table_schema,table_name,count(*) from tb_slow --这条SQL被记录下来了,其查询时间为1.573293s
    99. group by table_schema,table_name order by 3,2;
    100. root@localhost[tempdb]> show variables like '%log_queries_not_using_indexes';
    101. +-------------------------------+-------+
    102. | Variable_name | Value |
    103. +-------------------------------+-------+
    104. | log_queries_not_using_indexes | OFF |
    105. +-------------------------------+-------+
    106. root@localhost[tempdb]> set global log_queries_not_using_indexes=1;
    107. Query OK, 0 rows affected (0.00 sec)
    108. --查看表tb_slow索引信息,表tb_slow无任何索引
    109. root@localhost[tempdb]> show index from tb_slow;
    110. Empty set (0.00 sec)
    111. root@localhost[tempdb]> select count(*) from tb_slow;
    112. +----------+
    113. | count(*) |
    114. +----------+
    115. | 424448 |
    116. +----------+
    117. 1 row in set (0.20 sec)
    118. root@localhost[tempdb]> system tail -n3 /var/lib/mysql/suse11b-slow.log
    119. # Query_time: 0.199840 Lock_time: 0.000152 Rows_sent: 1 Rows_examined: 424448
    120. SET timestamp=1412432188;
    121. select count(*) from tb_slow; --此次查询时间为0.199840,被记录的原因是因为没有走索引,因为表本身没有索引

    4、格式化慢查询日志

    1. 结构化慢查询日志就是把慢查询日志中的重要信息按照便于阅读以及按照特定的排序方式来提取SQL。
    2. 这种方式有点类似于Oracle中有个tkprof来格式化oracle的trace文件。
    3. 对于前面的慢查询日志我们使用mysqldumpslow来提取如下:
    4. suse11b:~ # mysqldumpslow -s at,al /var/lib/mysql/suse11b-slow.log
    5. Reading mysql slow query log from /var/lib/mysql/suse11b-slow.log
    6. Count: 4 Time=16.87s (67s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
    7. insert into tb_slow select * from tb_slow
    8. Count: 1 Time=0.20s (0s) Lock=0.00s (0s) Rows=1.0 (1), root[root]@localhost
    9. select count(*) from tb_slow
    10. Count: 1 Time=1.57s (1s) Lock=0.00s (0s) Rows=83.0 (83), root[root]@localhost
    11. select table_schema,table_name,count(*) from tb_slow
    12. group by table_schema,table_name order by N,N
    13. #以下是按照最大耗用时间排最后,只显示2条的方式格式化日志文件
    14. suse11b:~ # mysqldumpslow -r -t 2 /var/lib/mysql/suse11b-slow.log
    15. Reading mysql slow query log from /var/lib/mysql/suse11b-slow.log
    16. Count: 1 Time=1.57s (1s) Lock=0.00s (0s) Rows=83.0 (83), root[root]@localhost
    17. select table_schema,table_name,count(*) from tb_slow
    18. group by table_schema,table_name order by N,N
    19. Count: 4 Time=16.87s (67s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
    20. insert into tb_slow select * from tb_slow
    21. #获取mysqldumpslow的帮助信息
    22. suse11b:~ # mysqldumpslow --help
    23. Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
    24. Parse and summarize the MySQL slow query log. Options are
    25. --verbose verbose
    26. --debug debug
    27. --help write this text to standard output
    28. -v verbose
    29. -d debug
    30. -s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default
    31. al: average lock time
    32. ar: average rows sent
    33. at: average query time
    34. c: count #query的次数
    35. l: lock time
    36. r: rows sent #返回的记录数
    37. t: query time
    38. -r reverse the sort order (largest last instead of first)
    39. -t NUM just show the top n queries
    40. -a don't abstract all numbers to N and strings to 'S'
    41. -n NUM abstract numbers with at least n digits within names
    42. -g PATTERN grep: only consider stmts that include this string
    43. -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
    44. default is '*', i.e. match all
    45. -i NAME name of server instance (if using mysql.server startup script)
    46. -l don't subtract lock time from total time


    原文地址:https://blog.csdn.net/leshami/article/details/39829605

  • 相关阅读:
    [转载]PHP中PSR-[0-4]规范
    Git忽略规则及.gitignore规则不生效的解决办法
    nginx配置tp5的pathinfo模式并隐藏后台入口文件
    php过滤 字符
    使用ajax的post方式下载excel
    scws简单中文分词
    php的api及登录的权限验证
    对钩子的理解
    基于角色的权限控制
    微信开发之SVN提交代码与FTP同步到apache的根目录
  • 原文地址:https://www.cnblogs.com/jpfss/p/12077274.html
Copyright © 2011-2022 走看看