Mysql slow query-mysql tutorial

Source: Internet
Author: User
Tags mysql tutorial
1. the definition of slow query refers to all SQL statements that have been executed beyond the time threshold set by long_query_time. Slow query logs are the logs that record these SQL statements. 2 enable slow query log find mysql configuration file my. cnf. add log-slow-queriesD: MySQLlogmysqld-slow-query.log # log existing

1. the definition of slow query refers to all SQL statements that have been executed beyond the time threshold set by long_query_time. Slow query logs are the logs that record these SQL statements. 2 enable slow query log find mysql configuration file my. cnf. add log-slow-queries = D:/MySQL/log/mysqld-slow-query.log # log existing

1. definition of slow query
Mysql records all SQL statements that have been executed beyond the time threshold set by the long_query_time parameter. Slow query logs are the logs that record these SQL statements.

2. enable slow query logs
Find the mysql configuration file my. cnf. add it under mysqld


Log-slow-queries = D:/MySQL/log/mysqld-slow-query.log # location where the log exists. (Pay attention to permission issues, you can not set, the system will give a default file host_name-slow.log)
Long-query-time = 5 # SQL execution time threshold. the default value is 10 seconds.
# Log-long-format # The log-long-format option is used to set the log format. it records related events in extended mode. Extended Mode can record who sends the query and when the query is sent. This allows us to better understand the operations of the client.
# Log-slow-admin-statements # log slow query statements such as optimize table, analyze table, and alter table.
Log-queries-not-using-indexes # if the running SQL statement does not use an index, the mysql database also records this SQL statement to the slow query log file.

Restart the mysql service after the configuration is complete.

3 Test  
For convenience, I only configured the path for slow query logs in my. cnf.

Mysql version 5.1.73

About long_query_time: starting from mysql 5.1, long_query_time records the SQL statement running time in microseconds. Previously, it was recorded in seconds only. This allows you to more accurately record the SQL running time for DBA analysis.

Enter mysql.


Mysql> show VARIABLES like "% slow %"; # View slow query log information
Mysql> set global long_query_time = 0.01; # Change the mysql location value to 0.01.
Mysql> show variables like "long_query_time"; # check whether it takes effect
Mysql> select sleep (2); #2 seconds delayed Execution note: After the SQL statement is executed, related information records should be generated in the log file for slow query.
Mysql> show global status like '% slow %'; # view the number of slow query statements

In the log file, you can see that SQL has been recorded.

Summary:

Enable MySQL slow query in Windows
In Windows, the configuration file of MySQL is usually my. ini. find [mysqld] and add
Log-slow-queries = F: \ MySQL \ log \ mysqlslowquery. log
Long_query_time = 2

Enable MySQL slow query in Linux
The configuration file of MySQL in Windows is usually my. cnf. find [mysqld] and add
Log-slow-queries =/data/mysqldata/slowquery. log
Long_query_time = 2

Note:
Log-slow-queries = F: \ MySQL \ log \ mysqlslowquery. log is the location where slow query logs are stored. Generally, this directory requires the write permission of the MySQL running account. Generally, this directory is set to the MySQL data storage directory;
2 in long_query_time = 2 indicates that the query takes more than two seconds to record;

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.