sql是什么 SQL中IN和EXISTS用法的區(qū)別?
SQL中IN和EXISTS用法的區(qū)別?in和existsin是把外表和內(nèi)表作hash連接,而exists是對外表作loop循環(huán),每次loop循環(huán)再對內(nèi)表進行查詢。如果兩個表中一個較小,一個是大表,則子
SQL中IN和EXISTS用法的區(qū)別?
in和existsin是把外表和內(nèi)表作hash連接,而exists是對外表作loop循環(huán),每次loop循環(huán)再對內(nèi)表進行查詢。如果兩個表中一個較小,一個是大表,則子查詢表大的用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)外表都進行全表掃描,沒有用到索引;而notextsts的子查詢依然能用到表上的索引。所以無論那個表大,用notexists都比notin要快。in與=的區(qū)別selectnamefromstudentwherenamein("zhang","wang","li","zhao")與selectnamefromstudentwherename="zhang"orname="li"orname="wang"orname="zhao"的結(jié)果是相同的。
sql中的in和exists區(qū)別?
1.exist,not exist一般都是與子查詢一起使用. In可以與子查詢一起使用,也可以直接in (a,b.....)。2.exist會針對子查詢的表使用索引. not exist會對主子查詢都會使用索引. in與子查詢一起使用的時候,只能針對主查詢使用索引. not in則不會使用任何索引. 注意,一直以來認為exists比in效率高的說法是不準確的。in 是把外表和內(nèi)表作hash 連接,而exists是對外表作loop循環(huán),每次loop循環(huán)再對內(nèi)表進行查詢。
SQL語句中in和exist區(qū)別?
Exist和Not Exist 強調(diào)返回值是True或False,理清這一點,理解也許就會簡單一些。