一直对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的更多相关文章

  1. 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 ...

  2. SQL exists( select 1 from

    use UnlockIndustry select * from Info_Coordinate as A join Info_Employee on A.EmployeeId=Info_Employ ...

  3. SQL Exists 的用法 转载

    比如在Northwind数据库中     有一个查询为 SELECT c.CustomerId, CompanyName FROM Customers c WHERE EXISTS( SELECT O ...

  4. SQL EXISTS 与 IN

    EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或FalseEXISTS 指定一个子查询,检测行的存在. EXISTS与IN的使用效率的问题,通常情 ...

  5. 浅谈sql中的in与not in,exists与not exists的区别

    转 浅谈sql中的in与not in,exists与not exists的区别   12月12日北京OSC源创会 —— 开源技术的年终盛典 »   sql exists in 1.in和exists ...

  6. 数据库——SQL中EXISTS怎么用2(转)

    数据库sql语句的exists总结 sql exists in 学习 先来比较下语法: --deals=交易表,areas=地域表,例如香港:我们的目的:查看有交易的地域 select * from ...

  7. sql中exists和notexists用法总结(并和in的比较)

    首先头脑中有三点概念: 1.EXISTS子查询找到的提交 NOT EXISTS 子查询中 找不到的提交 说明:不要去翻译为存在和不存在,把脑袋搞晕. 2.建立程序循环的概念,这是一个动态的查询过程.如 ...

  8. 小表驱动大表, 兼论exists和in

    给出两个表,A和B,A和B表的数据量, 当A小于B时,用exists select * from A where exists (select * from B where A.id=B.id) ex ...

  9. 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- ...

随机推荐

  1. 【转】JS常用函数整合库 lutils

    lutils 此工具包是在 outils 的基础上,加上个人平时收集的代码片段进行的二次整合 outils的GitHub:https://github.com/proYang/outils/blob/ ...

  2. Windows开启远程桌面服务(Win10)

    进入控制面版,找到远程设置 应用确定后Windows服务即会被启动.

  3. System.gc()和-XX:+DisableExplicitGC启动参数,以及DirectByteBuffer的内存释放

    首先我们修改下JVM的启动参数,重新运行之前博客中的代码.JVM启动参数和测试代码如下: -verbose:gc -XX:+PrintGCDetails -XX:+DisableExplicitGC ...

  4. Boost中的网络库ASIO,nginx

    boost C++ 本身就是跨平台的,在Linux.Unix.Windos上都可以使用. Boost.Asio  针对网络编程,很多服务端C++开发使用此库. 这个库在以下的平台和编译器上测试通过: ...

  5. 【Java算法】条件运算符

    利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. 条件运算符的表达式为: 表达式1?表达式2:表达式3 当表达式1为true ...

  6. Win10系列:JavaScript 项目模板中的文件和项模板文件

    通过上面内容的学习,相信读者已经对各种项目模板和项模板有了大致的了解,本节将进一步介绍项目模板中默认包含的项目文件以及项模板文件,首先讲解这些文件中的初始内容以及作用,然后介绍在一个页面中如何添加控件 ...

  7. [Codeforces721E]Road to Home

    Problem 有一条长为l的公路(可看为数轴),n盏路灯,每盏路灯有照射区间且互不重叠. 有个人要走过这条公路,他只敢在路灯照射的地方唱歌,固定走p唱完一首歌,歌曲必须连续唱否则就要至少走t才能继续 ...

  8. 深度学习----Xavier初始化方法

    “Xavier”初始化方法是一种很有效的神经网络初始化方法,方法来源于2010年的一篇论文<Understanding the difficulty of training deep feedf ...

  9. Thêm Một Lần Đau--错错错--IPA--越南语

    越南国际天团HKT的名曲.

  10. String转换成Boolean类型

    Boolean.valueOf()方法: public static Boolean valueOf(String s) { return toBoolean(s) ? TRUE : FALSE; } ...