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- ...
随机推荐
- Scheduler & Task & Worker & Thread & Request & Session & Connection of SQL Server
MSSQL一直以来被人们认为简单.好学,但等到大家掌握了入门操作,深入理解起来又觉得非常的“拧巴”,尤其是对用惯了Oracle的同学来说,究其根本原因,无非是MSSQL引入和暴露了太多的概念.细节和理 ...
- 什么是 开发环境、测试环境、生产环境、UAT环境、仿真环境
开发环境:开发环境是程序猿们专门用于开发的服务器,配置可以比较随意, 为了开发调试方便,一般打开全部错误报告. 测试环境:一般是克隆一份生产环境的配置,一个程序在测试环境工作不正常,那么肯定不能把它发 ...
- moment.js 常用(几天前、相差几天、自然周、自然月)
let pickDate = moment(this.searchForm.date); let firstDay = pickDate.day(0).format('YYYYMMDD');//上周天 ...
- kdbg安装使用教程(kali)
一.背景说明 所谓调试者,主要就是下断点.观察变量,不是太复杂的事情也不用太复杂的工具. 但具体到linux平台而言,gdb本来多敲几下命令也不是不可以的事,但是一个屏幕就那么大打印出一堆东西又乱又看 ...
- C++ 保留有效小数 保留有效数字
1.需要头文件 #include <iomanip> 2. 要保留两位有效小数 cout<<setiosflags(ios::fixed)<<setprecisio ...
- forget stereo step word out8
1★ stereo st əri əu 立体的 2★ step st əp 后,前妻所生,步骤
- Java Web(四) 过滤器Filter
Filter概述 Filter意为滤镜或者过滤器,用于在Servlet之外对request或者response进行修改.Filter提出了过滤链的概念.一个FilterChain包括多个Filter. ...
- break&&continue
break和continue的区别: 1. 当它们用在循环语句的循环体时,break用于立即退出本层循环,而continue仅仅结束本次循环(本次循环体内不执行continue语句后的其它语句,但下一 ...
- HTML(四)Form标签
<form>…</form> 定义供用户输入的 HTML 表单 例子 <html> <body> <form method="ge ...
- Grafana展示報表數據的配置(二)
一.Grafana以圖表的形式展示KPI報表的結果數據1.按照日期顯示數據達標量與未達標量2.顯示當前報表的最大值.最小值.平均值.總量3.報表結果數據的鏈接分享與頁面嵌入,用戶無需登錄直接訪問報表統 ...