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

MySQL Slow Query Log

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.

  1. Enable MySQL Slow Query Log:

    • Description: The Slow Query Log records queries that exceed a predefined execution time.
    • Enable in my.cnf or my.ini:
      [mysqld]
      slow_query_log = 1
      slow_query_log_file = /path/to/slow-query.log
      long_query_time = 1  # Time in seconds
      
    • Restart the MySQL server.
  2. Viewing Slow Query Log in MySQL:

    • Description: Use various methods to view the contents of the MySQL Slow Query Log.
    • Command to view the last few lines (Linux):
      tail /path/to/slow-query.log
      
    • Command to view the entire log:
      cat /path/to/slow-query.log
      
  3. MySQL Slow Query Log location:

    • Description: The location of the Slow Query Log depends on the configuration in my.cnf or my.ini.
    • Default location (Linux):
      /var/log/mysql/slow-query.log
      
  4. Configure slow query threshold in MySQL:

    • Description: Set the threshold time to determine which queries are considered slow.
    • Example (Set threshold to 2 seconds):
      [mysqld]
      slow_query_log = 1
      slow_query_log_file = /path/to/slow-query.log
      long_query_time = 2
      
    • Restart the MySQL server.
  5. Disable Slow Query Log in MySQL:

    • Description: Disable the MySQL Slow Query Log when it's no longer needed.
    • Disable in my.cnf or my.ini:
      [mysqld]
      slow_query_log = 0
      
  6. Rotating and purging Slow Query Logs:

    • Description: Rotate and purge old Slow Query Logs to manage disk space.
    • Use the following SQL command to rotate logs:
      FLUSH LOGS;
      
    • To purge old logs, you can use the PURGE BINARY LOGS command:
      PURGE BINARY LOGS TO 'mysql-bin.000003';