MySQL slow query log associated with the configuration

First, the concept

MySQL slow logs means running more than 10s (default) sql statement, its argument is long_query_time, MySQL database does not start slow query log, we need to manually set the parameters, of course, if it is not tuning required, it is generally not recommended to start this parameter, because slow query log will open more or less bring some performance impact. Slow query log file is written to support logging, also supports logging into the database table.

Second, the slow log parameters

1  slow_query_log: whether to open the slow query log, 1 on, 0 means closed.
2  
3 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
 4  
5 SLOW-Query-log-File: the new version (5.6 or later) MySQL database slow query log storage path. This parameter may not be provided, the system will default to a default file host_name- slow.log
 . 6  
. 7  long_query_time: slow query threshold, when the query time is more than a set threshold, logging.
8  
9  log_queries_not_using_indexes: unused index also recorded a query to the slow query log (optional).
10  
11 log_output: log storage. = log_output ' the FILE ' represents the log file is stored, the default value is ' the FILE ' . = log_output ' TABLE 'It said it would log into the database, so the log information will be written to mysql.slow_log table. MySQL database support simultaneous data <br> both logs storage configuration when separated by commas, such as: log_output = ' FILE, TABLE ' . Logging into the system of special log table, than the recorded files consume more system resources, so the need to enable the slow query log, and the need <br> to be able to get higher system performance, it is recommended that priority to a file .

Third, the slow log configuration

slow_query_log: whether to open the slow query log, 1 on, 0 means closed. slow_query_log value is OFF, meaning slow query log is disabled, can be turned on by setting the value of slow_query_log

 1 mysql> show variables like '%slow_query_log%';
 2 
 3 +---------------------+------------------------------------------+
 4 
 5 | Variable_name       | Value                                    |
 6 
 7 +---------------------+------------------------------------------+
 8 
 9 | slow_query_log      | OFF                                      |
10 
11 | slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
12 
13 +---------------------+------------------------------------------+
14 
15 2 rows in set (0.00 sec)

Logs can be slow to open by setting "slow_query_log"

 1 mysql> set global slow_query_log=1;
 2 
 3 Query OK, 0 rows affected (0.00 sec)
 4 
 5  
 6 
 7 mysql> show variables like '%slow_query_log%';
 8 
 9 +---------------------+------------------------------------------+
10 
11 | Variable_name       | Value                                    |
12 
13 +---------------------+------------------------------------------+
14 
15 | slow_query_log      | ON                                       |
16 
17 | slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
18 
19 +---------------------+------------------------------------------+
20 
21 2 rows in set (0.00 sec)

These are just a temporary set slow log, MySQL restart after failure, permanent need to modify the MySQL configuration file

After modifying the my.cnf file, add or modify parameters slow_query_log and slow_query_log_file, and then restart the MySQL server

long_query_time the default value is 10 seconds, can be used to modify the command, the parameters may be modified my.cnf inside. About the running time is exactly equal to long_query_time case, and will not be recorded. In other words, there is a judge in the mysql source greater than long_query_time, rather than greater than or equal.

=. 1 slow_query_log 
slow_query_log_file = / storage path usr / local / mysql / data / localhost-slow.log // slow the log file 

to view the log slow "long_query_time"
 1 mysql> show variables like 'long_query_time';
 2 
 3 +-----------------+-----------+
 4 
 5 | Variable_name   | Value     |
 6 
 7 +-----------------+-----------+
 8 
 9 | long_query_time | 10.000000 |
10 
11 +-----------------+-----------+
12 
13 1 row in set (0.00 sec)

Modify slow log

 1 set global long_query_time=4;
 2 
 3 Query OK, 0 rows affected (0.00 sec)
 4 
 5  
 6 
 7 mysql> show variables like 'long_query_time';
 8 
 9 +-----------------+-----------+
10 
11 | Variable_name   | Value     |
12 
13 +-----------------+-----------+
14 
15 | long_query_time | 10.000000 |
16 
17 +-----------------+-----------+
18 
19 1 row in set (0.00 sec)

After using the command set global long_query_time = 4 changes, the need to reconnect or open a new session to see the modified value. You use show variables like 'long_query_time' view is the variable value of the current session, you can not reconnect the session, but with the show global variables like 'long_query_time';

1 mysql> show global variables like 'long_query_time';
2 +-----------------+----------+
3 | Variable_name   | Value    |
4 +-----------------+----------+
5 | long_query_time | 4.000000 |
6 +-----------------+----------+
7 1 row in set (0.00 sec)

Select storage slow log

log_output parameters are specified 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, such information will be written to the log table mysql.slow_log. Simultaneously support both logs MySQL database storage, when arranged separated by commas, such as: log_output = 'FILE, TABLE'. Logging into the system of special log table, than the recorded files consume more system resources, so the need to enable the slow query log, but also need to be able to get higher system performance, it is recommended that priority to a file

 1 mysql> show variables like '%log_output%';
 2 
 3 +---------------+-------+
 4 
 5 | Variable_name | Value |
 6 
 7 +---------------+-------+
 8 
 9 | log_output    | TABLE |
10 
11 +---------------+-------+
12 
13 1 row in set (0.00 sec)

Slow query log how many

 1 mysql> show variables like '%log_output%';
 2 
 3 +---------------+-------+
 4 
 5 | Variable_name | Value |
 6 
 7 +---------------+-------+
 8 
 9 | log_output    | TABLE |
10 
11 +---------------+-------+
12 
13 1 row in set (0.00 sec)

 

Guess you like

Origin www.cnblogs.com/liaopeng123/p/11350562.html