Interviewer: Since you have a deep understanding of Mysql, how do you analyze the binary log and slow query log?

table of Contents

            What is the binary log

            What is slow query log

          Preface

          Enable slow query log

          Mysql slow query related parameter explanation

          Mysql slow query analysis tool

          Common slow query optimization

            to sum up

What is the binary log

1) The details about the binary log are recorded in the link below

           ~~~~>   Binary log details

What is slow query log

Preface

         MySQL slow query log is a log provided by MySQL, which is used to record the response time exceeds the threshold value statements in MySQL, refers specifically to run longer than the SQL long_query_time value, will be recorded to the slow query log. The default value of long_query_time is 10, which means to run the statement more than 10S. By default, Mysql database does not start the slow query log. We need to manually set this parameter. Of course, if it is not needed for tuning, it is generally not recommended to enable this parameter, because opening the slow query log will more or less bring some The performance impact. The slow query log supports writing log records to files and also supports writing log records to database tables.

 Enable slow query log

1) Download the database

[root@hya ~]# yum -y install mariadb mariadb-server

2) Start slow query of configuration file

[root@hya ~]# vim /etc/my.cnf
slow_query_log=1     #开启慢查询
long_query_time=1    #设置时间 
slow_query_log_file=/var/log/mariadb/mysql-slow.log    #设置日志存放路径
######################################第二种方法 临时开启
MariaDB [(none)]> set  global  slow_query_log='ON';
MariaDB [(none)]> set long_query_time=1;
MariaDB [(none)]> set  slow_query_log_file='/var/log/mariadb/mysql-slow.log';

Mysql slow query related parameter explanation

1) Parameter explanation

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

log-slow-queries :旧版(5.6以下版本)MySQL数据库慢查询日志存储路径。可以不设置该参数,系统则会默认给一个缺省的文件host_name-slow.log

slow-query-log-file:新版(5.6及以上版本)MySQL数据库慢查询日志存储路径。可以不设置该参数,系统则会默认给一个缺省的文件host_name-slow.log

long_query_time :慢查询阈值,当查询时间多于设定的阈值时,记录日志。

log_queries_not_using_indexes:未使用索引的查询也被记录到慢查询日志中(可选项)。

log_output:日志存储方式。log_output='FILE'表示将日志存入文件,默认值是'FILE'。log_output='TABLE'表示将日志存入数据库,这样日志信息就会被写入到mysql

2) Query details

MariaDB [(none)]> show variables like 'slow_query%';  #是否开启慢查询
+---------------------+---------------------------------+
| Variable_name       | Value                           |
+---------------------+---------------------------------+
| slow_query_log      | ON                              |
| slow_query_log_file | /var/log/mariadb/mysql-slow.log |
+---------------------+---------------------------------+
MariaDB [(none)]> show variables like 'long_query_time';  #查看查询时间
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
MariaDB [(none)]> show variables like 'log_queries_not_using_indexes'; #查看未使用索引的
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF   |
+-------------------------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> set global log_queries_not_using_indexes=1;  #设置未使用索引查询的也被记录到慢查询日志中
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show global status like '%slow_queries%';   #查询慢查询记录
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries  | 1     |
+---------------+-------+
1 row in set (0.00 sec)

3) View the maximum number of database connections and settings

MariaDB [(none)]> show variables like '%max_connections%'; #上限连接数
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| extra_max_connections | 1     |
| max_connections       | 151   |
+-----------------------+-------+
2 rows in set (0.00 sec)
MariaDB [(none)]> SHOW GLOBAL STATUS LIKE 'Max_used_connections';   #服务器响应最大连接数
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| Max_used_connections | 1     |
+----------------------+-------+
1 row in set (0.00 sec)
比较理想的设置:Max_used_connections / max_connections * 100% ≈ 85%
最大连接数占上限连接数的85%左右,如果发现比例在10%以下,MySQL服务器连接数上限设置的过高了。
#############################################################################
MariaDB [(none)]> set GLOBAL max_connections = 500;   #修改最大连接数
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GLOBAL STATUS;  #列出MySQL服务器运行各种状态值

Mysql slow query analysis tool

There are a lot of slow query log records. It is not easy to find a slow query log from it. Generally speaking, some tools are needed to quickly locate the SQL statement that needs to be optimized. The following describes the mysqldumpslow slow query auxiliary tool:

The tools in the bin directory summarize the same SQL except for the query conditions, and output the analysis results in the order specified in the parameters.

1) Analyze the slow query log

[root@hya ~]# mysqldumpslow -s r -t 10  /var/log/mariadb/mysql-slow.log 

Reading mysql slow query log from /var/log/mariadb/mysql-slow.log
Count: 1  Time=10.00s (10s)  Lock=0.00s (0s)  Rows_sent=1.0 (1), Rows_examined=0.0 (0), root[root]@localhost
  select sleep(N)

Died at /usr/bin/mysqldumpslow line 178, <> chunk 1.
#########################################################################################
-s order (c,t,l,r,at,al,ar)
c:总次数
t:总时间
l:锁的时间
r:总数据行
at,al,ar :t,l,r平均数 【例如:at = 总时间/总次数】

-t 指定取前面几天作为结果输出

Common slow query optimization

1、索引没起到作用的情况 
 - 使用LIKE关键字的查询语句        在使用LIKE关键字进行查询的查询语句中,如果匹配字符串的第一个字符为“%”,索引不会起作用。只有“%”不在第一个位置索引才会起作用。
 - 使用多列索引的查询语句        MySQL可以为多个字段创建索引。一个索引最多可以包括16个字段。对于多列索引,只有查询条件使用了这些字段中的第一个字段时,索引才会被使用。
2、优化数据库结构
 - 将字段很多的表拆解成多个表
 - 设置中间表
3、分解关联查询
   很多高性能的应用都会对关联查询进行分解,就是可以对每一个表进行一次单表查询,然后将查询结果在应用程序中进行关联,很多场景下这样会更高效。
4、优化limit分页
  在系统中需要分页的操作通常会使用limit加上偏移量的方法实现,同时加上合适的order by 子句。如果有对应的索引,通常效率会不错,否则MySQL需要做大量的文件排序操作。
    一个非常令人头疼问题就是当偏移量非常大的时候,例如可能是limit 10000,20这样的查询,这是mysql需要查询10020条然后只返回最后20条,前面的10000条记录都将被舍弃,这样的代价很高。
    优化此类查询的一个最简单的方法是尽可能的使用索引覆盖扫描,而不是查询所有的列。然后根据需要做一次关联操作再返回所需的列。对于偏移量很大的时候这样做的效率会得到很大提升。

to sum up

          The slow query log is not enabled by default, which means that most people have never played this function. If you need to optimize SQL statements, you can turn on this function. It allows you to easily know which statements need to be optimized. As for the optimization of slow queries, I have actually optimized them, and I happened to be asked about it. This piece is sorted out so that I can look back at it.

Guess you like

Origin blog.csdn.net/yeyslspi59/article/details/109097433