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结果匹配进行过滤
SQL EXISTS的更多相关文章
- LINQ系列:LINQ to SQL Exists/In/Any/All/Contains
1. Any 返回没有Product的Category var expr = from c in context.Categories where !c.Products.Any() select c ...
- SQL exists( select 1 from
use UnlockIndustry select * from Info_Coordinate as A join Info_Employee on A.EmployeeId=Info_Employ ...
- SQL Exists 的用法 转载
比如在Northwind数据库中 有一个查询为 SELECT c.CustomerId, CompanyName FROM Customers c WHERE EXISTS( SELECT O ...
- SQL EXISTS 与 IN
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或FalseEXISTS 指定一个子查询,检测行的存在. EXISTS与IN的使用效率的问题,通常情 ...
- 浅谈sql中的in与not in,exists与not exists的区别
转 浅谈sql中的in与not in,exists与not exists的区别 12月12日北京OSC源创会 —— 开源技术的年终盛典 » sql exists in 1.in和exists ...
- 数据库——SQL中EXISTS怎么用2(转)
数据库sql语句的exists总结 sql exists in 学习 先来比较下语法: --deals=交易表,areas=地域表,例如香港:我们的目的:查看有交易的地域 select * from ...
- sql中exists和notexists用法总结(并和in的比较)
首先头脑中有三点概念: 1.EXISTS子查询找到的提交 NOT EXISTS 子查询中 找不到的提交 说明:不要去翻译为存在和不存在,把脑袋搞晕. 2.建立程序循环的概念,这是一个动态的查询过程.如 ...
- 小表驱动大表, 兼论exists和in
给出两个表,A和B,A和B表的数据量, 当A小于B时,用exists select * from A where exists (select * from B where A.id=B.id) ex ...
- Stream Processing 101: From SQL to Streaming SQL in 10 Minutes
转自:https://wso2.com/library/articles/2018/02/stream-processing-101-from-sql-to-streaming-sql-in-ten- ...
随机推荐
- Vuejs选项卡案例
css .active { color: red; border-bottom: 1px solid red; } ul li { padding: 0 15px; float: left; list ...
- 【转】MVC中code first方式开发,数据库的生成与更新(Ef6)
一,在models文件夹中,建立相应的model文件 这里注意一点,这里建立的class名,就是数据库里表的名字. 在这里面,可以建立表之间的关系. 这里要说明一点的事 ...
- secFox setting
secFox setting 1● load file 2● install 3● use
- react router @4 和 vue路由 详解(三)react如何在路由里面定义一个子路由
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 5.react如何在路由里面定义一个子路由? a.引入在需要子路由的页面引入Rout ...
- 适应c++ 新特性 - 与我 - 多年传统方式开发(新特性参考微软标准:https://msdn.microsoft.com/zh-cn/library/hh279654.aspx)
公司同事都在积极使用c++的新特性,并对其赞不绝口,而自己一直做着传统的c++开发方式,到底这些新特性如何,又是怎么提高开发效率的,我依然在疑问当中,从同事的说法和实际代码操练里,确实在减少代码量,集 ...
- Asp.Net 中 HTTP 和 HTTPS 切换
Asp.Net 中 HTTP 和 HTTPS 切换 目的 HTTP,超文本传输协议,明文传输,无状态,服务器默认端口80 HTTPS,具有SSL加密的HTTP,加密传输,需要申请ca证书,服务器默 ...
- 学习Linux系统的方法经验
Linux系统是一个开源的高效的以命令行为主的操作系统,主要用于服务器操作系统领域.对于Linux操作系统更多详细准确的解释大家可以网上找到<Linux就该这么学>的第0章介绍的比较详细: ...
- Java使用wait() notify()方法操作共享资源
Java多个线程共享资源: 1)wait().notify()和notifyAll()方法是本地方法,并且为final方法,无法被重写. 2)调用某个对象的wait()方法能让当前线程阻塞,并且当前线 ...
- 卷积与反卷积以及步长stride
1. 卷积与反卷积 如上图演示了卷积核反卷积的过程,定义输入矩阵为 I(4×4),卷积核为 K(3×3),输出矩阵为 O(2×2): 卷积的过程为:Conv(I,W)=O 反卷积的过称为:Deconv ...
- Mac生成rsa证书
详细参考大神 https://blog.csdn.net/qq_30513483/article/details/51242338 RSA为一种加密算法,生成的文件格式有两种,一种是PEM格式,另一种 ...