MySQL database slow query log configuration and analysis

1. What is a slow query log?

  The official understanding of the slow query log is too laborious. This blogger, let’s explain it according to the daily thinking. The slow query log, as the name suggests, is to query the slow log records. We add, delete, modify, check and other operations in the database. After the time exceeds the default query time set by the slow query in the database, these slow-executing SQL will be recorded in the log, such a log is called the slow query log. That being said, it should be easier for everyone to understand, haha.

2. What parameters are there for slow query?

  Explanation of parameters related to MySQL slow query: slow_query_log : whether to enable slow query log, on means open, 0ff means close

  1、slow_query_log

     是否开启慢查询日志,1表示开启,0表示关闭。

  2、log-slow-queries
     旧版(5.6以下版本)MySQL数据库慢查询日志存储路径。可以不设置该参数,系统则会默认给一个缺省的文件host_name-slow.log
  3、slow-query-log-file
     新版(5.6及以上版本)MySQL数据库慢查询日志存储路径。可以不设置该参数,系统则会默认给一个缺省的文件host_name-slow.log
  4、long_query_time
     慢查询阈值,当查询时间多于设定的阈值时,记录日志。一般设置成1s或者2s
  5、log_queries_not_using_indexes
     未使用索引的查询也被记录到慢查询日志中(可选项)。
  6、log_output:
    日志存储方式。
    (1)log_output='FILE'表示将日志存入文件,默认值是'FILE'
    (2)log_output='TABLE'表示将日志存入数据库,这样日志信息就会被写入到mysql.slow_log表中。
    (3)MySQL数据库支持同时两种日志存储方式,配置的时候以逗号隔开即可,如:log_output='FILE,TABLE'
    (4)日志记录到系统的专用日志表中,要比记录到文件耗费更多的系统资源,因此对于需要启用慢查询日志,又需要能够获得更高的系统性能,那么建议优先记录到文件。

3. Where to check the slow query log?

  1. First of all, we need to check whether the slow query is enabled in the configuration in the database and where is the slow query log output, otherwise the ghost will know where it is? The specific viewing method is as follows:

    (1) Log in to the mysql database, mysql -uroot -p -hxxxx 

    (2) Check whether slow query is enabled:

       By default, the value of slow_query_log is OFF, which means that the slow query log is disabled. It can be enabled by setting the value of slow_query_log, as shown below:

mysql> show variables like '%slow_query_log%';
+---------------------+------------------------------------------+
| Variable_name       | Value                                    |
+---------------------+------------------------------------------+
| slow_query_log      | OFF                                      |
| slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
+---------------------+------------------------------------------+
2 rows in set (0.00 sec)
 
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show variables like '%slow_query_log%';
+---------------------+------------------------------------------+
| Variable_name       | Value                                    |
+---------------------+------------------------------------------+
| slow_query_log      | ON                                       |
| slow_query_log_file | /usr/local/mysql/data/localhost-slow.log |
+---------------------+------------------------------------------+
2 rows in set (0.00 sec)
#Use set global slow_query_log=1 to enable the slow query log, which only takes effect on the current database, and will become invalid after MySQL restarts. If you want to take effect permanently, you must modify the configuration file my.cnf (the same is true for other system variables). We add the following operations in my.cnf

    (3) Enable slow query and set the storage method:

      slow_query_log =1

      log_output = file    

    (4) Customize the path of slow query storage

      slow_query_log_file=/data/mysql/mysql-slow.log

    (5) Customize the configuration of slow query request time to 1s

       long_query_time = 1

    (6) After restarting the mysql server, check whether the parameters are set successfully

         /etc/init.d/mysqld restart

        You can view it using the following methods:

         show variables like 'parameter name';

 Fourth, use mysqldumpslow to analyze slow query logs

  

      

       

Guess you like

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