Learn these two points, easy to understand MySQL slow query log

MySQL in the log include: error logs, binary logs, general query log and slow query log, and so on. Here are the more common under the main two logs: general query log and slow query log.

  1. The General Query Log: record established client connections and executed statements
  2. Slow query log: a record of all query execution times all over long_query_time seconds or do not use indexes queries

The General Query Log

In studying the general log queries that need to know two databases frequently used commands.

  • show variables like ‘%version%’;
mysql> show variables like '%version%';
+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| innodb_version          | 5.6.37                       |
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| version                 | 5.6.37-log                   |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | Linux                        |
+-------------------------+------------------------------+
7 rows in set (0.00 sec)
#上述命令,显示当前数据库中与版本号相关的东西。
  • show variables like ‘%general%’;
mysql>  show variables like '%general%';
+------------------+-------------------------------+
| Variable_name    | Value                         |
+------------------+-------------------------------+
| general_log      | OFF                           |
| general_log_file | /var/lib/mysql/nginx-test.log |
+------------------+-------------------------------+
2 rows in set (0.00 sec)
#可以查看,当前的通用日志查询是否开启,如果general_log的值为ON则为开启,为OFF则为关闭(默认情况下是关闭的)。
  • show variables like ‘%log_output%’
mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set (0.00 sec)

View the current slow query log output format, which can be FILE (hostname.log stored in a data file in a database), may also be TABLE (mysql.general_log stored in the database)

  • How to open MySQL general query log, and how to set common log format to be output on it?
开启通用日志查询: set global general_log=on;
关闭通用日志查询: set global general_log=off;
设置通用日志输出为表方式: set global log_output=’TABLE’;
设置通用日志输出为文件方式: set global log_output=’FILE’;
设置通用日志输出为表和文件方式:set global log_output=’FILE,TABLE’;
#注意:上述命令只对当前生效,当MySQL重启失效,如果要永久生效,需要配置my.cnf

Log output effect is as follows:
Record the mysql.general_log table structure as follows:

mysql> desc general_log;
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| Field        | Type                | Null | Key | Default           | Extra                       |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
| event_time   | timestamp           | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_host    | mediumtext          | NO   |     | NULL              |                             |
| thread_id    | bigint(21) unsigned | NO   |     | NULL              |                             |
| server_id    | int(10) unsigned    | NO   |     | NULL              |                             |
| command_type | varchar(64)         | NO   |     | NULL              |                             |
| argument     | mediumtext          | NO   |     | NULL              |                             |
+--------------+---------------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.00 sec)
  • My.cnf configuration file is as follows
general_log=1  #为1表示开启通用日志查询,值为0表示关闭通用日志查询
log_output=FILE,TABLE#设置通用日志的输出格式为文件和表

Slow query log

MySQL slow query log is a log provided by MySQL, to record the response time exceeds the threshold value statements in MySQL, refers specifically to the SQL runtime exceeds long_query_time worth, it will be recorded to the slow query log (log can be written file or database table, if the high-performance requirements, the proposal is written to the file). By default, MySQL database is not open query slow day, long_query_time default value of 10 (ie 10 seconds, usually set to ask one second), that is more than 10 seconds to run slow query statement.
In general, slow queries occur in large table (for example: a data table of millions), and query the field is not indexed, this time, to match the query criteria fields full table scan, time-consuming than query_log_time, was slow query.

How to view the current status on the slow query log?

  • show variables like ‘%quer%’;
mysql> show variables like '%quer%';
+----------------------------------------+-------------------------------+
| Variable_name                          | Value                         |
+----------------------------------------+-------------------------------+
| binlog_rows_query_log_events           | OFF                           |
| ft_query_expansion_limit               | 20                            |
| have_query_cache                       | YES                           |
| log_queries_not_using_indexes          | ON                            |
| log_throttle_queries_not_using_indexes | 0                             |
| long_query_time                        | 10.000000                     |
| query_alloc_block_size                 | 8192                          |
| query_cache_limit                      | 1048576                       |
| query_cache_min_res_unit               | 4096                          |
| query_cache_size                       | 1048576                       |
| query_cache_type                       | OFF                           |
| query_cache_wlock_invalidate           | OFF                           |
| query_prealloc_size                    | 8192                          |
| slow_query_log                         | ON                            |
| slow_query_log_file                    | /var/log/mysql/mysql_slow.log |
+----------------------------------------+-------------------------------+
15 rows in set (0.00 sec)
#主要掌握以下的几个参数:
(1)slow_query_log 的值为ON为开启慢查询日志,OFF则为关闭慢查询日志。
(2)slow_query_log_file 的值是记录的慢查询日志到文件中(注意:默认名为主机名.log,慢查询日志是否写入指定文件中,需要指定慢查询的输出日志格式为文件,相关命令为:show variables like ‘%log_output%’;去查看输出的格式)。
(3)long_query_time 指定了慢查询的阈值,即如果执行语句的时间超过该阈值则为慢查询语句,默认值为10秒。
(4)log_queries_not_using_indexes 如果值设置为ON,则会记录所有没有利用索引的查询(注意:如果只是将log_queries_not_using_indexes设置为ON,而将slow_query_log设置为OFF,此时该设置也不会生效,即该设置生效的前提是slow_query_log的值设置为ON),一般在性能调优的时候会暂时开启。

Set MySQL slow query log format for the output file or table, or both?

  • show variables like ‘%log_output%’;
mysql> show variables like '%log_output%';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| log_output    | FILE,TABLE |
+---------------+------------+
1 row in set (0.00 sec)

Log_output can view the value of the output format, the above value FILE, TABLE. Of course, we can set the output format of the text, or both text and command recording database tables, set as follows:

#慢查询日志输出到表中(即mysql.slow_log)
set globallog_output=’TABLE’;
#慢查询日志仅输出到文本中(即:slow_query_log_file指定的文件)
setglobal log_output=’FILE’;
#慢查询日志同时输出到文本和表中
setglobal log_output=’FILE,TABLE’; 

Data analysis of the data format text in tables slow query log in

  • Logging myql.slow_log slow query table in the following format:
mysql> mysql> select * from mysql.slow_log limit 1;
+---------------------+--------------------------------+------------+-----------+-----------+---------------+------------+----------------+-----------+-----------+----------------------------------------------------------------------------------------+-----------+
| start_time          | user_host                      | query_time | lock_time | rows_sent | rows_examined | db         | last_insert_id | insert_id | server_id | sql_text                                                                               | thread_id |
+---------------------+--------------------------------+------------+-----------+-----------+---------------+------------+----------------+-----------+-----------+----------------------------------------------------------------------------------------+-----------+
| 2018-02-07 11:16:55 | root[root] @  [121.196.203.51] | 00:00:00   | 00:00:00  |        13 |            40 | jp_core_db |              0 |         0 |         0 | select pd.lastAuction from Product pd where pd.status = 'O' and pd.auctionStatus = 'A' |      1621 |
+---------------------+--------------------------------+------------+-----------+-----------+---------------+------------+----------------+-----------+-----------+----------------------------------------------------------------------------------------+-----------+
1 row in set (0.00 sec)
  • Slow query log records to mysql_slow.log file format is as follows:
# Time: 180118 14:58:37
# User@Host: root[root] @ localhost []  Id:   150
# Query_time: 0.000270  Lock_time: 0.000109 Rows_sent: 0  Rows_examined: 6
SET timestamp=1516258717;
delete from user where User='app';
#可以看到,不管是表还是文件,都具体记录了:是那条语句导致慢查询(sql_text),该慢查询语句的查询时间(query_time),锁表时间(Lock_time),以及扫描过的行数(rows_examined)等信息。

How to query the current number of slow queries?
There are a number of variables which tracks the current slow query in MySQL:

  • show global status like ‘%slow%’;
mysql> show global status like '%slow%';
+---------------------+-------+
| Variable_name       | Value |
+---------------------+-------+
| Slow_launch_threads | 132   |
| Slow_queries        | 1772  |
+---------------------+-------+
2 rows in set (0.00 sec)
(注意:上述所有命令,如果都是通过MySQL的shell将参数设置进去,如果重启MySQL,所有设置好的参数将失效,如果想要永久的生效,需要将配置参数写入my.cnf文件中)。

How to use the built-in MySQL slow query log analysis tool mysqldumpslow analyze the log?

mysqldumpslow –s c –t 10 slow-query.log

Specific parameters are as follows:

  • Sort represents ways -s, c, t, l, r are recorded in accordance with the number, time, query time, number of records returned to sort, ac, at, al, ar, indicate corresponding flashback;
  • -t meaning expressed top, followed by the return to the previous after the data showing how many;
  • -g behind can write regular expression matching, case-insensitive.
[root@nginx-test /var/log/mysql]# mysqldumpslow -s c -t 2 /var/log/mysql/mysql_slow.log 
Reading mysql slow query log from /var/log/mysql/mysql_slow.log
Count: 125448  Time=0.00s (131s)  Lock=0.00s (3s)  Rows=2.2 (272835), 2users@2hosts
  select productauc0_.productAuctionId as productA1_12_, productauc0_.auctionIndex as auctionI2_12_, productauc0_.bidCoins as bidCoins3_12_, productauc0_.bidPrice as bidPrice4_12_, productauc0_.bidStep as bidStep5_12_, productauc0_.bidTime as bidTime6_12_, productauc0_.bidder as bidder7_12_, productauc0_.buyFlag as buyFlag8_12_, productauc0_.categoryCode as category9_12_, productauc0_.createTime as createT10_12_, productauc0_.currentAuctionDetailId as current11_12_, productauc0_.currentBidPrice as current12_12_, productauc0_.currentBidTime as current13_12_, productauc0_.currentBidder as current14_12_, productauc0_.effectCoin as effectC15_12_, productauc0_.effetcPoint as effetcP16_12_, productauc0_.endTime as endTime17_12_, productauc0_.newUserFlag as newUser18_12_, productauc0_.productCode as product19_12_, productauc0_.productCost as product20_12_, productauc0_.productName as product21_12_, productauc0_.productPrice as product22_12_, productauc0_.refundRate as refundR23_12_, productauc0_.startPrice as startPr24_12_, productauc0_.startTime as startTi25_12_, productauc0_.status as status26_12_, productauc0_.updateTime as updateT27_12_ from ProductAuction productauc0_ where productauc0_.status='S'

Count: 66216  Time=0.00s (127s)  Lock=0.00s (2s)  Rows=1.7 (115074), root[root]@[121.196.203.51]
  select productauc0_.productAuctionId as productA1_12_, productauc0_.auctionIndex as auctionI2_12_, productauc0_.bidCoins as bidCoins3_12_, productauc0_.bidPrice as bidPrice4_12_, productauc0_.bidStep as bidStep5_12_, productauc0_.bidTime as bidTime6_12_, productauc0_.bidder as bidder7_12_, productauc0_.buyFlag as buyFlag8_12_, productauc0_.categoryCode as category9_12_, productauc0_.createTime as createT10_12_, productauc0_.currentAuctionDetailId as current11_12_, productauc0_.currentBidPrice as current12_12_, productauc0_.currentBidTime as current13_12_, productauc0_.currentBidder as current14_12_, productauc0_.effectCoin as effectC15_12_, productauc0_.effetcPoint as effetcP16_12_, productauc0_.endTime as endTime17_12_, productauc0_.firstBidTime as firstBi18_12_, productauc0_.newUserFlag as newUser19_12_, productauc0_.noviceReturnFlag as noviceR20_12_, productauc0_.productCode as product21_12_, productauc0_.productCost as product22_12_, productauc0_.productName as product23_12_, productauc0_.productPrice as product24_12_, productauc0_.refundRate as refundR25_12_, productauc0_.startPrice as startPr26_12_, productauc0_.startTime as startTi27_12_, productauc0_.status as status28_12_, productauc0_.updateTime as updateT29_12_ from ProductAuction productauc0_ where productauc0_.status='S'

上述中的参数含义如下:
Count:125448       #语句出现了125448次;
Time=0.00s(131s)  #执行最长时间为0.00s,累计总耗费时间131s;
Lock=0.0s(3s)     #等待锁最长时间为0s,累计等待锁耗费时间为3s;
Rows=2.2(272835) #发送给客户端最多的行数为2.2,累计发送给客户端的函数为272835
#注意:mysqldumpslow脚本是用perl语言写的,具体mysqldumpslow的用法后期再讲)
  • The actual learning process, learn how to set slow query is valid?
    Quite simply, we can manually generate a slow query, for example, if the value of our slow query log_query_time is 1, then we can execute the following statement:
    the SELECT SLEEP (1);
    article statement that is slow query, then, You can go to view the appropriate log output file or table if there is the article statement.

Guess you like

Origin blog.csdn.net/qq_33235529/article/details/87800454