Two analysis solutions for MySQL slow query _ MySQL

Source: Internet
Author: User
The following articles mainly introduce the MySQL slow query analysis method. a few days ago, I set a record to query SQL statements that are slower than 1 second in the MySQL database. There are several very well-configured methods, and the names of several parameters cannot be remembered, so the MySQL Query is repeated.

The following articles mainly introduce the MySQL slow query analysis method. a few days ago, I set a record to query SQL statements that are slower than 1 second in the MySQL database. There are several very well-configured methods, and the names of several parameters cannot be remembered, so I made a note again.

For troubleshooting and identifying performance bottlenecks, the most common problems are slow MySQL queries and queries without indexes.

OK. start to find the SQL statement that is not "refreshing" in MySQL.

MySQL slow query Analysis Method 1:

I am using this method.

MySQL and later versions support recording slow SQL statements.

 
 
  1. MySQL> show variables like 'long%';

Note: This long_query_time is used to define how many seconds are slower to calculate as "slow query"

 
 
  1. +-----------------+-----------+
  2. | Variable_name | Value |
  3. +-----------------+-----------+
  4. | long_query_time | 10.000000 |
  5. +-----------------+-----------+
  6. 1 row in set (0.00 sec)
  7. MySQL> set long_query_time=1;

Note: I set 1, that is, if the execution time exceeds 1 second, the query is slow.

 
 
  1. Query OK, 0 rows affected (0.00 sec)
  2. MySQL> show variables like 'slow%';
  3. +---------------------+---------------+
  4. | Variable_name | Value |
  5. +---------------------+---------------+
  6. | slow_launch_time | 2 |
  7. | slow_query_log | ON |

Note: whether to enable logging

 
 
  1. | slow_query_log_file | /tmp/slow.log |

Note: where to set

 
 
  1. +---------------------+---------------+
  2. 3 rows in set (0.00 sec)
  3. MySQL> set global slow_query_log='ON'

Note: Enable logging

Once the slow_query_log variable is set to ON, MySQL starts recording immediately.

In/etc/my. cnf, you can set the initial values of the above MySQL global variables.

 
 
  1. long_query_time=1
  2. slow_query_log_file=/tmp/slow.log

MySQL slow query Analysis Method 2:

MySQLdumpslow command

 
 
  1. /path/MySQLdumpslow -s c -t 10 /tmp/slow-log

This will output 10 SQL statements with the maximum number of records, of which:

-S indicates the sorting method. c, t, l, and r are sorted by the number of records, time, query time, and number of returned Records, ac, at, al, and ar indicate reverse descriptions;

-T indicates the top n, that is, the number of previous data records returned;

-G, followed by a regular expression matching mode, which is case insensitive;

For example

 
 
  1. /path/MySQLdumpslow -s r -t 10 /tmp/slow-log

You can obtain up to 10 queries from the returned record set.

 
 
  1. /path/MySQLdumpslow -s t -t 10 -g “left join” /tmp/slow-log

Obtain the query statements containing the left join in the first 10 results sorted by time.

The above content is an introduction to MySQL slow query analysis. I hope you will get some benefits.

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.