SQL EXISTS
一直对exists的用法不清楚,本次学习exists,并作出相应学习总结。
1.创造测试环境
SYS@ora122>create table a(id int,name varchar2());
SYS@ora122>insert into a values(,'a');
SYS@ora122>insert into a values(,'b');
SYS@ora122>insert into a values(,'c');
SYS@ora122>insert into a values(,'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
---- --------------------
a
b
c
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:
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| | SELECT STATEMENT | | | | ()| :: |
|* | FILTER | | | | | |
| | TABLE ACCESS FULL| A | | | ()| :: |
|* | INDEX RANGE SCAN | A_NAME | | | ()| :: |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
- filter( NOT EXISTS (SELECT FROM "A" "A" WHERE "NAME"='a'))
- 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结果匹配进行过滤
最新文章
- jquery的选择器
- MAC中设置java环境变量和MAVEN
- VS2013与MySql建立连接;您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧
- jQuery实例-简单选项卡-【一些常见方法(2)-练习】
- ASP.NET C# 有程序集加不了解决办法
- JavaScript实现复制功能
- Struts2基础学习(八)—Struts2防止表单重复提交
- Pygame常用方法
- [Swift]LeetCode142. 环形链表 II | Linked List Cycle II
- centos7 + php7
- 配置tomcat的https域名
- GO入门——4. 数组、切片与map
- CONVERT(varchar(10), getdate(), 120 )中数字参数用法
- vue mixins的使用
- [嵌入式培训 笔记]-----Vim编辑器使用简介
- mysql的cast()函数
- EF选择Mysql数据源
- apache htaccess 一个 例子
- 基于apache httpclient的常用接口调用方法
- Python基础二--基本控制语句
热门文章
- [转载]Python正则表达式匹配反斜杠'\'问题
- 一、SQLite学习
- 线程池 execute 和 submit 的区别
- java反序列化漏洞原理研习
- FFmpeg点播慢的最终方案
- vue中alert toast confirm loading 公用
- swagger注释@API详细说明
- 转 举例说明使用MATLAB Coder从MATLAB生成C/C++代码步骤
- 【框架】用excel管理测试用例需要的参数数据(二)
- python 自然语言处理(四)____词典资源