Oracle 的 not in 和 not exists

1.  regexp_substr 字符串匹配

select regexp_substr('陳凱-chenk','[^-]+',1,1), instr('yuechaotianyuechao','ao') position from dual;

起始位置,從第幾個字符開始正則表達式匹配(默認爲1),

匹配第一個‘-’   

得到的結果是:陳凱

2. not  in  或者 in 

爲什麼not in的效率會低?

因爲要判斷內表查詢字段是否有空值,如果有空值,則使用的時 FILTER ;如果明確指出關聯字段時不空的,則使用hash連接,速度會極大的提升

not in 的子查詢語句結果中不能有null值,否則就查詢不出結果

in (1,2,null) 其中null是會被忽略掉的;

判斷字段是否是null,只能使用 is null  或者  is not  null  來判斷

表A:  表B:

select * from A a where a.id not in (select id from B b  ) 

    如果 table2 中col1有null值,則查詢結果就是空

修改1:select * from A a where a.id not in (select id from B b where b.id is not null )   正確

修改2:select * from a where not exists (select * from b where b.id = a.id)   正確

 

明確通知Oracle,not in 的字段查詢時,是不會有空值參與的。這樣表連接就使用HASH JOIN連接表了。

或者兩張表的關聯字段設置不允許爲null,即可。

select * from a   where col is not null and col not in (select col from b where col is not null);

3。not  exists

1、對於not exists查詢,內表存在空值對查詢結果沒有影響;對於not in查詢,內表存在空值將導致最終的查詢結果爲空。

2、對於not exists查詢,外表存在空值,存在空值的那條記錄最終會輸出;對於not in查詢,外表存在空值,存在空值的那條記錄最終將被過濾,其他數據不受影響。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章