NOT IN 與NOT EXISTS的區別何在?
我測試了in與exists對於查詢是沒有區別的。不管是在表與表之間的連線方式,還是單表的訪問方式都一致,網上經常說要用exists代替in是錯誤的說法。而not in 與not exists的區別則大相徑庭,not exists操作明顯大大優於not in。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
先看看in與exists的實驗。
SQL> select count(*) from a where object_id in (select object_id from b);
COUNT(*)
----------
129835
已用時間: 00: 00: 00.40
執行計劃
----------------------------------------------------------
Plan hash value: 1819916167
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 10 | | 1538 (2)| 00:00:19 |
| 1 | SORT AGGREGATE | | 1 | 10 | | | |
|* 2 | HASH JOIN SEMI | | 94312 | 921K| 2160K| 1538 (2)| 00:00:19 |
| 3 | TABLE ACCESS FULL| A | 129K| 633K| | 482 (2)| 00:00:06 |
| 4 | TABLE ACCESS FULL| B | 209K| 1024K| | 770 (2)| 00:00:10 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"="OBJECT_ID")
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
4878 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(*) from a where exists (select 1 from b where b.object_id=a.object_id);
COUNT(*)
----------
129835
已用時間: 00: 00: 00.38
執行計劃
----------------------------------------------------------
Plan hash value: 1298564723
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 10 | | 1538 (2)| 00:00:19 |
| 1 | SORT AGGREGATE | | 1 | 10 | | | |
|* 2 | HASH JOIN ANTI | | 35524 | 346K| 2160K| 1538 (2)| 00:00:19 |
| 3 | TABLE ACCESS FULL| A | 129K| 633K| | 482 (2)| 00:00:06 |
| 4 | TABLE ACCESS FULL| B | 209K| 1024K| | 770 (2)| 00:00:10 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("B"."OBJECT_ID"="A"."OBJECT_ID")
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
4880 consistent gets
0 physical reads
0 redo size
408 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
可以看到in與exists查詢時間與邏輯讀都基本一樣。而且表之間的連線順序,單表訪問方式也一樣。
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
先看看in與exists的實驗。
SQL> select count(*) from a where object_id in (select object_id from b);
COUNT(*)
----------
129835
已用時間: 00: 00: 00.40
執行計劃
----------------------------------------------------------
Plan hash value: 1819916167
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 10 | | 1538 (2)| 00:00:19 |
| 1 | SORT AGGREGATE | | 1 | 10 | | | |
|* 2 | HASH JOIN SEMI | | 94312 | 921K| 2160K| 1538 (2)| 00:00:19 |
| 3 | TABLE ACCESS FULL| A | 129K| 633K| | 482 (2)| 00:00:06 |
| 4 | TABLE ACCESS FULL| B | 209K| 1024K| | 770 (2)| 00:00:10 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("OBJECT_ID"="OBJECT_ID")
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
4878 consistent gets
0 physical reads
0 redo size
410 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> select count(*) from a where exists (select 1 from b where b.object_id=a.object_id);
COUNT(*)
----------
129835
已用時間: 00: 00: 00.38
執行計劃
----------------------------------------------------------
Plan hash value: 1298564723
------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 10 | | 1538 (2)| 00:00:19 |
| 1 | SORT AGGREGATE | | 1 | 10 | | | |
|* 2 | HASH JOIN ANTI | | 35524 | 346K| 2160K| 1538 (2)| 00:00:19 |
| 3 | TABLE ACCESS FULL| A | 129K| 633K| | 482 (2)| 00:00:06 |
| 4 | TABLE ACCESS FULL| B | 209K| 1024K| | 770 (2)| 00:00:10 |
------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("B"."OBJECT_ID"="A"."OBJECT_ID")
統計資訊
----------------------------------------------------------
0 recursive calls
0 db block gets
4880 consistent gets
0 physical reads
0 redo size
408 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
可以看到in與exists查詢時間與邏輯讀都基本一樣。而且表之間的連線順序,單表訪問方式也一樣。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22034023/viewspace-662229/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle in與exists 的區別Oracle
- exists和not exists及in和not in的用法與區別
- EXISTS、IN、NOT EXISTS、NOT IN的區別(ZT)
- 詳解not in與not exists的區別與用法
- 大神級回答exists與in的區別
- EXISTS、IN、NOT EXISTS、NOT IN用法區別
- [精選] SQL中的IN與NOT IN、EXISTS與NOT EXISTS的區別及效能分析SQL
- 對線面試官:SQL中的IN與NOT IN、EXISTS與NOT EXISTS的區別及效能分析面試SQL
- in 和 exists區別
- 子查詢中的IN與EXISTS的區別(轉)
- oracle中in和exists的區別Oracle
- 在關聯子查詢中in與exists的區別
- oracle sql tuning_in與exists的區別_轉摘OracleSQL
- SQL語句中exists和in的區別SQL
- (轉)ORACLE 中IN和EXISTS的區別Oracle
- in和exists的一些區別
- SQL中IN和EXISTS用法的區別SQL
- fs.exists 與 fs.access的區別是什麼
- NOT IN ,NOT EXISTS 區別 11G改變
- SQL中IN,NOT IN,EXISTS,NOT EXISTS的用法和差別SQL
- impdp匯入時使用table_exists_action引數的區別
- not exists 中from 後面不同寫法帶來的效率區別
- ??與?:的區別
- IN&EXISTS與NOT IN&NOT EXISTS 的優化原則的討論優化
- MySQL的@與@@區別MySql
- mybatis #與$的區別MyBatis
- Null 與 “” 的區別Null
- &與&&, |與||區別
- MySQL之in與existsMySql
- in與exist , not in與not exist 的區別
- in,exists和not exists ,not in與null的一些關係記載Null
- CentOS 與 Ubuntu 的區別CentOSUbuntu
- artice與section的區別
- GET 與 POST 的區別
- WebSocket 與 Socket 的區別Web
- Postgresql與MySQL的區別MySql
- chown與chmod的區別
- LESS與SASS的區別