not exists and not in compare
时间:2011-02-22
来源:互联网
SQL> select * from tbl_test1;
COL1 COL2
———- ———-
2 2
SQL> select * from tbl_test;
COL1 COL2
———- ———-
1 2
2 2
2
SQL> rollback;
Rollback complete.
SQL> select * from tbl_test1;
COL1 COL2
———- ———-
2
3 2
2 2
SQL> select * from tbl_test;
COL1 COL2
———- ———-
1 2
2 2
2
SQL> delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
0 rows deleted.
SQL> delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
2 rows deleted.
SQL> select * from tbl_test;
COL1 COL2
———- ———-
1 2
2 2
2
SQL> select * from tbl_test1;
COL1 COL2
———- ———-
2 2
SQL> rollback;
Rollback complete.
SQL> explain plan for delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
——————————————————————————————————————————————–
Plan hash value: 3192674508
———————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
———————————————————————————
| 0 | DELETE STATEMENT | | 1 | 13 | 4 (0)| 00:00:01 |
| 1 | DELETE | TBL_TEST1 | | | | |
|* 2 | FILTER | | | | | |
| 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |
———————————————————————————
Predicate Information (identified by operation id):
—————————————————
2 – filter( NOT EXISTS (SELECT /*+ */ 0 FROM “TBL_TEST” “TBL_TEST”
WHERE LNNVL(“COL1″<>:B1)))
4 – filter(LNNVL(“COL1″<>:B1))
Note
—–
- dynamic sampling used for this statement
22 rows selected.
SQL> explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1)
*
ERROR at line 1:
ORA-00905: missing keyword
SQL> explain plan for delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
——————————————————————————————————————————————–
Plan hash value: 1337621389
———————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
———————————————————————————
| 0 | DELETE STATEMENT | | 2 | 52 | 5 (20)| 00:00:01 |
| 1 | DELETE | TBL_TEST1 | | | | |
|* 2 | HASH JOIN ANTI | | 2 | 52 | 5 (20)| 00:00:01 |
| 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |
———————————————————————————
Predicate Information (identified by operation id):
—————————————————
2 – access(“T1″.”COL1″=”T2″.”COL1″)
Note
—–
- dynamic sampling used for this statement
20 rows selected.
SQL> select * from tbl_test1;
COL1 COL2———- ———- 2 2
SQL> select * from tbl_test;
COL1 COL2———- ———- 1 2 2 2 2
SQL> rollback;
Rollback complete.
SQL> select * from tbl_test1;
COL1 COL2———- ———- 2 3 2 2 2
SQL> select * from tbl_test;
COL1 COL2———- ———- 1 2 2 2 2
SQL> delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
0 rows deleted.
SQL> delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
2 rows deleted.
SQL> select * from tbl_test;
COL1 COL2———- ———- 1 2 2 2 2
SQL> select * from tbl_test1;
COL1 COL2———- ———- 2 2
SQL> rollback;
Rollback complete.
SQL> explain plan for delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT——————————————————————————————————————————————–Plan hash value: 3192674508
———————————————————————————| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |———————————————————————————| 0 | DELETE STATEMENT | | 1 | 13 | 4 (0)| 00:00:01 || 1 | DELETE | TBL_TEST1 | | | | ||* 2 | FILTER | | | | | || 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 ||* 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |———————————————————————————
Predicate Information (identified by operation id):—————————————————
2 – filter( NOT EXISTS (SELECT /*+ */ 0 FROM “TBL_TEST” “TBL_TEST” WHERE LNNVL(“COL1″<>:B1))) 4 – filter(LNNVL(“COL1″<>:B1))
Note—– – dynamic sampling used for this statement
22 rows selected.
SQL> explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) *ERROR at line 1:ORA-00905: missing keyword
SQL> explain plan for delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT——————————————————————————————————————————————–Plan hash value: 1337621389
———————————————————————————| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |———————————————————————————| 0 | DELETE STATEMENT | | 2 | 52 | 5 (20)| 00:00:01 || 1 | DELETE | TBL_TEST1 | | | | ||* 2 | HASH JOIN ANTI | | 2 | 52 | 5 (20)| 00:00:01 || 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 || 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |———————————————————————————
Predicate Information (identified by operation id):—————————————————
2 – access(“T1″.”COL1″=”T2″.”COL1″)
Note—– – dynamic sampling used for this statement
20 rows selected.
not in和not exists通用的前提是第二个表的被关联字段非空
COL1 COL2
———- ———-
2 2
SQL> select * from tbl_test;
COL1 COL2
———- ———-
1 2
2 2
2
SQL> rollback;
Rollback complete.
SQL> select * from tbl_test1;
COL1 COL2
———- ———-
2
3 2
2 2
SQL> select * from tbl_test;
COL1 COL2
———- ———-
1 2
2 2
2
SQL> delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
0 rows deleted.
SQL> delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
2 rows deleted.
SQL> select * from tbl_test;
COL1 COL2
———- ———-
1 2
2 2
2
SQL> select * from tbl_test1;
COL1 COL2
———- ———-
2 2
SQL> rollback;
Rollback complete.
SQL> explain plan for delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
——————————————————————————————————————————————–
Plan hash value: 3192674508
———————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
———————————————————————————
| 0 | DELETE STATEMENT | | 1 | 13 | 4 (0)| 00:00:01 |
| 1 | DELETE | TBL_TEST1 | | | | |
|* 2 | FILTER | | | | | |
| 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |
———————————————————————————
Predicate Information (identified by operation id):
—————————————————
2 – filter( NOT EXISTS (SELECT /*+ */ 0 FROM “TBL_TEST” “TBL_TEST”
WHERE LNNVL(“COL1″<>:B1)))
4 – filter(LNNVL(“COL1″<>:B1))
Note
—–
- dynamic sampling used for this statement
22 rows selected.
SQL> explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1)
*
ERROR at line 1:
ORA-00905: missing keyword
SQL> explain plan for delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
——————————————————————————————————————————————–
Plan hash value: 1337621389
———————————————————————————
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
———————————————————————————
| 0 | DELETE STATEMENT | | 2 | 52 | 5 (20)| 00:00:01 |
| 1 | DELETE | TBL_TEST1 | | | | |
|* 2 | HASH JOIN ANTI | | 2 | 52 | 5 (20)| 00:00:01 |
| 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |
———————————————————————————
Predicate Information (identified by operation id):
—————————————————
2 – access(“T1″.”COL1″=”T2″.”COL1″)
Note
—–
- dynamic sampling used for this statement
20 rows selected.
SQL> select * from tbl_test1;
COL1 COL2———- ———- 2 2
SQL> select * from tbl_test;
COL1 COL2———- ———- 1 2 2 2 2
SQL> rollback;
Rollback complete.
SQL> select * from tbl_test1;
COL1 COL2———- ———- 2 3 2 2 2
SQL> select * from tbl_test;
COL1 COL2———- ———- 1 2 2 2 2
SQL> delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
0 rows deleted.
SQL> delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
2 rows deleted.
SQL> select * from tbl_test;
COL1 COL2———- ———- 1 2 2 2 2
SQL> select * from tbl_test1;
COL1 COL2———- ———- 2 2
SQL> rollback;
Rollback complete.
SQL> explain plan for delete from tbl_Test1 where col1 not in (select col1 from tbl_Test);
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT——————————————————————————————————————————————–Plan hash value: 3192674508
———————————————————————————| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |———————————————————————————| 0 | DELETE STATEMENT | | 1 | 13 | 4 (0)| 00:00:01 || 1 | DELETE | TBL_TEST1 | | | | ||* 2 | FILTER | | | | | || 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 ||* 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |———————————————————————————
Predicate Information (identified by operation id):—————————————————
2 – filter( NOT EXISTS (SELECT /*+ */ 0 FROM “TBL_TEST” “TBL_TEST” WHERE LNNVL(“COL1″<>:B1))) 4 – filter(LNNVL(“COL1″<>:B1))
Note—– – dynamic sampling used for this statement
22 rows selected.
SQL> explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;explain delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) *ERROR at line 1:ORA-00905: missing keyword
SQL> explain plan for delete from tbl_Test1 t1 where not exists (select col1 from tbl_Test t2 where t1.col1=t2.col1) ;
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT——————————————————————————————————————————————–Plan hash value: 1337621389
———————————————————————————| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |———————————————————————————| 0 | DELETE STATEMENT | | 2 | 52 | 5 (20)| 00:00:01 || 1 | DELETE | TBL_TEST1 | | | | ||* 2 | HASH JOIN ANTI | | 2 | 52 | 5 (20)| 00:00:01 || 3 | TABLE ACCESS FULL| TBL_TEST1 | 3 | 39 | 2 (0)| 00:00:01 || 4 | TABLE ACCESS FULL| TBL_TEST | 3 | 39 | 2 (0)| 00:00:01 |———————————————————————————
Predicate Information (identified by operation id):—————————————————
2 – access(“T1″.”COL1″=”T2″.”COL1″)
Note—– – dynamic sampling used for this statement
20 rows selected.
not in和not exists通用的前提是第二个表的被关联字段非空
作者: digoal 发布时间: 2011-02-22
从理论上看,not exists比not in的效率要高。
作者: renxiao2003 发布时间: 2011-02-28
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28