How to enable and view the MySQL slow query log?
Jun 12, 2025 am 10:38 AMTo enable MySQL slow query logs, you need to modify the configuration file and set relevant parameters. 1. Add slow_query_log = 1 to enable logging in the [mysqld] part of my.cnf or my.ini; 2. Set slow_query_log_file to specify the log path, such as /var/log/mysql/mysql-slow.log; 3. Define the slow query threshold through long_query_time, the default unit is seconds. If set to 1, it means to record queries that have exceeded 1 second; 4. Restart MySQL after modification or use the SET GLOBAL command to take effect dynamically; 5. The log is in text format by default, and can be viewed by tail, cat and other commands. The entry contains key information such as execution time, lock waiting time, return row count and scan row count; 6. Notes include checking write permissions, reconnecting to ensure that parameters take effect, optionally configuring log_queries_not_using_indexes to log queries that are not used with indexes, and temporarily switch the logging function through SQL commands. After correct configuration, SQL can be effectively positioned.
MySQL's Slow Query Log is a powerful tool to troubleshoot performance problems, especially when you find that the database response is slow, enabling this log can help you find SQL that "dragged back". Let’s talk about how to operate it.
Enable MySQL slow query logs
To enable slow query logs, you need to modify the MySQL configuration file (usually my.cnf
or my.ini
), add or modify the following in the [mysqld]
section:
slow_query_log = 1 slow_query_log_file = /var/log/mysql/mysql-slow.log long_query_time = 1
-
slow_query_log = 1
means that slow query log is enabled. -
slow_query_log_file
is the location where the log is saved, and the path needs to be ensured that MySQL has write permissions. -
long_query_time
is the standard for defining "slow", and the default unit is seconds. For example, setting to 1 means that queries with execution time exceeding 1 second will be recorded.
After the modification is completed, restart the MySQL service or use the command to take effect dynamically:
SET GLOBAL slow_query_log = 'ON'; SET GLOBAL long_query_time = 1; -- Setting the file path requires restart to take effect
Note: In some systems, after using
SET GLOBAL
to modifylong_query_time
, the currently connected session may not take effect immediately. You can reconnect and then test it.
View slow query log content
The log format is text by default, and you can directly view it with tail
, cat
and other commands:
tail -f /var/log/mysql/mysql-slow.log
A typical log entry is as follows:
# Time: 2024-03-25T14:30:45.123456Z # User@Host: root[root] @ localhost [] # Query_time: 2.123456 Lock_time: 0.000123 Rows_sent: 100 Rows_examined: 10000 SET timestamp=1711373445; SELECT * FROM orders WHERE customer_id = 123;
From this you can see:
- Execution time (Query_time)
- Lock waiting time (Lock_time)
- Returns the number of rows (Rows_sent)
- Number of scan lines (Rows_examined)
This information is very useful for analyzing SQL performance.
Frequently Asked Questions and Notes
The log file was not generated?
Checks whether MySQL has permission to write to the target path, or whether slow query logs are enabled but no queries that meet the criteria are triggered.Set long_query_time but no effect?
It may be because it is set but not reconnected, or the value is too small, so that too many logs are ignored.Don't want to record indexed queries?
This configuration can be added to filter:log_queries_not_using_indexes
In this way, those queries that do not use the index will also be recorded.
Temporary switch slow query?
If you just want to open it temporarily, you can use SQL commands to control it:SET GLOBAL slow_query_log = 'OFF'; -- Close SET GLOBAL slow_query_log = 'ON'; -- Turn on
Basically that's it. Enabling slow query logs is not complicated, but it is indeed easy to ignore some details, such as path permissions, effectiveness methods, etc. As long as the configuration is correct, it can help you quickly locate SQL that affects performance.
The above is the detailed content of How to enable and view the MySQL slow query log?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

How to use MySQL's slow query log to locate performance bottlenecks Performance bottlenecks are one of the problems that database applications often face, and the slow query log function provided by MySQL can help us find slow query statements and then locate performance bottlenecks. This article will introduce how to use MySQL's slow query log to locate performance bottlenecks and provide corresponding code examples. 1. Enable slow query log To use the slow query log function, you first need to enable the corresponding configuration option. Open the MySQL configuration file (usually my.ini or my.cn

The main steps to optimize SQL using slow query logs: 1. Turn on the slow query log and set the execution time threshold (for example, modify the my.cnf file in MySQL); 2. Analyze the slow query log and pay attention to execution time, SQL statements and additional information (such as execution plan); 3. Find performance bottlenecks based on the log information, such as missing indexes; 4. Take optimization measures, such as adding indexes (CREATEINDEX statements) or optimizing the SQL statement itself; 5. Combining database monitoring tools and business logic comprehensive analysis, continuously monitor and optimize database performance. Ultimately, we will achieve the goal of improving database efficiency.

With the increase in data volume and application complexity, database performance has become an increasingly important issue. As a popular relational database management system, MySQL also provides many tools and methods for optimizing performance. Among them, using slow query logs to optimize MySQL performance is a very practical method. This article will introduce how to use MySQL's slow query log to optimize performance. 1. What is the slow query log? The slow query log is a logging mechanism in MySQL. It records that the execution time exceeds a certain

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.

To enable MySQL slow query logs, you need to modify the configuration file and set relevant parameters. 1. Add slow_query_log=1 to enable logging in the [mysqld] part of my.cnf or my.ini; 2. Set slow_query_log_file to specify the log path, such as /var/log/mysql/mysql-slow.log; 3. Define the slow query threshold through long_query_time, the default unit is seconds. If set to 1, it means to record queries that exceed 1 second; 4. After modification, restart MySQL or use the SETGLOBAL command to take effect dynamically; 5. The log is in text format by default, and you can use tail, cat and other commands to view it.

MySQL slow query log is used to locate database performance bottlenecks. By checking and turning on slow query logs (slow_query_log=1), setting the log file path and query time threshold (long_query_time), recording the execution time-consuming SQL. When analyzing the content of the log, you need to pay attention to information such as query time, number of scanned rows, and number of returned rows. Common problems include the lack of indexes that lead to full table scanning, unnecessary sorting or grouping, and unreasonable association queries. The optimization suggestions are: 1. Use EXPLAIN to analyze the execution plan and add appropriate indexes; 2. Ensure that the sorted fields have indexes and avoid depth paging; 3. Ensure that the connected fields are indexed and simplify the JOIN logic. You can use mysqldumpslow
