Mysql slow query analysis

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags analysis find find out index mysql mysql database mysql tutorial name

This article mainly introduces the MySQL slow query analysis method, some time ago, I have set a record in the MySQL database to query SQL statements slower than 1 second. Think of a few very set method, there are several parameters of the name of life and death memories can not afford, so re-sort it yourself as a note.

For troubleshooting bottlenecks to find out the performance, the most easy to find and solve the problem is MySQL slow query and did not have to use the index query.

OK, began to find out MySQL implementation is not "cool" SQL statement.

MySQL slow query analysis method one:

This method I am using, huh, huh, I like this kind of real-time.

MySQL5.0 or later version can support the implementation of slower SQL statements recorded.

MySQL> show variables like 'long%';

Note: The long_query_time is used to define how many seconds slower than "slow query"

+ ----------------- + ----------- +
| Variable_name | Value |
+ ----------------- + ----------- +
long_query_time | 10.000000 |
+ ----------------- + ----------- +
1 row in set (0.00 sec)
MySQL> set long_query_time = 1;

Note: I set 1, that is, the execution time of more than 1 second are slow query.

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

Note: Whether to open the log records

slow_query_log_file | /tmp/slow.log |

Note: Set to what position

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

Note: Turn on logging

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

/etc/my.cnf Which can set the initial value of the above MySQL global variables.

long_query_time = 1 slow_query_log_file = / tmp / slow.log

MySQL slow query analysis method two:

MySQLdumpslow command

/ path / MySQLdumpslow -sc -t 10 / tmp / slow-log

This will output the most recorded 10 SQL statements, of which:

-s, is that in accordance with the sort, c, t, l, r, respectively, according to the number of records, time, query time, the number of records returned to sort ac, at, al, ar, that the corresponding flashback;

-t, is the meaning of top n, that is, to return to the front of the number of data;

-g, behind you can write a regular matching pattern, case insensitive;

such as

/ path / MySQLdumpslow -sr -t 10 / tmp / slow-log

Get back to the record set up to 10 queries.

/ path / MySQLdumpslow -st -t 10 -g "left join" / tmp / slow-log

Get sorted by time the top 10 which contains the left join the query.

The relevant content is the introduction of slow query analysis of MySQL, hope you can gain something.

Related Article

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.