2.8 MySQL- slow query application

1. Scene demand

       Division I discovered that our business is very slow in the corresponding part of the interface, users can even instant noodles in the waiting process, discussed by our analysis of you people need to use mysql slow query locate problematic SQL statements, please Division I explain what mysql slow queries and how to turn on slow query, its detailed configuration and use.

2. Reference answer

      MySQL slow query, full name is slow query log is a log provided by MySQL, the response time exceeds the threshold used to record statements in MySQL. Specific environment, running for more than long_query_time value of SQL statements, it will be logged to the slow query log. long_query_time The default value is 10, meaning that the recording operation is 10 seconds or more statements. By default, MySQL database does not start slow query log, you need to manually set this parameter.

       Of course, if it is not tuning required, generally we do not recommend activating this feature, because the slow query log will open more or less bring some performance impact.

       In addition, support will slow query log records written to the log files and database tables.

3. MySQL parameters slow query explanation:

L   slow_query_log : whether to open the slow query log, 1 on, 0 means closed.

L   log-SLOW-Queries  : Legacy (version 5.6 or less) MySQL database slow query log storage path. Can not set this parameter, the system will default to a default file host_name-slow.log

L   SLOW-Query-log-File : the new version (5.6 or later) MySQL database slow query log storage path. Can not set this parameter, the system will default to a default file host_name-slow.log

L   long_query_time : slow query threshold, when the query time is more than a set threshold, logging.

L   log_queries_not_using_indexes : unused index also recorded a query to the slow query log (optional).

L   log_output : log storage. log_output = 'FILE' represents the log file is stored, the default value is 'FILE'. log_output = 'TABLE' represents the log stored in the database.

       How to configure slow query log it? Slow_query_log default value is OFF, meaning slow query log is disabled, can be turned on by setting the value slow_query_log as follows:

show variables  like '%slow_query_log%';

+----------------------+-----------------------------------------------+
| Variable_name  | Value                                              |
+----------------------+-----------------------------------------------+
| slow_query_log | OFF                                                |
| slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |
+----------------------+------------------------------------------------+

2 rows in set (0.00 sec)

       Use set global slow_query_log = 1 opens the slow query log only take effect for the current database, MySQL after the restart will fail. If you want permanent, you must modify the configuration file my.cnf (other system variables, too)

set global slow_query_log=1;

       my.cnf to add or modify the parameters and slow_query_log slow_query_log_file, as follows:

slow_query_log = 1

slow_query_log_file = /tmp/mysql_slow.log

       slow_query_log_file This parameter is used to specify the slow query log storage path, the default is the host_name-slow.log file.

show variables like 'slow_query_log_file';

+---------------------+-----------------------------------------------+

 | Variable_name       | Value                                         |

 +---------------------+-----------------------------------------------+

 | slow_query_log_file | /home/WDPM/MysqlData/mysql/DB-Server-slow.log |

 +---------------------+-----------------------------------------------+

 1 row in set (0.00 sec)

       After opening the slow query log, what kind of SQL will slow query log to record inside it? This parameter is long_query_time control default long_query_time is 10 seconds, the command may be used to modify and to be in my.cnf modify parameters inside.

       Exactly equal to the running time on long_query_time case, it will not be recorded; that is, where the source is determined greater than mysql long_query_time , not greater than or equal.

       Starting MySQL 5.1, long_query_time began to run SQL statements in microseconds record time, just seconds before the recording. If the record to the table inside, only the integer part of the recording, the recording is not part microseconds

       Note that the following situations may occur under certain circumstances when the way you use local settings, look at the following:

the Variables like Show 'long_query_time%';
# to view the current time long_query_time

+-----------------+-----------+

 | Variable_name   | Value     |

 +-----------------+-----------+

 | long_query_time | 10.000000 |

 +-----------------+-----------+

Global long_query_time. 4 = SET;
# Set the current time long_query_time

the Variables like Show 'long_query_time';
# View long_query_time time again

       As shown above, modify the variables long_query_time , but the query variable long_query_time value or 10, do not have to modify it? Note: Use the command set global long_query_time = 4 after the modification, you need to reconnect or open a new session to see the modified value. 'Long_query_time' view of the current session only variable values show variables like. You may not reconnect the session, but rather like 'long_query_time' Show Global Variables; .

       log_output参数指定慢查询日志的存储方式:

l   log_output='FILE'表示将日志存入文件,默认值也是'FILE'。

l   log_output='TABLE'表示将日志存入数据库,这样日志信息就会被写入到mysql.slow_log表中。同时也支持两种日志存储方式,配置的时候以逗号隔开即可,如:log_output='FILE,TABLE'。

       日志记录到系统的专用日志表中,要比记录到文件耗费更多的系统资源。因此对于需要启用慢查询日志,又需要能够获得更高的系统性能,那么建议优先记录到文件。

show variables like '%log_output%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_output    | FILE  |

+---------------+-------+

set global log_output='TABLE';

show variables like '%log_output%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_output    | TABLE |

+---------------+-------+

select sleep(5) ;

+----------+

| sleep(5) |

+----------+

|        0 |

+----------+

       当我们在上面执行了一次sleep5之后,这条操作将会被记录在慢查询日志中,我们来看看:

mysql> select * from mysql.slow_log;

 +---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+

 | start_time          | user_host                 | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text        | thread_id |

 +---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+

 | 2016-06-16 17:37:53 | root[root] @ localhost [] | 00:00:03   | 00:00:00  |         1 |             0 |    |              0 |         0 |         1 | select sleep(3) |         5 |

 | 2016-06-16 21:45:23 | root[root] @ localhost [] | 00:00:05   | 00:00:00  |         1 |             0 |    |              0 |         0 |         1 | select sleep(5) |         2 |

 +---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+

  1. 4.   一些其他慢查询配置选项如下。

      log-queries-not-using-indexes该系统变量指定未使用索引的查询也被记录到慢查询日志中,调优的话,建议开启这个选项。另外,开启了这个参数,其实使用full index scan的SQL也会被记录到慢查询日志。

set global log_queries_not_using_indexes=1;
# 开启该选项

       log_slow_admin_statements这个系统变量表示,是否将慢管理语句例如ANALYZE TABLE和ALTER TABLE等记入慢查询日志

       slow_queries如果你想查询有多少条慢查询记录,可以使用slow _queries系统变量。

mysql> show global status like '%Slow_queries%';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| Slow_queries  | 2104  |

+---------------+-------+

1 row in set (0.00 sec)

Guess you like

Origin www.cnblogs.com/lihouqi/p/12664261.html