數(shù)據(jù)庫(kù)not exists用法 SQL中IN和EXISTS用法的區(qū)別?
SQL中IN和EXISTS用法的區(qū)別?in和existsin是把外表和內(nèi)表作hash連接,而exists是對(duì)外表作loop循環(huán),每次loop循環(huán)再對(duì)內(nèi)表進(jìn)行查詢(xún)。如果兩個(gè)表中一個(gè)較小,一個(gè)是大表,則子
SQL中IN和EXISTS用法的區(qū)別?
in和existsin是把外表和內(nèi)表作hash連接,而exists是對(duì)外表作loop循環(huán),每次loop循環(huán)再對(duì)內(nèi)表進(jìn)行查詢(xún)。如果兩個(gè)表中一個(gè)較小,一個(gè)是大表,則子查詢(xún)表大的用exists,子查詢(xún)表小的用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如果查詢(xún)語(yǔ)句使用了notin那么內(nèi)外表都進(jìn)行全表掃描,沒(méi)有用到索引;而notextsts的子查詢(xún)依然能用到表上的索引。所以無(wú)論那個(gè)表大,用notexists都比notin要快。in與=的區(qū)別selectnamefromstudentwherenamein("zhang","wang","li","zhao")與selectnamefromstudentwherename="zhang"orname="li"orname="wang"orname="zhao"的結(jié)果是相同的。
sql中exists是什么意思,怎么講解?
Exists 方法 描述如果在 Dictionary 對(duì)象中指定的關(guān)鍵字存在,返回 True,若不存在,返回 False。舉個(gè)例子吧:select * from a where exists(select * from b where a.id = b.id)a表和b表使用id關(guān)聯(lián),這條語(yǔ)句的含義是,當(dāng)b表能夠查詢(xún)出結(jié)果時(shí),exists(select * from b where a.id = b.id)子句為真,只有滿足exists結(jié)果為真時(shí),才會(huì)查詢(xún)出a表的記錄。這樣解釋你明白了嗎。