MySQL5.6如何优化慢查询的SQL语句--慢日志介绍_MySQL
最近有个开发团队抱怨我们平台包含的mysql cluster不行,总是报mysql的”heartbeat Error“,分析了他们收集的日志,没有发现mysql cluster节点之间的网络有问题,倒是发现了一个很有意思的现象,在mysql的slow log里面发现了大量的慢查询,基本上都是要1秒钟以上,很明显这种耗时高的慢查询会影响系统行为,指出这点让开发团队去优化他们的应用,很好奇他们竟然不知道有这些东西可以分析。。。。。。
数据库响应慢问题最多的就是查询了。现在大部分数据库都提供了性能分析的帮助手段。例如Oracle中会帮你直接找出慢的语句,并且提供优化方案。在MySQL中就要自己开启慢日志记录加以分析(记录可以保存在表或者文件中,默认是保存在文件中,我们系统使用的就是默认方式)。
先看看MySQL慢查询日志里面的记录长什么样的:
Time Id Command Argument # Time: 141010 9:33:57 # User@Host: root[root] @ localhost [] Id: 1 # Query_time: 0.000342 Lock_time: 0.000142 Rows_sent: 3 Rows_examined: 3 use test; SET timestamp=1412904837; select * from t;
这个日志应该很好理解了,第一个#记录时间戳,第二个#记录执行命令的用户和地址信息,第三个#记录执行查询的时间、锁的时间、返回行数、被扫描的行数。接着后面记录真正执行的SQL语句。还可以通过以下命令看看cvs存储格式每个字段意义。
SHOW CREATE TABLE mysql.slow_log;
接下来说说如何获取和分析慢日志吧。
查看MySQL慢日志参数
进入启动好的MySQL,执行以下命令
mysql> show variables like '%slow_query%'; +---------------------------+----------------------------------------+ | Variable_name | Value | +---------------------------+----------------------------------------+ | slow_query_log | OFF | | slow_query_log_file | /usr/local/mysql/data/cloudlu-slow.log | +---------------------------+----------------------------------------+
那么什么样的查询需要被日志呢?在MySQL中,没有index的查询以及超过指定时间同时超过指定扫描行数的查询需要记录在慢日志查询里面。
那么它们的参数又是怎么查看的呢?
没有index的查询记录开关
mysql> show global variables like '%indexes%'; +----------------------------------------+-------+ | Variable_name | Value | +----------------------------------------+-------+ | log_queries_not_using_indexes | OFF | | log_throttle_queries_not_using_indexes | 0 | +----------------------------------------+-------+
超过指定时长的查询开关
mysql> show global variables like '%long_query%'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | long_query_time | 10.000000 | +-----------------+-----------+ 1 row in set (0.00 sec)
就一个参数指定超过多少时长的查询需要被记录
超过指定行数的扫描查询开关
mysql> show variables like '%min_examined_row_limit%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | min_examined_row_limit | 0 | +------------------------+-------+ 1 row in set (0.00 sec)
设置开启MySQL慢日志参数
进入MySQL,输入以下命令或者在MySQL的启动配置文件里面修改或者给MySQL添加启动参数,进入MySQL后的修改如下:
set global long_query_time=0.1; set global log_queries_not_using_indexes=on; set global slow_query_log = on;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The logs of win10 can help users understand the system usage in detail. Many users must have encountered log 6013 when looking for their own management logs. So what does this code mean? Let’s introduce it below. What is win10 log 6013: 1. This is a normal log. The information in this log does not mean that your computer has been restarted, but it indicates how long the system has been running since the last startup. This log will appear once every day at 12 o'clock sharp. How to check how long the system has been running? You can enter systeminfo in cmd. There is one line in it.

The function is to provide engineers with feedback on usage information and records to facilitate problem analysis (used during development); because users themselves do not often generate upload logs, they are useless to users. The logging buffer is a small, temporary area used for short-term storage of change vectors for redo logs to be written to disk. A log buffer write to disk is a batch of change vectors from multiple transactions. Even so, the change vector in the log buffer is written to disk in near real-time, and when the session issues a COMMIT statement, the log buffer write operation is performed in real time.

The logs of win10 can help users understand the system usage in detail. Many users must have seen a lot of error logs when looking for their own management logs. So how to solve them? Let’s take a look below. . How to solve win10 log event 7034: 1. Click "Start" to open "Control Panel" 2. Find "Administrative Tools" 3. Click "Services" 4. Find HDZBCommServiceForV2.0, right-click "Stop Service" and change it to "Manual Start "

With the rapid development of the Internet and Web applications, log management is becoming more and more important. When developing web applications, how to find and locate problems is a very critical issue. A logging system is a very effective tool that can help us achieve these tasks. ThinkPHP6 provides a powerful logging system that can help application developers better manage and track events that occur in applications. This article will introduce how to use the logging system in ThinkPHP6 and how to utilize the logging system

iPhone lets you add medications to the Health app to track and manage the medications, vitamins and supplements you take every day. You can then log medications you've taken or skipped when you receive a notification on your device. After you log your medications, you can see how often you took or skipped them to help you track your health. In this post, we will guide you to view the log history of selected medications in the Health app on iPhone. A short guide on how to view your medication log history in the Health App: Go to the Health App>Browse>Medications>Medications>Select a Medication>Options&a

In Linux systems, you can use the following command to view the contents of the log file: tail command: The tail command is used to display the content at the end of the log file. It is a common command to view the latest log information. tail [option] [file name] Commonly used options include: -n: Specify the number of lines to be displayed, the default is 10 lines. -f: Monitor the file content in real time and automatically display the new content when the file is updated. Example: tail-n20logfile.txt#Display the last 20 lines of the logfile.txt file tail-flogfile.txt#Monitor the updated content of the logfile.txt file in real time head command: The head command is used to display the beginning of the log file

The logs of win10 have a lot of rich content. Many users must have seen the event ID455 display error when looking for their own management logs. So what does it mean? Let’s take a look below. What is event ID455 in the win10 log: 1. ID455 is the error <error> that occurred in <file> when the information store opened the log file.

Types of Linux log files and configuration methods In Linux systems, log files are very important. They record the running status of the system, user operations, and the occurrence of various events. By checking log files, system administrators can discover problems in time and handle them accordingly. This article will introduce the common types of log files in Linux systems and how to configure logging. 1. Types of log files System log: System log is a log file that records the operating status of the system, including system startup, shutdown, service startup and stop, etc.
