MySQL Database SQL optimization--sub-query optimization

Source: Internet
Author: User
1, what is a subquery, table association query:
Subquery: Refers to using a SELECT query statement in the Select or WHERE clause in the main SQL statement; Select A.name, (select B.name from B where b.id=a.id) from a where a.name lik ' a %‘
Table Association query: refers to multiple tables union query; Select A.name,b.name from a A, where a.id=b.id and a.name like ' a% ';
Although Federated query performance is poor, there is a significant performance advantage over MySQL subqueries. MySQL's sub-query execution plan has been a big problem, although this problem has existed for many years, but has been released by all the stable version of the widespread, has not been much improved. While the authorities have recognized this issue early and pledged to resolve it as soon as possible, at least we have not yet seen which version of the issue has been better solved.
By default, a full table scan is used in a subquery, so the total I/o count = number of primary tables * sub-table. When the data in the main table, the child table is checked 1w, the overall performance of the subquery will be reduced immediately, and the drop will be very severe (the test found that it needs to be more than 10s)


For the above problem, the solution:
1) index or establish a foreign key relationship with the associated column in the child table:
There are two tables A, B, and the following are the execution plans using subqueries

A table structure:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MySQL Database SQL optimization--sub-query optimization

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.