MySQL slow Query

Source: Internet
Author: User

MySQL slow Query

The slow query (slow log) helps us locate specific SQL statements for SQL statement optimization. For example, the slow query log records the SQL statements whose execution time exceeds the given value, locate the problem.

Enable slow Query

View MySQL database instance slow query parameters

  1. Mysql> show variables like '% slow % ';
  2. + --------------------------- + ------------------------------------- +
  3. | Variable_name | Value |
  4. + --------------------------- + ------------------------------------- +
  5. | Log_slow_admin_statements | OFF |
  6. | Log_slow_slave_statements | OFF |
  7. | Slow_launch_time | 2 |
  8. | Slow_query_log | OFF |
  9. | Slow_query_log_file |/var/lib/mysql/localhost-slow.log |
  10. + --------------------------- + ------------------------------------- +
  11. 5 rows in set (0.00 sec)
  12. Mysql> show variables like '% long_query_time % ';
  13. + ----------------- + ----------- +
  14. | Variable_name | Value |
  15. + ----------------- + ----------- +
  16. | Long_query_time | 10.000000 |
  17. + ----------------- + ----------- +
  18. 1 row in set (0.02 sec)
mysql> show variables like '%slow%';+---------------------------+-----------------------------------+| Variable_name             | Value                             |+---------------------------+-----------------------------------+| log_slow_admin_statements | OFF                               || log_slow_slave_statements | OFF                               || slow_launch_time          | 2                                 || slow_query_log            | OFF                               || slow_query_log_file       | /var/lib/mysql/localhost-slow.log |+---------------------------+-----------------------------------+5 rows in set (0.00 sec)mysql> show variables like '%long_query_time%';+-----------------+-----------+| Variable_name   | Value     |+-----------------+-----------+| long_query_time | 10.000000 |+-----------------+-----------+1 row in set (0.02 sec)


Where,

Slow_query_log is the parameter for enabling slow query.

Slow_query_log_file is the path of the slow query log file.

Log_query_time is the maximum query time. When this time is exceeded, log records are queried slowly.

The following example shows how to start a slow query, set the maximum query time to 2 seconds, and run sleep3 seconds to view the slow query log.

  1. Mysql> set global slow_query_log = ON;
  2. Query OK, 0 rows affected (0.03 sec)
  3. Mysql> set long_query_time = 2;
  4. Query OK, 0 rows affected (0.00 sec)
  5. Mysql> show variables like '% long_query_time % ';
  6. + ----------------- + ---------- +
  7. | Variable_name | Value |
  8. + ----------------- + ---------- +
  9. | Long_query_time | 2.000000 |
  10. + ----------------- + ---------- +
  11. 1 row in set (0.00 sec)
  12. Mysql> select sleep (3 );
  13. + ---------- +
  14. | Sleep (3) |
  15. + ---------- +
  16. | 0 |
  17. + ---------- +
  18. 1 row in set (3.00 sec)
  19. Mysql> system cat/var/lib/mysql/localhost-slow.log
  20. /Usr/sbin/mysqld, Version: 5.6.22-log (MySQL Community Server (GPL). started:
  21. Tcp port: 3306 Unix socket:/var/lib/mysql. sock
  22. Time Id Command Argument
  23. # Time: 150102 16:56:54
  24. # User @ Host: root [root] @ localhost [] Id: 1
  25. # Query_time: 3.001084 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
  26. Use test2;
  27. SET timestamp = 1420189014;
  28. Select sleep (3 );
mysql> set global slow_query_log=ON;Query OK, 0 rows affected (0.03 sec)mysql> set long_query_time=2;Query OK, 0 rows affected (0.00 sec)mysql> show variables like '%long_query_time%';+-----------------+----------+| Variable_name   | Value    |+-----------------+----------+| long_query_time | 2.000000 |+-----------------+----------+1 row in set (0.00 sec)mysql> select sleep(3);+----------+| sleep(3) |+----------+|        0 |+----------+1 row in set (3.00 sec)mysql> system cat /var/lib/mysql/localhost-slow.log/usr/sbin/mysqld, Version: 5.6.22-log (MySQL Community Server (GPL)). started with:Tcp port: 3306  Unix socket: /var/lib/mysql/mysql.sockTime                 Id Command    Argument# Time: 150102 16:56:54# User@Host: root[root] @ localhost []  Id:     1# Query_time: 3.001084  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0use test2;SET timestamp=1420189014;select sleep(3);


Slow query also has a parameter log_queries_not_using_indexes, which indicates the number of times that no index is used to record slow query logs per minute. The default value is 0, indicating no limit, but in the production environment, these statements occupy a large number of slow query logs, which causes difficulties for DBA analysis.

In addition to viewing slow query logs using log files, you can also view logs using tables. In mysql mode, a table named slow_log records the output of slow queries. You only need to change the log output mode log_output. The log_output parameter is global and dynamic, it can be dynamically changed at runtime.

  1. Mysql> show variables like '% log_output % ';
  2. + --------------- + ------- +
  3. | Variable_name | Value |
  4. + --------------- + ------- +
  5. | Log_output | FILE |
  6. + --------------- + ------- +
  7. 1 row in set (0.00 sec)
  8. Mysql> set global log_output = 'table ';
  9. Query OK, 0 rows affected (0.00 sec)
  10. Mysql> select sleep (3 );
  11. + ---------- +
  12. | Sleep (3) |
  13. + ---------- +
  14. | 0 |
  15. + ---------- +
  16. 1 row in set (3.00 sec)
mysql> show variables like '%log_output%';+---------------+-------+| Variable_name | Value |+---------------+-------+| log_output    | FILE  |+---------------+-------+1 row in set (0.00 sec)mysql> set global log_output='table';Query OK, 0 rows affected (0.00 sec)mysql> select sleep(3);+----------+| sleep(3) |+----------+|        0 |+----------+1 row in set (3.00 sec)


Note that the engine room csv of the slow_log table may not be very efficient in query, but it is very efficient to append and insert logs.

  1. Mysql> show create table slow_log \ G
  2. * *************************** 1. row ***************************
  3. Table: slow_log
  4. Create Table: create table 'slow _ log '(
  5. 'Start _ time' timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  6. 'User _ host' mediumtext not null,
  7. 'Query _ time' time not null,
  8. 'Lock _ time' time not null,
  9. 'Rows _ sent' int (11) not null,
  10. 'Rows _ examined' int (11) not null,
  11. 'Db' varchar (512) not null,
  12. 'Last _ insert_id 'int (11) not null,
  13. 'Insert _ id' int (11) not null,
  14. 'Server _ id' int (10) unsigned not null,
  15. 'SQL _ text' mediumtext NOT NULL,
  16. 'Thread _ id' bigint (21) unsigned NOT NULL
  17. ) ENGINE = csv default charset = utf8 COMMENT = 'lowlog'
mysql> show create table slow_log\G*************************** 1. row ***************************       Table: slow_logCreate Table: CREATE TABLE `slow_log` (  `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  `user_host` mediumtext NOT NULL,  `query_time` time NOT NULL,  `lock_time` time NOT NULL,  `rows_sent` int(11) NOT NULL,  `rows_examined` int(11) NOT NULL,  `db` varchar(512) NOT NULL,  `last_insert_id` int(11) NOT NULL,  `insert_id` int(11) NOT NULL,  `server_id` int(10) unsigned NOT NULL,  `sql_text` mediumtext NOT NULL,  `thread_id` bigint(21) unsigned NOT NULL) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log'


Another useful parameter for slow queries is long_query_io, which indicates that the logical IO count will exceed the specified number of logical IO reads (logical IO includes physical IO, indicating the sum of the number of reads of physical IO and buffer pool) the SQL statement is recorded in the slow query log.

-------------------------------------- Split line --------------------------------------

Install MySQL in Ubuntu 14.04

MySQL authoritative guide (original book version 2nd) Clear Chinese scan PDF

Ubuntu 14.04 LTS install LNMP Nginx \ PHP5 (PHP-FPM) \ MySQL

Build a MySQL Master/Slave server in Ubuntu 14.04

Build a highly available distributed MySQL cluster using Ubuntu 12.04 LTS

Install MySQL5.6 and Python-MySQLdb in the source code of Ubuntu 12.04

MySQL-5.5.38 universal binary Installation

-------------------------------------- Split line --------------------------------------

This article permanently updates the link address:

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.