热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

LoggingallMySQLqueriesintotheSlowLog

MySQLoptionallylogsslowqueriesintotheSlowQueryLog–orjustSlowLog,asfriendscallit.However,Thereareseveralreasonstologallqueries.Thislistisnotexhaustive:Belowyoucanfindthevariablestochange,astheyshouldbewritteninth


Logging all MySQL queries into the Slow Log

Is this road’s traffic logged somehow?


MySQL optionally logs slow queries into the Slow Query Log – or just Slow Log, as friends call it. However, there are good reasons to log all the queries , not just some of them. This article shows how to log all available information into the Slow Log.



Table of Contents


1 Why can’t I only log the slowest queries?


2 How to apply the changes


3 Setting the Slow Log


4 Is the Slow Log slow?



Why can’t I only log the slowest queries?


There are several reasons to log all queries. This list is not exhaustive:



  • If you have a locking query that takes 0.5 seconds and runs 10 per second, most probably it’s much more relevant than a query that takes 1 second and is executed once per second.

  • Threshold are not smart. Put a 5 seconds threshold, and you will never know that a very frequent query takes 4.9 seconds.

  • Suppose you only log queries that take 5 or more seconds. If a query takes less than 1 second more of the times and ore than 5 seconds in rare cases (which typically depends on data distribution) you will lose this information.

  • If you don’t log relatively fast queries, you cannot analyse your workload and see, for example, that many locking statements insist on the same table.

  • You want to see and removeuseless queries.

  • For some queries it is perfectly ok to take a long time (one-time queries, cached queries, analytics…). This makes any time threshold not much significant.


How to apply the changes


Below you can find the variables to change, as they should be written in the configuration file (most probably /etc/mysql/my.cnf or /etc/my.cnf ). Note that changes in the configuration file will take effect on MySQL restart .


You avoid restarting MySQL, but I recommend to make the changes to the configuration file anyway, in case it crashes.


To change variables values at runtime:


SET GLOBAL variable_name := 1;
SET GLOBAL variable_name := 'value'; # string values

This only takes effect for new connections . You are not logging all queries until all existing connections are closed and new ones are established.


Setting the Slow Log


Here you can find the correct setting to use to log all your queries, depending on which MySQL flavour you use.


MySQL


8.0


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
log_throttle_queries_not_using_indexes = 0
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

# information in the slow log
log_slow_extra = 1

5.7


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
log_throttle_queries_not_using_indexes = 0
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

5.6


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
log_throttle_queries_not_using_indexes = 0
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

Percona Server


8.0


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
log_throttle_queries_not_using_indexes = 0
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
slow_query_log_use_global_cOntrol= all

# information in the slow log
log_slow_extra = 1
log_slow_verbosity = full

5.7


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
log_throttle_queries_not_using_indexes = 0
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
slow_query_log_use_global_cOntrol= all

5.6


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
log_throttle_queries_not_using_indexes = 0
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
slow_query_log_use_global_cOntrol= all

MariaDB


10.5


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

# information in the slow log
log_slow_verbosity = query_plan,innodb,explain

10.4


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

# information in the slow log
log_slow_verbosity = query_plan,innodb,explain

10.3


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

# information in the slow log
log_slow_verbosity = query_plan,innodb,explain

10.2


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

# information in the slow log
log_slow_verbosity = query_plan,innodb,explain

10.1


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1

# information in the slow log
log_slow_verbosity = query_plan,innodb,explain

10.0


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1

# information in the slow log
log_slow_verbosity = query_plan,innodb,explain

5.5 and previous versions


# log queries into a log.slow file
slow_query_log = 1
log_output = FILE
slow_query_log_file = log.slow

# log all queries
long_query_time = 0
min_examined_row_limit = 0
log_queries_not_using_indexes = 1

Is the Slow Log slow?


In most cases, the slow log performance impact is minimum. It may sound scary to think that MySQL will write something into a file for every single statement; but not if you know the amount of IO that is required for every single statement (undo log, redo log, binlog, potentially reads from tablespaces) and InnoDB background threads. And the slow log is just a sequential write-only file.


That said, if your IO is nearly saturated, MariaDB allows to throttle the slow log . Which means, multiple queries will be written to the file together, to reduce the IO operations. Take a look at log_slow_rate_limit .


See also


Related courses



  • MySQL Administration Essentials


Reference



  • The Slow Query Log , from MySQL documentation

  • Slow Query Log , from Percona Server documentation

  • Slow Query Log Overview , from MariaDB KnowledgeBase

  • Proactive MySQL: Query Reviews , by Sheeri Cabral


Conclusions


We discussed why you should log all your queries and all available information about them. We saw exactly how to do it, depending on your MySQL or Percona Server or MariaDB version.


This is something that I check at the beginning of myMySQL Health Checks. This is because I include recommendations so optimise the most impacting queries, so I need to be sure to have complete query statistics.


If you spot any mistake, or if you have more ideas on how to get the most from the Slow Log, please drop a comment below.


Remember: your comments are valuable and welcome !


Toodle pip,
Federico


Photo credit




以上所述就是小编给大家介绍的《Logging all MySQL queries into the Slow Log》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 我们 的支持!




推荐阅读
  • 探讨 HDU 1536 题目,即 S-Nim 游戏的博弈策略。通过 SG 函数分析游戏胜负的关键,并介绍如何编程实现解决方案。 ... [详细]
  • 在编译BSP包过程中,遇到了一个与 'gets' 函数相关的编译错误。该问题通常发生在较新的编译环境中,由于 'gets' 函数已被弃用并视为安全漏洞。本文将详细介绍如何通过修改源代码和配置文件来解决这一问题。 ... [详细]
  • YB02 防水车载GPS追踪器
    YB02防水车载GPS追踪器由Yuebiz科技有限公司设计生产,适用于车辆防盗、车队管理和实时追踪等多种场合。 ... [详细]
  • CSS高级技巧:动态高亮当前页面导航
    本文介绍了如何使用CSS实现网站导航栏中当前页面的高亮显示,提升用户体验。通过为每个页面的body元素添加特定ID,并结合导航项的类名,可以轻松实现这一功能。 ... [详细]
  • 深入解析Spring启动过程
    本文详细介绍了Spring框架的启动流程,帮助开发者理解其内部机制。通过具体示例和代码片段,解释了Bean定义、工厂类、读取器以及条件评估等关键概念,使读者能够更全面地掌握Spring的初始化过程。 ... [详细]
  • 深入解析 Android IPC 中的 Messenger 机制
    本文详细介绍了 Android 中基于消息传递的进程间通信(IPC)机制——Messenger。通过实例和源码分析,帮助开发者更好地理解和使用这一高效的通信工具。 ... [详细]
  • 探讨ChatGPT在法律和版权方面的潜在风险及影响,分析其作为内容创造工具的合法性和合规性。 ... [详细]
  • 优化SQL Server批量数据插入存储过程的实现
    本文介绍了一种改进的SQL Server存储过程,用于生成批量插入语句。该方法不仅提高了性能,还支持单行和多行模式,适用于SQL Server 2005及以上版本。 ... [详细]
  • 本题要求在一组数中反复取出两个数相加,并将结果放回数组中,最终求出最小的总加法代价。这是一个经典的哈夫曼编码问题,利用贪心算法可以有效地解决。 ... [详细]
  • 本文探讨了如何利用HTML5和JavaScript在浏览器中进行本地文件的读取和写入操作,并介绍了获取本地文件路径的方法。HTML5提供了一系列API,使得这些操作变得更加简便和安全。 ... [详细]
  • 在寻找轻量级Ruby Web框架的过程中,您可能会遇到Sinatra和Ramaze。两者都以简洁、轻便著称,但它们之间存在一些关键区别。本文将探讨这些差异,并提供详细的分析,帮助您做出最佳选择。 ... [详细]
  • 掌握Mosek矩阵运算,轻松应对优化挑战
    本篇文章继续深入探讨Mosek学习笔记系列,特别是矩阵运算部分,这对于优化问题的解决至关重要。通过本文,您将了解到如何高效地使用Mosek进行矩阵初始化、线性代数运算及约束域的设定。 ... [详细]
  • 在 Android 开发中,通过 Intent 启动 Activity 或 Service 时,可以使用 putExtra 方法传递数据。接收方可以通过 getIntent().getExtras() 获取这些数据。本文将介绍如何使用 RoboGuice 框架简化这一过程,特别是 @InjectExtra 注解的使用。 ... [详细]
  • 本文介绍如何从字符串中移除大写、小写、特殊、数字和非数字字符,并提供了多种编程语言的实现示例。 ... [详细]
  • KMP算法是处理字符串匹配的一种高效算法它首先用O(m)的时间对模板进行预处理,然后用O(n)的时间完成匹配。从渐进的意义上说,这样时间复 ... [详细]
author-avatar
卜弃miao_286
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有