一直對exists的用法不清楚,本次學習exists,並作出相應學習總結。
1.創造測試環境
SYS@ora122>create table a(id int,name varchar2(200)); SYS@ora122>insert into a values(1,'a'); SYS@ora122>insert into a values(2,'b'); SYS@ora122>insert into a values(2,'c'); SYS@ora122>insert into a values(3,'a'); SYS@ora122>commit;
2.簡單exists 舉例說明,對於單個exists ,子查詢返回null,則結果為null,子查詢非null,則查詢顯示輸出結果
SYS@ora122> select * from a where exists(select id from a where name='a') ID NAME ---- -------------------- 1 a 2 b 2 c 3 a
SYS@ora122> select * from a where exists(select id from a where name='d');
no rows selected
SYS@ora122>select * from a where not exists(select id from a where name='a'); no rows selected
3.創建測試索引,觀察執行計划,找到執行計划的規律
SYS@ora122>create index a_id on a(id); SYS@ora122>create index a_name on a(name); SYS@ora122>set autotrace on SYS@ora122>select id from a where not exists(select id from a where name='a'); no rows selected Execution Plan ---------------------------------------------------------- Plan hash value: 3514764880 ----------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 52 | 4 (0)| 00:00:01 | |* 1 | FILTER | | | | | | | 2 | TABLE ACCESS FULL| A | 4 | 52 | 3 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | A_NAME | 2 | 204 | 1 (0)| 00:00:01 | ----------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter( NOT EXISTS (SELECT 0 FROM "A" "A" WHERE "NAME"='a')) 3 - access("NAME"='a')
可以得知,表A全表掃、not exists 查詢結果為2行記錄, 最終filter 返回null值
SQL改寫
select id from a where id>2 and not exists(select id from a where name='a');
no rows selected
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01 |
|* 1 | FILTER | | | | | |
|* 2 | INDEX RANGE SCAN| A_ID | 1 | 13 | 1 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN| A_NAME | 2 | 204 | 1 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter( NOT EXISTS (SELECT 0 FROM "A" "A" WHERE "NAME"='a'))
2 - access("ID">2)
3 - access("NAME"='a') 執行計划並未改變多少
SYS@ora122>SELECT 0 FROM "A" "A" WHERE "NAME"='a';
0
----------
0
0
SYS@ora122>select id from a where name='a';
ID
----
1
3
SQL改寫
SYS@ora122>select * from a where name='a' and not exists(select id from a where id=8);
ID NAME
---- --------------------
1 a
3 a
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 230 | 3 (0)| 00:00:01 |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS BY INDEX ROWID BATCHED| A | 2 | 230 | 2 (0)| 00:00:01 |
|* 3 | INDEX RANGE SCAN | A_NAME | 2 | | 1 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | A_ID | 1 | 13 | 1 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter( NOT EXISTS (SELECT 0 FROM "A" "A" WHERE "ID"=8))
3 - access("NAME"='a')
4 - access("ID"=8)
與IN不同的是,
in 相互對等匹配返回結果
exists 要么都可以,要么都失敗
not exists 與exists本質相同
not in ,則是第一個查詢結果與not in結果匹配進行過濾