一直对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. 八、持久层框架(MyBatis)

    一.基于MyBatis的CRUD 1.首先是配置文件Category.xml修改 一次性修改配置文件Category.xml,提供CRUD对应的sql语句. <?xml version=&quo ...

  2. Talend 数据转换

    2个系统都有客户信息,产品信息. 要从一个系统a导出数据给另一个系统b用. 有2个方法. 1.在a系统的客户表,产品表加一个字段,记录b系统对应的ID,导出时直接用sql转换了. 2. 用ETL工具转 ...

  3. PAT-GPLT训练集 L1-039 古风排版

    PAT-GPLT训练集 L1-039 古风排版 注意:在输入字符串时,最后一个字符为'\0', 虽然输出时显示为空格,但是系统检查为‘\0’!!! 代码: #include<stdio.h> ...

  4. 【框架】PageObject(一)

    1.目的:为了将元素的find方法和业务逻辑分开来.如果元素的页面位置发生了变化,只需改动一个文件,而不影响业务的实现. 2.原理:一般一个页面对应一个class,在class里描述所有要用到的web ...

  5. learning ddr pagesize calculate

    example: if DDR is 512MB*16*8 COLBITS = 10,  A0-A9 be used for cloumn address. ORG = 16 ,  each bank ...

  6. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  7. sass嵌套风格

    1.嵌套输出方式 nested Sass 提供了一种嵌套显示 CSS 文件的方式.例如 nav { ul { margin:; padding:; list-style: none; } li { d ...

  8. :状态模式:GumballMachine

    #ifndef __STATE_H__ #define __STATE_H__ #include <iostream> #include<stdlib.h> using nam ...

  9. C++11智能指针 share_ptr,unique_ptr,weak_ptr用法

    0x01  智能指针简介  所谓智能指针(smart pointer)就是智能/自动化的管理指针所指向的动态资源的释放.它是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动 ...

  10. [PyImageSearch] Ubuntu16.04 使用深度学习和OpenCV实现物体检测

    上一篇博文中讲到如何用OpenCV实现物体分类,但是接下来这篇博文将会告诉你图片中物体的位置具体在哪里. 我们将会知道如何使用OpenCV‘s的dnn模块去加载一个预训练的物体检测网络,它能使得我们将 ...