Use connection (join) to replace subquery (sub-queries) MySQL optimization series record

Source: Internet
Author: User

Use connection (join) instead of subquery (sub-queries)

MySQL supports SQL subquery starting from 4.1. This technique can use the SELECT statement to create a single-column query result, and then use the result as a filter for another query. For example, if we want to delete a customer who does not have any orders in the customer's basic information table, we can take advantage of the subquery to remove all the customer IDs from the Sales Information table and then pass the results to the main query, as follows:

DELETE  from  WHERE not in (theSELECT from


The use of subqueries allows you to do a lot of SQL operations that logically require multiple steps to complete, as well as to avoid transactions or table locks and write them easily. However, in some cases, subqueries can be more efficiently connected (join). Alternative. For example, suppose we want to take out all the users without an order record, which can be done with the following query:

SELECT *  from  WHERE not in (theSELECT from

If you use Connection (join): To complete this query work, the speed will be much faster. Especially if the Salesinfo table in the CustomerID index, the performance will be better, query as follows:

SELECT *  from  leftjoins on CustomerInfo. CustomerID=WHEREisNULL

Connect (Join): It is more efficient because MySQL does not need to create a temporary table in memory to complete this logical two-step query effort

Use connection (join) to replace subquery (sub-queries) MySQL optimization series record

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.