exists和in的區(qū)別性能比較 sql中的in和exists區(qū)別?
sql中的in和exists區(qū)別?1.exist,not exist一般都是與子查詢一起使用. In可以與子查詢一起使用,也可以直接in (a,b.....)。2.exist會(huì)針對(duì)子查詢的表使用索引.
sql中的in和exists區(qū)別?
1.exist,not exist一般都是與子查詢一起使用. In可以與子查詢一起使用,也可以直接in (a,b.....)。2.exist會(huì)針對(duì)子查詢的表使用索引. not exist會(huì)對(duì)主子查詢都會(huì)使用索引. in與子查詢一起使用的時(shí)候,只能針對(duì)主查詢使用索引. not in則不會(huì)使用任何索引. 注意,一直以來認(rèn)為exists比in效率高的說法是不準(zhǔn)確的。in 是把外表和內(nèi)表作hash 連接,而exists是對(duì)外表作loop循環(huán),每次loop循環(huán)再對(duì)內(nèi)表進(jìn)行查詢。
SQL中IN和EXISTS用法的區(qū)別?
in和existsin是把外表和內(nèi)表作hash連接,而exists是對(duì)外表作loop循環(huán),每次loop循環(huán)再對(duì)內(nèi)表進(jìn)行查詢。如果兩個(gè)表中一個(gè)較小,一個(gè)是大表,則子查詢表大的用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那么內(nèi)外表都進(jìn)行全表掃描,沒有用到索引;而notextsts的子查詢依然能用到表上的索引。所以無論那個(gè)表大,用notexists都比notin要快。in與=的區(qū)別selectnamefromstudentwherenamein("zhang","wang","li","zhao")與selectnamefromstudentwherename="zhang"orname="li"orname="wang"orname="zhao"的結(jié)果是相同的。