Common causes and solutions for slow MySQL SQL statements

Common causes and solutions for slow MySQL SQL statements

1. Slow query due to lack of index or invalid index

If you use a non-indexed column as a query condition in a table with tens of millions of data, the query will be very time-consuming in most cases. This query is undoubtedly a slow SQL query. Therefore, for queries with large amounts of data, it is necessary to create appropriate indexes to optimize the queries.

Although indexes are often created, they may still fail in certain scenarios, so index failure is also one of the main causes of slow queries.

2. Lock Wait

Commonly used storage engines include InnoDB and MyISAM. The former supports row locks and table locks, while the latter only supports table locks.

If database operations are implemented based on table locks, imagine that if an order table needs to be locked when it is updated, then a large number of other database operations (including queries) will be in a waiting state, which will seriously affect the concurrency performance of the system.

At this time, the row lock supported by the InnoDB storage engine is more suitable for high concurrency scenarios. However, when using the InnoDB storage engine, special attention should be paid to the possibility of row locks being upgraded to table locks. During batch update operations, row locks are likely to be upgraded to table locks.

MySQL believes that if a large number of row locks are used on a table, the efficiency of transaction execution will decrease, which may cause other transactions to wait for a long time and more lock conflicts, resulting in serious performance degradation. Therefore, MySQL will upgrade row locks to table locks. In addition, row locks are locks based on indexes. If the conditional index fails during an update operation, the row lock will be upgraded to a table lock.

Therefore, database operations based on table locks will cause SQL to block and wait, thus affecting the execution speed. In some cases where update operations (insert\update\delete) are greater than or equal to read operations, MySQL does not recommend using the MyISAM storage engine.

In addition to lock upgrades, row locks have finer granularity and improved concurrency capabilities compared to table locks, but they also bring new problems, namely deadlocks. Therefore, when using row locks, be careful to avoid deadlocks.

3. Inappropriate SQL statements

Using inappropriate SQL statements is also one of the most common causes of slow SQL. For example, you are accustomed to using <SELECT *>, <SELECT COUNT(*)> SQL statements, using <LIMIT M,N> paging queries in large data tables, and sorting non-indexed fields.

The above is all the knowledge points introduced this time. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to solve the problem of a slow SQL query in MySQL causing the entire website to crash
  • MySQL starts slow SQL and analyzes the causes
  • Detailed explanation of MySQL using profile to analyze slow SQL (group left join is more efficient than subquery)
  • Let's talk in detail about the direction of slow SQL optimization in MySQL

<<:  Migrate virtual machines between VMware Workstation and vSphere (picture and text)

>>:  Example of converting JavaScript flat array to tree structure

Recommend

Optimization methods when Mysql occupies too high CPU (must read)

When Mysql occupies too much CPU, where should we...

Detailed explanation of the execution process of MySQL query statements

Table of contents 1. Communication method between...

Nginx configuration PC site mobile site separation to achieve redirection

Use nginx to configure the separation of PC site ...

Why should the number of rows in a single MySQL table not exceed 5 million?

Today, let’s discuss an interesting topic: How mu...

Example of Html shielding right-click menu and left-click typing function

Disable right-click menu <body oncontextmenu=s...

W3C Tutorial (15): W3C SMIL Activities

SMIL adds support for timing and media synchroniz...

Detailed tutorial on using cmake to compile and install mysql under linux

1. Install cmake 1. Unzip the cmake compressed pa...

4 functions implemented by the transform attribute in CSS3

In CSS3, the transform function can be used to im...

How to install MySQL 5.7.17 and set the encoding to utf8 in Windows

download MySQL official download, select Windows ...

Pure CSS to change the color of the picture

The css technique for changing the color of an im...

JavaScript Advanced Closures Explained

Table of contents 1. The concept of closure Addit...

Docker file storage path, get container startup command operation

The container has already been created, how to kn...

IE conditional comments for XHTML

<br />Conditional comments are a feature uni...