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

Detailed explanation on how to get the IP address of a docker container

1. After entering the container cat /etc/hosts It...

MYSQL database basics - Join operation principle

Join uses the Nested-Loop Join algorithm. There a...

Tutorial on installing VMWare15.5 under Linux

To install VMWare under Linux, you need to downlo...

MySQL permission control details analysis

Table of contents 1. Global level 2. Database lev...

Steps for Django to connect to local MySQL database (pycharm)

Step 1: Change DATABASES in setting.py # Configur...

js to realize the production method of carousel

This article shares the specific code for js to r...

20 CSS coding tips to make you more efficient (sorted)

In this article, we would like to share with you ...

How to check the version of Kali Linux system

1. Check the kali linux system version Command: c...

Detailed explanation of nginx optimization in high concurrency scenarios

In daily operation and maintenance work, nginx se...

30 excellent examples of color matching in web design

Today, this article has collected 30 excellent cas...

Implementing a simple timer based on Vue method

Vue's simple timer is for your reference. The...

MySQL 8.0.12 Installation and Configuration Tutorial

This article records the detailed tutorial for in...

The implementation process of ECharts multi-chart linkage function

When there is a lot of data to be displayed, the ...

How to use Docker to limit container resources

Problem Peeping In the server, assuming that the ...