Home >Database >Mysql Tutorial >mysql query slow log

mysql query slow log

WBOY
WBOYOriginal
2023-05-14 10:05:375162browse

MySQL is a commonly used relational database management system with good performance and scalability in large-scale data processing. Query is one of the most frequently used operations. However, when processing large amounts of data, queries may run slowly for various reasons, affecting system performance. In this case, you need to use MySQL's slow query log to identify and optimize query performance issues. This article will introduce in detail the basic knowledge and application of MySQL slow log query.

1. What is MySQL Query Slow Log

MySQL Query Slow Log is a performance analysis tool that comes with MySQL and is used to record query requests that exceed the specified time threshold. The query slow log records various performance indicators of each query, including required time, number of executions, execution plan, etc. Through these metrics, developers can identify which queries need to be optimized for efficiency and take appropriate action.

Generally, MySQL query slow log is not enabled by default and must be manually configured to enable it. In the MySQL configuration file (my.cnf), you can set the parameters of the configuration file to enable the query slow log. The following is an example configuration for querying the slow log:

slow_query_log = 1

slow_query_log_file = /var/log/mysql/mysql-slow.log

log_queries_not_using_indexes = 1

long_query_time = 2

Among them, the slow_query_log parameter is used to enable slow query logs, the slow_query_log_file parameter specifies the location of the slow query log file, the log_queries_not_using_indexes parameter is used to log unused information of slow query indexes, and the long_query_time parameter Used to specify the query timeout, in seconds.

2. How to enable the MySQL slow query log

In MySQL 5.7 and later versions, the MySQL query slow log is enabled by default. For older versions of MySQL, such as MySQL 5.6 or 5.5, manual configuration is required to enable the query slow log function. The following are detailed steps to enable MySQL query slow log:

  1. Open the MySQL configuration file

In Linux systems, the default configuration file for MySQL is located at /etc/my.cnf . In Windows systems, the configuration file is usually located in C:Program FilesMySQLMySQL Servermy.ini or C:ProgramDataMySQLMySQL Servermy.ini. Open the file with your favorite editor.

  1. Enable slow query log

Find the following line in the configuration file:

slow_query_log = 0

Comment out the line The symbol # is removed and the value is changed to 1.

slow_query_log = 1

  1. Specify the slow log file path

Find the following line:

slow_query_log_file = /var/log/mysql /mysql-slow.log

Remove the comment symbol # from the line and change the file path to the path you want.

slow_query_log_file = /var/log/mysql/mysql-slow.log

  1. Configure query timeout

Find the following line:

long_query_time = 10

Remove the comment symbol # from this line and change the value to your desired query timeout threshold in seconds.

long_query_time = 2

  1. Configure the switch for index unused information

Find the following line:

  • log_queries_not_using_indexes = 0

Remove the comment symbol # from this line and change the value to 1 to enable unused slow query indexes in logging operations.

log_queries_not_using_indexes = 1

  1. Save and close the configuration file

Check whether the configuration file has been saved and close the file.

  1. Restart the MySQL service

Use the following command to restart the MySQL service:

sudo systemctl restart mysqld

3. How to view MySQL queries Slow log

Once you have enabled the MySQL query slow log, the query slow log will automatically record query information and store it in the specified query slow log file. You can use the following command to view the slow query log:

sudo tail -n 100 /var/log/mysql/mysql-slow.log

Use this command to display the latest 100 slow query logs Record. You can also change the number of rows displayed to your liking. In the output, the time taken to execute the slow query is displayed, along with all tables and subqueries involved in the query.

If you need to query slow logs by date filtering, you can use grep and awk to filter the logs on Linux, as shown below:

grep "21-Jun-2022" /var/log/ mysql/mysql-slow.log | awk '{print substr($2,0,length($2)-1)" "$3$4" "$5}' | less

This command will output in June 2022 All timestamps containing slow query logs on the 21st of the month.

4. How to analyze and optimize MySQL slow query logs

Once you have collected enough MySQL query slow logs, you can analyze them and find out the queries that need to be optimized. . Here are some best practices for analyzing and optimizing query performance:

  1. Parsing query slow logs with pt-query-digest

pt-query-digest is a Open source software that can help you analyze MySQL query slow logs and optimize problems that arise in them. Here are the steps to install and use pt-query-digest:

  • Install Percona Toolkit

On CentOS 7, you can install it using the following command.

yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
yum install percona-toolkit

  • Parse and query slow logs

You can use the following command to use pt-query-digest to parse and query slow logs.

pt-query-digest /var/log/mysql/mysql-slow.log > slow-query-analysis.out

  • Analysis of query slow log

pt-query-digest will provide you with a comprehensive analysis report on query slow logs. You can find the queries that occur most frequently and have the longest timeouts and identify the queries that need optimization.

  1. Using index fields

The MySQL query slow log records all queries that failed to use the index. You can use this information to determine which queries did not use the appropriate field index. to speed up the query. Adding indexes in MySQL can greatly optimize query performance. However, it must be noted that adding too many indexes may cause query performance to decrease because it may increase the overhead of query requests and update operations.

  1. Optimize query statements

The slow query log can tell you which queries need to be optimized. If the execution time exceeds the set threshold, the query will be recorded in the log file. You can check the query statement and try using different query statements to optimize execution speed.

  1. Use caching mechanism

The caching mechanism can greatly improve the query speed. If the query results already exist in the cache, the results can be returned directly from the cache, avoiding the overhead of executing the query. Caching mechanisms can be implemented using cache providers such as Redis and Memcached.

  1. Adjust MySQL server parameters

In the MySQL server, you can optimize the performance of the entire database by adjusting various parameters. These parameters include MySQL cache size, connection limit, query timeout, etc. By adjusting these parameters, database service performance can be optimized for specific queries.

Summary

The MySQL Query Slow Log is a very useful tool that can help you identify and resolve slow query performance issues. This tool is easy to start and customize, and the information it logs can help you analyze query performance and determine which queries need optimization. Following best practices and performing appropriate optimizations can improve the overall performance of your MySQL server and help you better manage and maintain large databases.

The above is the detailed content of mysql query slow log. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:mysql macforgot passwordNext article:mysql macforgot password