sql中in和exist語句的區別?

2021-08-20 09:26:57 字數 822 閱讀 5371

in和exists

in 是把外表和內錶作hash 連線,而exists是對外表作loop迴圈,每次loop迴圈再對內表進行查詢。

如果兩個表中乙個較小,乙個是大表,則子查詢表大的用exists,子查詢錶小的用in:

例如:表a(小表),表b(大表)1:select * from a where cc in (select cc from b)

效率低,用到了a表上cc列的索引;select * from a where exists(select cc from b where cc=a.cc)

效率高,用到了b表上cc列的索引。

相反的2:select * from b where cc in (select cc from a)

效率高,用到了b表上cc列的索引;select * from b where exists(select cc from a where cc=b.cc)

效率低,用到了a表上cc列的索引。

not in 和not exists如果查詢語句使用了not in 那麼內外表都進行全表掃瞄,沒有用到索引;而not extsts 的子查詢依然能用到表上的索引。所以無論那個表大,用not exists都比not in要快。

in 與 =的區別

select name from student where name in ('zhang','wang','li','zhao');與

select name from student where name='zhang' or name='li' or

name='wang' or name='zhao'

的結果是相同的。

SQL中exist和in的執行效率問題

select from a where id in select id from b select a.from a a where exists select 1 from b b where a.id b.id 結論 1 看表資料規模,a表 b表,使用in a表2 無論什麼情況,not exis...

sql中exist與in的區別

in 和 exists也是很好區別的.in 是乙個集合運算子.a in 這個運算中,前面是乙個元素,後面是乙個集合,集合中的元素型別是和前面的元素一樣的.而exists是乙個存在判斷,如果後面的查詢中有結果,則exists為真,否則為假.in 運算用在語句中,它後面帶的select 一定是選乙個字段...

sql中exist與in的區別

in 和 exists也是很好區別的.in 是乙個集合運算子.a in 這個運算中,前面是乙個元素,後面是乙個集合,集合中的元素型別是和前面的元素一樣的.而exists是乙個存在判斷,如果後面的查詢中有結果,則exists為真,否則為假.in 運算用在語句中,它後面帶的select 一定是選乙個字段...