MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The MySQL Slow Query Log is a log file where the MySQL server records queries that took longer than a certain amount of time to execute. This log is useful for finding queries that might need optimization.
Here's a brief tutorial on how to configure and use the MySQL Slow Query Log.
Configuring the Slow Query Log
The Slow Query Log is not enabled by default. You can enable it by setting the slow_query_log
variable to ON
and specifying a file to store the log data in the MySQL configuration file (my.cnf or my.ini).
Here's an example of how to configure the Slow Query Log:
[mysqld] slow_query_log = ON slow_query_log_file = /var/log/mysql/mysql-slow.log long_query_time = 2
In this example, MySQL will write slow query logs to /var/log/mysql/mysql-slow.log
and consider any query that takes longer than 2 seconds as a slow query. You can replace these paths and values with the desired ones on your system.
Viewing the Slow Query Log
To view the slow query log, you can use any text editor or command-line tools like 'cat', 'less', or 'tail':
tail /var/log/mysql/mysql-slow.log
This command will show the last few lines of the slow query log.
Interpreting the Slow Query Log
Here's an example of what a slow query log entry might look like:
# Time: 2023-05-12T10:41:29.123456Z # User@Host: root[root] @ localhost [] # Thread_id: 12 Schema: test QC_hit: No # Query_time: 2.123456 Lock_time: 0.000040 Rows_sent: 1 Rows_examined: 10000 # Rows_affected: 0 # Full_scan: Yes Full_join: No SET timestamp=1678908889; SELECT * FROM test_table WHERE column1 = 'value';
In this example, you can see the timestamp of the query (2023-05-12T10:41:29.123456Z
), the user who executed the query (root@localhost
), the thread ID (12
), the database (test
), whether the query cache was hit (QC_hit: No
), the time it took to execute the query (Query_time: 2.123456
), the number of rows sent and examined, and the query itself.
Managing the Slow Query Log
The Slow Query Log can grow quite large on a busy server, so it's important to implement log rotation to manage its size. Many systems use the 'logrotate' utility for this purpose.
Additionally, slow queries can impact database performance, so it's a good practice to regularly review the slow query log and optimize any slow queries.
Note: Modifying the MySQL configuration file and viewing the log file typically require administrative privileges. Be careful when modifying configuration files and always back up any files before you modify them.
Enable MySQL Slow Query Log:
[mysqld] slow_query_log = 1 slow_query_log_file = /path/to/slow-query.log long_query_time = 1 # Time in seconds
Viewing Slow Query Log in MySQL:
tail /path/to/slow-query.log
cat /path/to/slow-query.log
MySQL Slow Query Log location:
/var/log/mysql/slow-query.log
Configure slow query threshold in MySQL:
[mysqld] slow_query_log = 1 slow_query_log_file = /path/to/slow-query.log long_query_time = 2
Disable Slow Query Log in MySQL:
[mysqld] slow_query_log = 0
Rotating and purging Slow Query Logs:
FLUSH LOGS;
PURGE BINARY LOGS
command:PURGE BINARY LOGS TO 'mysql-bin.000003';