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

Detailed explanation of top command output in Linux

Preface I believe everyone has used the top comma...

How to use ssh tunnel to connect to mysql server

Preface In some cases, we only know the intranet ...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...

Detailed tutorial on installing MySQL database in Linux environment

1. Install the database 1) yum -y install mysql-s...

Invalid solution when defining multiple class attributes in HTML

In the process of writing HTML, we often define mu...

Vue3+script setup+ts+Vite+Volar project

Table of contents Create a vue + ts project using...

Solution to span width not being determined in Firefox or IE

Copy code The code is as follows: <html xmlns=...

Lambda expression principles and examples

Lambda Expressions Lambda expressions, also known...

An article teaches you how to use js to achieve the barrage effect

Table of contents Create a new html file: Create ...

Detailed explanation of publicPath usage in Webpack

Table of contents output output.path output.publi...

js uses FileReader to read local files or blobs

Table of contents FileReader reads local files or...

Solve the problem of PhPStudy MySQL startup failure under Windows system

Report an error The Apache\Nginx service started ...