MySQL slow query

That's how Linux should learn 2020-11-08 23:48:05
mysql slow query


MySQL The slow query , Full name is slow query log , yes MySQL A type of logging provided , To record in MySQL Statement with response time over threshold in .
1 Concept

MySQL The slow query , Full name is slow query log , yes MySQL A type of logging provided , To record in MySQL Statement with response time over threshold in .

In a specific environment , Running time exceeds long_query_time It's worth it SQL sentence , It will be recorded in the slow query log .

long_query_time The default value is 10, It means record operation 10 Seconds or more .

By default ,MySQL The database does not start the slow query log , You need to set this parameter manually .

Of course , If it's not for tuning , It is generally not recommended to start this parameter , Because opening slow query log will bring some performance impact more or less . Slow query log supports writing log records to files and database tables .

2 Parameters

MySQL Slow query related parameter interpretation :

slow_query_log: Open slow query log or not ,1 Open for indication ,0 Means closing .

log-slow-queries : Old edition (5.6 The following versions )MySQL Database slow query log storage path . This parameter can not be set , The system will default to a default file host_name-slow.log

slow-query-log-file: new edition (5.6 And above )MySQL Database slow query log storage path . This parameter can not be set , The system will default to a default file host_name-slow.log

long_query_time: Slow query threshold , When the query time is longer than the set threshold , Log .

log_queries_not_using_indexes: Queries that are not indexed are also recorded in the slow query log ( optional ).

log_output: How to store logs .log_output='FILE' Means to save the log to a file , The default value is 'FILE'.log_output='TABLE' Indicates that the log is stored in the database .

3 To configure
3.1 slow_query_log

By default slow_query_log The value of is OFF, Indicates that slow query logs are disabled , Can be set by slow_query_log Value to open , As shown below :

mysql> 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)
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.09 sec)

Use set global slow_query_log=1 The slow query log is enabled and only works for the current database ,MySQL It will fail after restart .

If it's going to be permanent , You have to modify the configuration file my.cnf( The same is true for other system variables ).

my.cnf To add or modify parameters slow_query_log and slow_query_log_file, As shown below

slow_query_log = 1
slow_query_log_file = /tmp/mysql_slow.log

And then restart MySQL The server .

3.2 slow_query_log_file

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

mysql> 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)
3.3 long_query_time

After turning on the slow query log , Like what? SQL Will be recorded in the slow query log ?

This is by parameter long_query_time control , By default long_query_time The value of is 10 second , have access to command modify , It can also be in my.cnf Modify the parameters .

The run time is exactly equal to long_query_time The situation of , It's not going to be recorded .

in other words , stay mysql In the source code, judgment is greater than long_query_time, Not greater than or equal to .

from MySQL 5.1 Start ,long_query_time Start recording in microseconds SQL Statement run time , Previously, it was only recorded in seconds .

If it's recorded in the table , Only the integer part will be recorded , It doesn't record microseconds .

mysql> show variables like 'long_query_time%';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
mysql> set global long_query_time=4;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name | Value |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)

As shown above , I changed the variables long_query_time, But query variables long_query_time The value of the or 10, Isn't it revised to ?

Be careful : Use command  set global long_query_time=4 After modification , You need to reconnect or open a new session to see the modified values .

use show variables like 'long_query_time'  View the variable value of the current session .

It can also be done without reconnecting the session , It's about using  show global variables like 'long_query_time';.

3.4 log_output

log_output Parameter specifies how the log is stored .

log_output='FILE' Means to save the log to a file , The default value is also 'FILE'.

log_output='TABLE' Indicates that the log is stored in the database , In this way, the log information will be written to mysql.slow_log In the table .

It also supports two log storage methods , The configuration can be separated by commas , Such as :log_output='FILE,TABLE'.

The log is recorded in the special log table of the system , It takes more system resources than recording to a file .

So for the need to enable slow query logging , Also need to be able to obtain higher system performance , Then it is suggested that priority should be given to the file .

mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | FILE |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set global log_output='TABLE';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%log_output%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output | TABLE |
+---------------+-------+
1 row in set (0.00 sec)
mysql> select sleep(5) ;
+----------+
| sleep(5) |
+----------+
| 0 |
+----------+
1 row in set (5.00 sec)
mysql>
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 |
+---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+
2 rows in set (0.00 sec)
3.5 log-queries-not-using-indexes

This system variable specifies that queries that do not use indexes are also logged to the slow query log ( optional ).

If you tune , It is recommended to turn on this option .

in addition , This parameter is turned on , Actually use full index scan Of SQL It will also be recorded in the slow query log .

mysql> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | OFF |
+-------------------------------+-------+
1 row in set (0.00 sec)
mysql> set global log_queries_not_using_indexes=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'log_queries_not_using_indexes';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON |
+-------------------------------+-------+
1 row in set (0.00 sec)
3.6 log_slow_admin_statements

This system variable represents , Whether to slow management statements such as ANALYZE TABLE and ALTER TABLE And so on .

mysql> show variables like 'log_slow_admin_statements';
+---------------------------+-------+
| Variable_name | Value |
+---------------------------+-------+
| log_slow_admin_statements | OFF |
+---------------------------+-------+
1 row in set (0.00 sec)
3.7 Slow_queries

If you want to query how many slow queries there are , have access to Slow_queries System variables .

mysql> show global status like '%Slow_queries%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 2104 |
+---------------+-------+
1 row in set (0.00 sec)

in addition , also log_slow_slave_statements and --log-short-format Parameters , Accessible MySQL Website understanding .

4 mysqldumpslow Tools

In the production environment , If you want to analyze the log manually , lookup 、 analysis SQL, It's obviously individual work .

MySQL Provides log analysis tools mysqldumpslow

see mysqldumpslow Help for :

[root@DB-Server ~]# 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( sort order )
al: average lock time( Average lock time )
ar: average rows sent( Average number of returned records )
at: average query time( Average query time )
c: count( Access count )
l: lock time( Lock time )
r: rows sent( Back to the record )
t: query time( Query time )
-r reverse the sort order (largest last instead of first)
-t NUM just show the top n queries( Back to front n Data )
-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( Regular match pattern , Case insensitive )
-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

such as , Get the most returned recordset 10 individual SQL.

mysqldumpslow -s r -t 10 /database/mysql/mysql06_slow.log

The most visited 10 individual SQL

mysqldumpslow -s c -t 10 /database/mysql/mysql06_slow.log
 Get the top... In chronological order 10 There are left connected query statements in the bar .
mysqldumpslow -s t -t 10 -g “left join” /database/mysql/mysql06_slow.log

It is also recommended to use these commands in conjunction with | and more Use , Otherwise, screen swiping may occur .

mysqldumpslow -s r -t 20 /mysqldata/mysql/mysql06-slow.log | more

This paper addresses :https://www.linuxprobe.com/mysql-slow-query.html

版权声明
本文为[That's how Linux should learn]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云