MySQL slow SQL statements common cause

Reference: https://time.geekbang.org/column/article/113440

 

1. No index, which leads to failure of slow queries

If tens of millions in a table of data to a non-indexed columns as a query, in most cases very time-consuming queries, such queries is undoubtedly a slow SQL query. So for a large amount of data query, the need to establish an appropriate index to optimize queries.

Although a lot of time established the index, but in certain scenarios, the index still might fail, so the index is one of the main cause of slow queries fail.

 

2. Lock Wait

There are commonly used storage engine InnoDB and MyISAM, the former support line locks and table locks, which only supports table locks .

If the database is based on the operating table lock implemented under Imagine, if an order table when you update, you need to lock the entire table, then a lot of other database operations (including queries) will be in a wait state, which would seriously affect the system concurrent performance.

At this time, InnoDB storage engine supports row locking is more suitable for high concurrency scenarios. However, when using the InnoDB storage engine, pay special attention to the line lock escalation to a table lock possible. When the batch update operation, row locks would likely escalate to a table lock.

If a large number of rows MySQL believes lock on a table, will lead to decline in the efficiency of the transaction, which may cause other transactions to wait long locks and more locks conflict occurs, resulting in a serious decline in performance, so MySQL will be upgraded to a row lock table lock. Also, the row lock is based on an index plus the lock, if at the time of the update operation, conditions index fails, the row lock will escalate to a table lock.

Therefore, based on the operating table database locks, cause SQL block waiting, thus affecting the speed of execution. In the case of a number of update operations (insert \ update \ delete) is greater than or equal to the read operation, MySQL MyISAM storage engine is not recommended.

In addition to the lock escalation, lock table row lock relatively speaking, although finer granularity, concurrent capacity has improved, but also brought new problems, and that is a deadlock. Therefore, when using row locks to avoid deadlock.

 

3. inappropriate SQL statements

Inappropriate use SQL statements SQL is a slow one of the most common causes. For example, using customary <SELECT *>, <SELECT COUNT (*)> SQL statement, the table used in the large data <LIMIT M, N> paging query, as well as non-indexed fields to sort the like.

 

Guess you like

Origin www.cnblogs.com/agilestyle/p/11429037.html