The reason database query slow

Foreword

This article itself, but the database query slow only possible factors, as not enough memory, slower connections are beyond the scope.

In this article from the public reference number below:

Tencent interview: execute a SQL statement is very slow What are the reasons? --- Bukanhouhui series

I make my own sort summary of the number of public content

Start

First of all to discuss classification, this query is in the case of the occasional slow query efficiency, or efficiency has been a problem of slow queries.

In the first case, it may query itself is no problem, the database is experiencing other problems;

In the second case, the query should be out of the question, the need to optimize

Efficiency and occasionally slow case

One reason: Refresh "dirty" page

What is a "dirty" page

When the database insert or update operation, the database will immediately information on the memory data pages updated, but not immediately updated data stored on disk, but to redo log in to save, such as the right time in the redo log information is stored on disk

Case for different data on this data in memory and disk page we will data pages in memory called "dirty" page, and the same memory and on-disk data is called a "clean" page.

When you refresh "dirty" page, the system will suspend other operations, heart and soul of the stored data to disk, it will lead mysql statement is usually performed normally slow

When will refresh "dirty" page

case1: When redo log is full

case2: memory is not enough time

case3: mysql think the system is idle

case4: mysql normally closed

 

Two reasons: the data is locked

You can use Show processlis t command to check the status of execution of sentence, to see whether the data to be queried is locked

 

There have been cases of slow efficiency

One reason: too large amount of data query

To see if the query unnecessary rows and columns, avoid using select * from such statements table

 

Two reasons: the index is not used

When a large amount of data, if not all-table index for index is time consuming. The situation here is not used by the index can be more than a minute

case1: not built Index

case2: Failure Index

Possible causes of failure caused by index

1) columns in the index with other built-in functions or + - * / calculation

2) beginning with wildcards

3) multi-column index that most closely matches contrary to the principle of the best

. 4) or the container caused by operator failure index, unless the operation of each column has an index or

5) a string without single quotation marks

case3: wrong index system

System the wrong index is actually a form of indexing failure, but as more related to knowledge, so come alone to analyze.

Wrong index system causes the system to fail when the full table scan index is compared with the number of rows to scan the index, if the index but that the use of more complex, the system will give up the index by way of a full table scan.

 

So when will the use of the index case but less than a full table scan efficiency occurs it ?

First, we all know that the primary key index is stored entire row of data, rather than save the primary key index is the value of the primary key. Therefore, the use of non-primary first positioning key index to satisfy the condition of the primary key of the row, the entire row of data in the process to get the information from the primary key index to twice.

In extreme cases, when the index to find a full table of data conditions are met, this time looking for the index compared to a full table scan instead of a series of multi-indexing process.

So the system needs to be applied in determining whether the index will first determine if the use of the index roughly how many lines need to scan, if the system predicts the number of rows to scan a lot, the system will choose to give up the index take the form of a full table scan.

 

How to use the system to determine the number of rows to scan the index? That's where discrimination index . Discrimination index, also known as the base, an index on the more different values, means less the same value of the index, the higher the discrimination means that the index appears.

Less higher discrimination index data query condition is satisfied, then the system predicts small scanning function.

Then the index of discrimination and how to get it?

Sampling . Discrimination index estimation system by way of sampling. Since then there will be a sampling error, if you want to avoid this error, the system does not want to choose this way it thinks of humanity, you can force the use of the index

select * from t index(a) where c>100 and c<1000;

 

You may also be below the first line of command to view the index of the base, if the base does not meet the actual words you can also be the second line of command allows the system to re-sampled to calculate the index base

1 show index from t;
2 analyze table t;

 

Can explain + SQL query to view the execution of SQL statements to see if the expected use of the index

explain command can refer to my other blog post mysql optimization of a query optimization

 

Guess you like

Origin www.cnblogs.com/huanglf714/p/11106684.html