在了解之前要先了解對應語法 in 與 exist。
IN: select * from A where A.id in (select B.id from B)
in后的括號的表達式結果要求之輸出一列字段。與之前的搜索字段匹配,匹配到相同則返回對應行。
mysql的執行順序是先執行子查詢,然后執行主查詢,用子查詢的結果按條匹配主查詢。
EXIST: select * from A where exists(select * from B where B.id= A.id)
exist后的括號里則無輸出要求,exist判斷后面的結果集中有沒有行,有行則返回外層查詢對應的行。
ps所以exist還可以這樣寫: 用常量替換* ,反正是判斷有沒有行,不需要實際傳回的數據。
select * from A where exist(select 1 from B where B.id= A.id)
mysql的執行順序是先執行主查詢,將主查詢的數據放在子查詢中做條件驗證。
大體看來貌似exist的執行效率比in低,但其實exists子查詢在底層做了優化,會忽略select清單,也並不會對每條數據進行對比。
比如這里有兩張表
+--------+----------+ | A.id | A.name | //500行 +--------+----------+
+--------+----------+ | B.id | B.name | //5000行 +--------+----------+
在查詢中最好使用小表驅動大表,因為在外層表循環內層的時候,會鎖定外層表,如果大表在外,會鎖定5k次 。
如果要求查詢所有id相同的Aname 有兩種查詢方式
1.select A.name from A where A.id in(select B.id from B) 2.select A.name from A where exists(select 1 from B where A.id = B.id)
1.由B表驅動A表 會先執行子查詢 大表驅動小表
2.由A表驅動B表 會先執行主查詢 小表驅動大表
如果需求變為 查詢所有id相同的Bname
1.select B.name from B where B.id in(select A.id from B) 2.select B.name from B where exists(select 1 from A where A.id = B.id)
1.小表驅動大表
2.大表驅動小表