ORACLE 中exist和in的區別

2021-07-31 13:55:24 字數 4609 閱讀 2165

博文**(oracle中的exists 和not exists 用法):

博文**(  in與exists語句的效率問題)

(一)

exists(sql 返回結果集為真)

notexists(sql 不返回結果集為真)

如下:

表a id name

1a1

2a2

3a3

表b id aid name

11b1

22b2

32b3

表a和表b是1對多的關係 a.id =>b.aid

1

select id,name from a where exist (select

*from b where a.id=

b.aid)

2執行結果為31

a142a2

5原因可以按照如下分析

6select id,name from a where

exists (select

*from b where b.aid=1)

7--->select * from b where b.aid=1有值返回真所以有資料89

select id,name from a where

exists (select

*from b where b.aid=2)

10--

->select * from b where b.aid=2有值返回真所以有資料

1112

select id,name from a where

exists (select

*from b where b.aid=3)

13--

->select * from b where b.aid=3無值返回真所以沒有資料

1415

notexists

就是反過來

16select id,name from a where

not exist (select

*from b where a.id=

b.aid)

17執行結果為

183 a3

(二)sql中in,notin,exists,notexists的用法和差別:

in

關鍵字使您得以選擇與列表中的任意乙個值匹配的行。確定給定的值是否與子查詢或列表中的值相匹配。

(1)獲得居住在 california、indiana 或 maryland 州的所有作者的姓名和州的列表時,就需要下列查詢:

select productid, productname from northwind.dbo.products where categoryid =

1or categoryid =

4or categoryid =

5然而,如果使用

in,少鍵入一些字元也可以得到同樣的結果:

select productid, productname from northwind.dbo.products where categoryid in (1, 4, 5)  

in關鍵字之後的專案必須用逗號隔開,並且括在括號中。

(2)下列查詢在 titleauthor 表中查詢在任一種書中得到的版稅少於 50

%的所有作者的 au_id,然後從 authors 表中選擇 au_id 與titleauthor 查詢結果匹配的所有作者的姓名:

select au_lname, au_fname from authors where au_id in (select au_id from titleauthor where royaltyper <50)

結果顯示有一些作者屬於少於 50%

的一類。

notin:通過 not

in關鍵字引入的子查詢也返回一列零值或更多值。

以下查詢查詢沒有出版過商業書籍的出版商的名稱。

select pub_name from publishers where pub_id not

in (select pub_id from titles where type =

'business')

使用 exists 和 not

exists

引入的子查詢可用於兩種集合原理的操作:交集與差集。

(1) 兩個集合的交集包含同時屬於兩個原集合的所有元素。

(2)差集包含只屬於兩個集合中的第乙個集合的元素。

exists

:指定乙個子查詢,檢測行的存在。

本示例所示查詢查詢由位於以字母 b 開頭的城市中的任一出版商出版的書名:

select

distinct pub_name from publishers where

exists (select

*from titles where pub_id = publishers.pub_id and type =

'business')

select

distinct pub_name from publishers where pub_id in (select pub_id from titles where type =

'business')

兩者的區別:

exists:後面可以是整句的查詢語句如:select

*from

titles

in:後面只能是對單列:select pub_id from

titles

notexists

:  例如,要查詢不出版商業書籍的出版商的名稱:

select pub_name from publishers where

notexists (select

*from titles where pub_id = publishers.pub_id and type =

'business')

下面的查詢查詢已經不銷售的書的名稱:

select title from titles where

notexists (select title_id from sales where title_id = titles.title_id)

(三)in 於 exists 的效率問題

select *from a

where id in(select id from b)

以上查詢使用了in語句,in()只執行一次,它查出b表中的所有id欄位並快取起來.之後,檢查a表的id是否與b表中的id相等,如果相等則將a表的記錄加入結果集中,直到遍歷完a表的所有記錄.

它的查詢過程類似於以下過程

list resultset=;

array a=(select *from a);

array b=(select id from b);

for(int i=0;i)

}}return

resultset;

可以看出,當b表資料較大時不適合使用in(),因為它會b表資料全部遍歷一次.

如:a表有10000條記錄,b表有1000000條記錄,那麼最多有可能遍歷10000*1000000次,效率很差.

再如:a表有10000條記錄,b表有100條記錄,那麼最多有可能遍歷10000*100次,遍歷次數大大減少,效率大大提公升.

結論:in()適合b錶比a表資料小的情況

select a.*from a a

where exists(select 1 from b b where a.id=b.id)

以上查詢使用了exists語句,exists()會執行a.length次,它並不快取exists()結果集,因為exists()結果集的內容並不重要,重要的是結果集中是否有記錄,如果有則返回true,沒有則返回false.

它的查詢過程類似於以下過程

list resultset=;

array a=(select *from a)

for(int i=0;i)

}return

resultset;

當b錶比a表資料大時適合使用exists(),因為它沒有那麼遍歷操作,只需要再執行一次查詢就行.

如:a表有10000條記錄,b表有1000000條記錄,那麼exists()會執行10000次去判斷a表中的id是否與b表中的id相等.

如:a表有10000條記錄,b表有100000000條記錄,那麼exists()還是執行10000次,因為它只執行a.length次,可見b表資料越多,越適合exists()發揮效果.

再如:a表有10000條記錄,b表有100條記錄,那麼exists()還是執行10000次,還不如使用in()遍歷10000*100次,因為in()是在記憶體裡遍歷比較,而exists()需要查詢資料庫,我們都知道查詢資料庫所消耗的效能更高,而記憶體比較很快.

結論:exists()適合b錶比a表資料大的情況

當a表資料與b表資料一樣大時,in與exists效率差不多,可任選乙個使用.

oracle常識(一) in和exist的區別

in 與 exist 的語法比較 select from 資料表 t where t.x in 括號內可以是符合t.x欄位型別的值集合,如 1 2 3 但如果t.x是number型別的時候,似乎這樣的寫法會出問題 也可以是通過另外的select語句查詢出來的值集合,如 select y from 資...

sql中in和exist語句的區別?

in和exists in 是把外表和內錶作hash 連線,而exists是對外表作loop迴圈,每次loop迴圈再對內表進行查詢。如果兩個表中乙個較小,乙個是大表,則子查詢表大的用exists,子查詢錶小的用in 例如 表a 小表 表b 大表 1 select from a where cc in ...

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...