MySQL slow log

The Slow Query Log: https://dev.mysql.com/doc/refman/5.7/en/slow-query-log.html
Selecting General Query and Slow Query Log Output Destinations: https://dev.mysql.com/ doc/refman/5.7/en/log-destinations.html
MySQL slow query log summary: http://www.cnblogs.com/kerrycode/p/5593204.html
MYSQL enable slow query log implementation: http://www.cnblogs .com/wangtao_20/archive/2013/09/06/3304645.htmlIntroduction
:
    The slow query log is a log record provided by MySQL. It is used to record SQL statements whose response time exceeds the threshold long_query_time in MySQL. If the SQL execution time exceeds long_query_timeL, it will be recorded in the slow query log. The default value of long_query_time is 10, which means that statements with an execution time of more than 10s will be recorded in the slow query log. long_query_time is accurate to milliseconds, you can configure 1.005, the threshold of this form. By default, the Mysql database does not start the slow query log. We need to manually set this parameter or through the configuration file (my.cnf). Of course, if it is not required for tuning, it is generally not recommended to start this parameter, because the slow query is turned on. Logs have a more or less performance impact. As far as the method of starting the slow query log is concerned, it is recommended to set it manually, and it is not recommended to use a configuration file. If it is set manually, it will take effect without restarting, and the configuration file method requires restarting. Tuning is not needed until the official date, so we use the manual setting method. The slow query log supports writing log records to files (FILE) and also supports writing log records to database tables (TABLE).
Today we will look at manually starting the slow query log function:
check whether the slow query log is enabled
mysql> show variables like '%slow_query%';
    -> ;
+---------------------------+---------------------------------+
| Variable_name             | Value                           |
+---------------------------+---------------------------------+
| slow_query_log            | OFF                             |
| slow_query_log_file       | /mysqldata/Donald_Draper-slow.log |
+---------------------------+---------------------------------+
2 rows in set


slow_query_log controls the slow query log, slow_query_log_file is the slow query log file, generally hostname-slow.log, mainly this file mysql must have permission to read and write. Enable slow query log function


mysql> set global slow_query_log = 1;
Query OK, 0 rows affected

mysql> show variables like '%slow_query%';
+---------------------+---------------------------------+
| Variable_name       | Value                           |
+---------------------+---------------------------------+
| slow_query_log      | ON                              |
| slow_query_log_file | /mysqldata/Donald_Draper-slow.log |
+---------------------+---------------------------------+
2 rows in set

Turned on, and

then check the slow query response time threshold long_query_time
mysql> show variables like '%query_time%';
+------------------------------+---------------------------------+
| Variable_name                | Value                           |
+------------------------------+---------------------------------+
| long_query_time              | 10.000000                       |
+------------------------------+---------------------------------+
1 rows in set

The default value of long_query_time is 10, which means that statements with an execution time of more than 10s will be recorded in the slow query log.
long_query_time is accurate to milliseconds, you can configure 1.005, the threshold of this form.

Modify the slow query response time threshold long_query_time to 1 second
mysql> set global  long_query_time = 1;
Query OK, 0 rows affected

mysql> show variables like '%query_time%';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set


Check the modified response time threshold long_query_time.
Note: the above command show variables like '%query_time%' cannot be used at this time. After this command is executed, the original value is still used. This may be the cache used by mysql. We use show global variables like '%query_time%' command to view

mysql> show global variables like '%query_time%';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set

mysql>


Let's take a look at the default output format of the slow query log:


mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set

mysql>

There are two ways of log_output, one is FILE, and the log is recorded, and the other is TABLE, which records the slow log in the
mysql.slow_log table.

Let’s first look at the first method:
execute the query and view the slow query log file:
Donald_Draper:~ #tail -f Donald_Draper-slow.log
# Time: 170518 14:29:14
# User@Host: donald[123456] @  [192.168.31.153]  Id:    16
# Query_time: 0.009314  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0
use test;
SET timestamp=1495088954;
# administrator command: Init DB;
# Time: 170518 14:29:15
# User@Host: donald[123456] @  [192.168.31.153]  Id:    16
# Query_time: 0.665908  Lock_time: 0.000156 Rows_sent: 69  Rows_examined: 69
SET timestamp=1495088955;
SHOW FULL TABLES WHERE Table_type != 'VIEW';
# Time: 170518 14:29:28
# User@Host: donald[123456] @  [192.168.31.153]  Id:    17
# Query_time: 0.033067  Lock_time: 0.000183 Rows_sent: 1  Rows_examined: 1
SET timestamp=1495088968;
SHOW TABLE STATUS LIKE 'message_info';
# User@Host: donald[123456] @  [192.168.31.153]  Id:    17
# Query_time: 0.048564  Lock_time: 0.000116 Rows_sent: 1000  Rows_examined: 1000
SET timestamp=1495088968;
SELECT * FROM `message_info` LIMIT 0, 1000;
# Time: 170518 14:32:28
# User@Host: donald[123456] @  [192.168.31.153]  Id:    10
# Query_time: 106.102272  Lock_time: 0.000191 Rows_sent: 1  Rows_examined: 8494928
SET timestamp=1495089148;
select count(*) from message_info;


Let's take one
# Time: 170518 14:32:28 Occurrence time
# User@Host: donald[123456] @ [192.168.31.153] Id: 10 Access information
# Query time lock time           
# Query_time: 106.102272  Lock_time: 0.000191 Rows_sent: 1  Rows_examined: 8494928
SET timestamp=1495089148;
#Execute slow query statement
select count(*) from message_info;


Let's look at the second slow log output mode - TABLE:

Modify the slow log output mode log_output to TABLE
mysql> set global  log_output = 'TABLE';
Query OK, 0 rows affected

mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+
1 row in set

mysql>


Query the slow log table
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 |
+---------------------+-----------------------------------+------------+-----------+-----------+---------------+------------------+----------------+-----------+-----------+------------------------------------------+-----------+
| 2017-05-18 14:42:01 | donald[123456] @  [192.168.31.153] | 00:00:00   | 00:00:00  |        12 |            12 | mysql            |              0 |         0 |         1 | SHOW COLUMNS FROM `mysql`.`slow_log`     |        19 |
| 2017-05-18 14:42:30 | donald[123456] @  [192.168.31.153] | 00:00:37   | 00:00:00  |         1 |       1260746 | test |              0 |         0 |         1 | select count(*) from company_info |        10 |
| 2017-05-18 14:42:45 | donald[123456] @  [192.168.31.153] | 00:00:00   | 00:00:00  |         2 |             2 | mysql            |              0 |         0 |         1 | SELECT * FROM `slow_log` LIMIT 0, 1000   |        19 |
+---------------------+-----------------------------------+------------+-----------+-----------+---------------+------------------+----------------+-----------+-----------+------------------------------------------+-----------+
3 rows in set

mysql>


We can also configure the log_queries parameter to record statements that do not use indexes in the
slow log. See log_queries_not_using_indexes


mysql> show variables like '%log_queries%';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF   |
+-------------------------------+-------+
1 row in set


Enable logging of statements that do not use an index

mysql>  set global log_queries_not_using_indexes = 1;
Query OK, 0 rows affected

mysql> show variables like '%log_queries%';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON    |
+-------------------------------+-------+
1 row in set


When the function of recording statements without using indexes is enabled, we can use log_throttle_queries_not_using_indexes to
control the number of statements per second that are allowed to record without using indexes. The default value is 0, that is, there is no limit. If there is no limit, too many such logs will be generated. It will affect the performance of mysql.

mysql> show variables like '%log_throttle%';
+----------------------------------------+-------+
| Variable_name                          | Value |
+----------------------------------------+-------+
| log_throttle_queries_not_using_indexes | 0     |
+----------------------------------------+-------+
1 row in set


It is not clear and convenient to analyze the specific slowness of the slow query statement directly through the slow log file and the slow log table. We can use the slow log analysis tool mysqldumpslow provided by MySQL to analyze the slow log file.

Donald_Draper:~ # mysqldumpslow --help
Usage: 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, default is average query time
                al: average lock time
                ar: average rows sent average number of records returned
                at: average query time average query time
                 c: count number of visits
                 l: lock time lock time
                 r: the number of records returned by rows sent
                 t: query time 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

Example: The query statement is sorted by the average query time, average lock time, query time, lock time
Donald_Draper:~ # mysqldumpslow -s at al t l c -t 6 /mysqldata/Donald_Draper-slow.log

Reading mysql slow query log from al t l c /mysqldata/Donald_Draper-slow.log
Count: 1  Time=173.86s (173s)  Lock=0.00s (0s)  Rows=1.0 (1), donald[123456]@[192.168.31.153]
  select count(*) from message_info

Count: 1  Time=106.10s (106s)  Lock=0.00s (0s)  Rows=1.0 (1), donald[123456]@[192.168.31.153]
  select count(*) from company_info

Count: 1  Time=0.67s (0s)  Lock=0.00s (0s)  Rows=69.0 (69), donald[123456]@[192.168.31.153]
  SHOW FULL TABLES WHERE Table_type != 'S'

Count: 1  Time=0.05s (0s)  Lock=0.00s (0s)  Rows=1000.0 (1000), donald[123456]@[192.168.31.153]
  SELECT * FROM `company_info` LIMIT N, N

Count: 1  Time=0.03s (0s)  Lock=0.00s (0s)  Rows=1.0 (1), donald[123456]@[192.168.31.153]
  SHOW TABLE STATUS LIKE 'S'

Count: 1  Time=0.01s (0s)  Lock=0.00s (0s)  Rows=0.0 (0), donald[123456]@[192.168.31.153]


When there are many output results, we can use the pipeline, combined with more or grep, to find the required information:
Donald_Draper:~ # mysqldumpslow -s at al t l c -t 6 /mysqldata/Donald_Draper-slow.log | more/grep


Or use stream orientation to import the sorted results into a file for viewing:
Donald_Draper:~ # mysqldumpslow -s at al t l c -t 6 /mysqldata/Donald_Draper-slow.log > mysql-slow-log-sort.txt


     All the variables we manually set in this article have global and session scope and can take effect dynamically. What we set is global. Since these are set manually, they will take effect dynamically. When the database restarts, it will be invalid. If you don’t want to fail, you can write it in the configuration file.

[mysqld]
slow_query_log = 1
slow_query_log_file = /mysqldata/Donald_Draper-slow.log
long_query_time = 1
log_output=FILE
log-queries-not-using-indexes = 1
log_throttle_queries_not_using_indexes = 10

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270720&siteId=291194637