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

Docker memory monitoring and stress testing methods

The Docker container that has been running shows ...

Vue achieves seamless carousel effect (marquee)

This article example shares the specific code of ...

How to implement JavaScript's new operator yourself

Table of contents Constructor new Operator Implem...

A detailed introduction to the basics of Linux scripting

Table of contents 1. Script vim environment 2. Ho...

How to implement insert if none and update if yes in MySql

summary In some scenarios, there may be such a re...

Understand CSS3 FlexBox elastic layout in 10 minutes

Basic Introduction Features Flexbox is a CSS disp...

Detailed example of clearing tablespace fragmentation in MySQL

Detailed example of clearing tablespace fragmenta...

Notes on matching MySql 8.0 and corresponding driver packages

MySql 8.0 corresponding driver package matching A...

Tutorial on installing Ubuntu 20.04 and NVIDIA drivers

Install Ubuntu 20.04 Install NVIDIA drivers Confi...

Vue implements real-time refresh of the time display in the upper right corner

This article example shares the specific code of ...

Detailed explanation of the code for implementing linear gradients with CSS3

Preface The gradient of the old version of the br...

Installation and configuration tutorial of MySQL 8.0.16 under Win10

1. Unzip MySQL 8.0.16 The dada folder and my.ini ...