exists和in的區(qū)別性能比較 sql中的in和exists區(qū)別?
sql中的in和exists區(qū)別?1.exist,not exist一般都是與子查詢一起使用. In可以與子查詢一起使用,也可以直接in (a,b.....)。2.exist會針對子查詢的表使用索引.
sql中的in和exists區(qū)別?
1.exist,not exist一般都是與子查詢一起使用. In可以與子查詢一起使用,也可以直接in (a,b.....)。2.exist會針對子查詢的表使用索引. not exist會對主子查詢都會使用索引. in與子查詢一起使用的時候,只能針對主查詢使用索引. not in則不會使用任何索引. 注意,一直以來認為exists比in效率高的說法是不準確的。in 是把外表和內表作hash 連接,而exists是對外表作loop循環(huán),每次loop循環(huán)再對內表進行查詢。
SQL中IN和EXISTS用法的區(qū)別?
in和existsin是把外表和內表作hash連接,而exists是對外表作loop循環(huán),每次loop循環(huán)再對內表進行查詢。如果兩個表中一個較小,一個是大表,則子查詢表大的用exists,子查詢表小的用in:例如:表A(小表),表B(大表)1:select*fromAwhereccin(selectccfromB)效率低,用到了A表上cc列的索引;select*fromAwhereexists(selectccfromBwherecc=A.cc)效率高,用到了B表上cc列的索引。相反的2:select*fromBwhereccin(selectccfromA)效率高,用到了B表上cc列的索引;select*fromBwhereexists(selectccfromAwherecc=B.cc)效率低,用到了A表上cc列的索引。notin和notexists如果查詢語句使用了notin那么內外表都進行全表掃描,沒有用到索引;而notextsts的子查詢依然能用到表上的索引。所以無論那個表大,用notexists都比notin要快。in與=的區(qū)別selectnamefromstudentwherenamein("zhang","wang","li","zhao")與selectnamefromstudentwherename="zhang"orname="li"orname="wang"orname="zhao"的結果是相同的。