Related to the efficiency of mysql database connection query and subquery

Preface: Under normal circumstances, join queries are more efficient than subqueries, because subqueries need to create temporary tables. However, in the actual test, there are some cases where the efficiency of the subquery is higher than that of the join query

1: In the case of deduplication

        ①Connect query

SELECT
	DISTINCT itm.team_id 
FROM
	表1 itm
	LEFT JOIN 表2 itmc on itm.team_id=itmc.team_id
WHERE
	itmc.up_start = !1 
	AND itmc.check_state = !1
	AND itm.NAME LIKE CONCAT( '', '张', '%' ) 

       Here are the query results:

       

        Use explian to view the statement and find that the index is used, which is the effect we want (use index)

      

       ②Using subqueries

	SELECT
	  DISTINCT itm.team_id 
FROM
	表1 itm ,
	(select DISTINCT itmg.team_id from 表2 itmg where itmg.up_start != 1 AND itmg.check_state != 1) tab_a
	where  
 itm.team_id = tab_a.team_id
 and itm.NAME LIKE '张%'
	

       Below is the query result

        

       Use explian to view the statement and found that the primary table and the secondary table also use the index

      

    Test summary:

              1: The result of the query is the same. If there is no de-duplication, the two sql statements, the connection query efficiency is relatively higher

              2: Now the two query statements use deduplication, but the efficiency of join query is much lower than that of subquery

                    Analysis: I personally think this is due to deduplication. The join query deduplication mechanism is to filter out the eligible data in the two tables first, and then deduplicate; but the subquery is equivalent to deduplication twice, and the secondary table is filtered After the first time, the amount of data is much smaller. At this time, it matches the data in the main table and removes the duplication. The workload is much smaller, so the query efficiency is much higher.

     to sum up:

             1: Under normal circumstances, I still insist that the join query is better, the readability of the subquery is relatively poor first, and the maintenance cost is relatively high. At the same time, the subquery will also create a temporary table and waste resources.

             2: The situation like the above does give us enlightenment. In some special cases, sub-queries are really useful and should not be rejected blindly.

             3: The query requirements like the above, in fact, if you can consider this application scenario at the beginning of the design, it will not be as difficult as it is now, so the table design is still the core

 

     On the small expansion of subqueries:

           1: Subqueries can be placed in many places, such as select, from, where, but cannot be placed after group by

           2: If it is placed after the select, it will cause multiple executions of the sub-query statement, and each time the outer query is executed, the sub-query will be executed once (I don’t know if I understand it correctly, I hope some students can correct my mistake!)

 

 

Guess you like

Origin blog.csdn.net/qq_20594019/article/details/111474641