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:
|
<<: js canvas realizes circular water animation
>>: Detailed explanation of the difference between chown and chmod commands in Linux
This article introduces how to install Chrome bro...
Note: You cannot use scoped animations! ! ! ! via...
There is no mysql by default in the yum source of...
This article collects 20 excellent web page color ...
Preface Docker can configure environment variable...
Table of contents When declaring multiple variabl...
Since Uniapp does not have DingTalk authorization...
background: The site is separated from the front ...
Table of contents 1. React combined with Antd to ...
Table of contents What to do when registering an ...
Table of contents Get the content of the iframe o...
Why beautify the file control? Just imagine that a...
Here we take the Jenkins container as an example ...
Or write down the installation process yourself! ...
Table of contents Features of etcd There are thre...