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.