Mysql Database SQL Optimization-subquery optimization and mysql Database SQL Optimization

Source: Internet
Author: User

Mysql Database SQL Optimization-subquery optimization and mysql Database SQL Optimization
1. What are subqueries and table join queries:
Subquery: The select statement is used in the select or where clause of the primary SQL statement; select. name, (select B. name from B where B. id =. id) from a where. name lik 'a %'
Table join query: a joint query of multiple tables; select. name, B. name from a, B where. id = B. id and. name like 'a % ';
Although the performance of the Combined Query is not good, it has a great performance advantage compared with MySQL subqueries. MySQL's subquery Execution Plan has been a big problem. Although this problem has existed for many years, it has been common in all the stable versions that have been released and has not been significantly improved. Although the official team admitted this issue for a long time and promised to solve it as soon as possible, we have not yet seen any better version to solve this problem.
By default, full table scan is used in subqueries, so the total I/O COUNT = Number of Primary tables * Number of subtables. When the data in both the primary and subtables is million queries, the overall performance of subqueries will immediately decrease, and the decline will be very severe (the test shows that it takes more than 10 seconds)


Solution for the above problem:
1) index the associated columns in the sub-table or create a foreign key relationship:
There are two tables a and B as follows. The execution plan is as follows:

Table a structure:



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.