MySQL optimization: use join instead of subquery

MySQL optimization: use join instead of subquery

Use JOIN instead of sub-queries

MySQL supports SQL subqueries starting from version 4.1. This technique uses a SELECT statement to create a single-column query result, and then uses this result as a filter condition in another query. For example, if we want to delete customers who do not have any orders in the customer basic information table, we can use a subquery to first retrieve the customer IDs of all customers who have placed orders from the sales information table, and then pass the result to the main query, as shown below:

DELETE FROM customerinfo 
WHERE CustomerID NOT in (SELECT CustomerID FROM salesinfo ) 

Using subqueries can complete many SQL operations that logically require multiple steps to complete at one time, while also avoiding transaction or table locks, and is also easy to write. However, in some cases, a subquery can be replaced by a more efficient JOIN.. For example, suppose we want to retrieve all users who have no order records, we can use the following query to complete:

SELECT * FROM customerinfo 
WHERE CustomerID NOT in (SELECT CustomerID FROM salesinfo ) 

If you use a JOIN to complete this query, the speed will be much faster. Especially when there is an index on CustomerID in the salesinfo table, the performance will be better. The query is as follows:

SELECT * FROM customerinfo 
LEFT JOIN salesinfoON customerinfo.CustomerID=salesinfo. 
CustomerID 
WHERE salesinfo.CustomerID IS NULL 

JOIN.. is more efficient because MySQL does not need to create a temporary table in memory to complete this logical two-step query.

Summarize

This is the end of this article about MySQL optimization using joins instead of subqueries. It is for reference only. Please point out any deficiencies. Everyone is welcome to exchange ideas and discuss. Thank you friends for supporting this site.

You may also be interested in:
  • In-depth understanding of MySQL self-connection and join association
  • Analysis of MySQL multiple left join query usage
  • MySQL query optimization: Introduction to join query sort limit (join, order by, limit statement)
  • Detailed tutorial on using JOIN statement to perform connection operations in MySQL
  • Mysql join query principle knowledge points
  • Mysql join query syntax and examples
  • Summary of several commonly used join connection methods in Mysql

<<:  js canvas realizes circular water animation

>>:  Detailed explanation of the difference between chown and chmod commands in Linux

Recommend

How to install Chrome browser on CentOS 7

This article introduces how to install Chrome bro...

Using Openlayer in Vue to realize loading animation effect

Note: You cannot use scoped animations! ! ! ! via...

Detailed steps to install Mysql5.7.19 using yum on Centos7

There is no mysql by default in the yum source of...

20 excellent foreign web page color matching cases sharing

This article collects 20 excellent web page color ...

Use of environment variables in Docker and solutions to common problems

Preface Docker can configure environment variable...

20 JS abbreviation skills to improve work efficiency

Table of contents When declaring multiple variabl...

Uniapp implements DingTalk scan code login sample code

Since Uniapp does not have DingTalk authorization...

Nginx external network access intranet site configuration operation

background: The site is separated from the front ...

Basic usage knowledge points of mini programs (very comprehensive, recommended!)

Table of contents What to do when registering an ...

Summary of how JS operates on pages inside and outside Iframe

Table of contents Get the content of the iframe o...

How to define input type=file style

Why beautify the file control? Just imagine that a...

Detailed explanation of how to copy and backup docker container data

Here we take the Jenkins container as an example ...

Detailed explanation of the process of installing msf on Linux system

Or write down the installation process yourself! ...

Detailed tutorial on building an ETCD cluster for Docker microservices

Table of contents Features of etcd There are thre...