MySql database query slow

First, what is a database query slow?

Slow database query, the query time is more than the statement we set time.

You can view the time set:

 

 

 The default setting time is 10 seconds. They can also set their own according to the actual project. 

set long_query_time=0.0001;

Second, the relevant parameters MySQL slow query explanation:

slow_query_log: whether to open the slow query log, 1 on, 0 means closed.

log-slow-queries: Legacy (version 5.6 or less) MySQL database slow query log storage path. Can not set this parameter, the system will default to a default file host_name-slow.log

slow-query-log-file: the new version (5.6 or later) MySQL database slow query log storage path. Can not set this parameter, the system will default to a default file host_name-slow.log

long_query_time: slow query threshold, when the query time is more than a set threshold, logging.

log_queries_not_using_indexes: Unused index also recorded a query to the slow query log (optional).

log_output: log storage. log_output = 'FILE' represents the log file is stored, the default value is 'FILE'. log_output = 'TABLE' represents the log stored in the database, such information will be written to the log mysql

Slow query log configuration:

Slow_query_log default value is OFF, meaning slow query log is disabled, can be turned on by setting the value slow_query_log as follows:

show variables like 'slow_query%'; - slow query

show variables like 'long_query_time'; - query time set

show variables like '% slow_query_log%'; - slow query log configuration View

show variables like 'log_queries_not_using_indexes'; - View unused indexes

set global log_queries_not_using_indexes = 1; - not use the index to the query is also recorded slow query log

show global status like '% slow_queries%'; - slow query Search record

Third, check the maximum number of connections and settings

- 1, see the maximum number of connections

show variables like '% max_connections%'; - limit the number of connections

SHOW GLOBAL STATUS LIKE 'Max_used_connections' - maximum number of connections in response to service

Ideal settings: Max_used_connections / max_connections * 100% ≈ 85%
the maximum number of connections accounted for about 85% of the upper limit of the connection, if it is found at the ratio of 10% or less, MySQL server connections set upper limit too high.

- 2, modify the maximum number of connections
set GLOBAL max_connections = 500;

- List MySQL servers running various status values
SHOW GLOBAL STATUS;

Fourth, the common slow query optimization

1, the index did not play a role in the case of

1), using the LIKE keyword query in the query using the LIKE keyword query, if the first character of the string is "%", the index will not work. Only "%" not in the first position index will work.

2) using a multi-column index query MySQL can create indexes on multiple fields. An index can comprise up to 16 fields. For multi-column index, only the query using these fields in the first field, the index will be used.

2, optimize the structure of the database

1), the table number field disassembled into a plurality of tables

2), an intermediate table

3, associated with the query decomposition

Many high performance applications will decompose on the relational query is a query may be a single table, and associating the query result in the application, many scenes would be more efficient for each table.

4, page optimization limit

  Paged limit operation generally used in a system implemented method of adding the offset, while adding an appropriate order by clause. If there is a corresponding index, usually efficiency will be good, otherwise MySQL needs to do a lot of file sorting operations.

      A very troublesome problem is that when the offset is very large, for example, may be such a query limit 10000,20, which is needed mysql query 10020 then only return the last 20, in front of 10,000 records will be discarded , such a high price.

        The easiest way to optimize such a query is to use the index as much as possible to cover the scan, instead of querying all the columns. The time needed to do and then return the association operation required for the column. For large offsets doing so efficiency will be greatly improved.

Guess you like

Origin www.cnblogs.com/jane4321/p/11607719.html