Use MySQL Slow Log to solve the high CPU usage problem of MySQL

Source: Internet
Author: User
Tags high cpu usage

Using MySQL Slow Log to solve the problem of high CPU usage in MySQL sometimes finds that MySQL occupies a high CPU usage in Linux VPS, resulting in a high system load. This is probably because the execution time of an SQL statement is too long. Optimizing this SQL statement or optimizing the index of a table referenced by this SQL statement can solve the problem. But how can we find out which SQL statement has been executed for a long time? You can refer to the MySQL Slow Log for details. First find the MySQL configuration file my. cnf: the configuration for enabling slow query based on mysql of different versions is different. mysql 5.0 [mysqld] long_query_time = 1log-slow-queries =/var/log/mysql/slow. log mysql 5.1 [mysqld] long_query_time = 1slow_query_log = 1slow_query_log_file =/var/log/mysql/slow. log long_query_time indicates how long the SQL statement will be logged down after execution, which is 1 second. Log-slow-queries and slow_query_log_file set where to write the log and open the above parameter. After a period of operation, the log can be turned off, saving the trouble of affecting the production environment. The next step is to analyze the log, the file name here is/var/log/mysql/slow. log. In mysqldumpslow-help, we mainly use-s ORDER what to sort by (t, at, l, al, r, ar etc ), 'at' is default-t NUM just show the top n queries-g PATTERN grep: only consider orders ts that include this string-s, which is the order, indicating that the write is not detailed enough, c, t, l, r, ac, at, al, ar are sorted by the number of queries, time, lock time, and number of returned records, the time-reverse-t of a is added to the front, which is the meaning of top n, that is, the number of data-g in the front is returned, and a regular match mode can be written to the back, case Insensitive mysqldumpslow-s c-t 20/var/log/mysql/slow. logmysqldumpslow-s r-t 20 The preceding command/var/log/mysql/slow. log shows the 20 most frequently accessed SQL statements and the 20 most returned SQL statements in the record set. Mysqldumpslow-t 10-s t-g "left join"/var/log/mysql/slow. log: return the first 10 SQL statements containing left connections by time. With this tool, you can find out which SQL statements are performance bottlenecks and optimize them, such as adding indexes and implementing the application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.