数据库——SQL中EXISTS怎么用2(转)
数据库sql语句的exists总结 sql exists in 学习
先来比较下语法:
--deals=交易表,areas=地域表,例如香港;我们的目的:查看有交易的地域
select * from areas where id in (select city_id from deals);
select * from areas where id in (select city_id from deals where deals.city_id = areas.id);
select * from areas where exists (select null from deals where deals.city_id = areas.id);
区别:
EXISTS语法并没有说哪个字段落在了子查寻的结果中,而是说exists后面的语句执行的结果是不是有记录,只要有记录,则主查询语句就成立。它代表‘存在’,用来引领嵌套查询的子查询,它不返回任何数据,只产生逻辑真值‘true’与逻辑假值‘False’。由EXISTS引出的子查询,其目标列表达式通常都用*(用null也可以),因为带有EXISTS的子查询只返回真值或假值,给出列名没有实际意义。
select * from areas where id in (select city_id from deals where deals.city_id = areas.id);
--子查询的关联其实是一样的,但子查询只要查到一个结果,就返回了,所以效率还是比较高些的
select * from areas where exists (select null from deals where deals.city_id = areas.id);
|
1.
EXISTS的执行流程 select * from t1 where exists ( select null from t2 where y = x ) 可以理解为: for x in ( select * from t1 ) loop if ( exists ( select null from t2 where y = x.x ) then OUTPUT THE RECORD end if end loop 对于in和exists的性能区别: 如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in,反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists。 其实我们区分in和exists主要是造成了驱动顺序的改变(这是性能变化的关键),如果是exists,那么以外层表为驱动表,先被访问,如果是IN,那么先执行子查询,所以我们会以驱动表的快速返回为目标,那么就会考虑到索引及结果集的关系了 另外IN时不对NULL进行处理
如: select 1 from dual where null in (0,1,2,null) 2.NOT IN与NOT EXISTS:
NOT EXISTS的执行流程 select ..... from rollup R where not exists ( select 'Found' from title T where R.source_id = T.Title_ID); 可以理解为: for x in ( select * from rollup ) loop if ( not exists ( that query ) ) then OUTPUT end if; end; 注意:NOT EXISTS与 NOT IN不能完全互相替换,看具体的需求。如果选择的列可以为空,则不能被替换。 例如下面语句,看他们的区别: 对于not in和 not exists的性能区别: 比如: 改成(佳) select ...... |
问题和解决
问题1:
--users表有1000条记录,id自增,id都大于0
select * from users where exists (select * from users limit 0); --输出多少条记录?
select * from users where exists (select * from users where id < 0); --输出多少条记录?
答案(请选中查看):
10000条
0条
原因:
exists查询的本质,只要碰到有记录,则返回true;所以limit根本就不会去管,或者说执行不到。
问题2:
exists可以完全代替in吗?
不能。
例如:
--没有关联字段的情况:枚举常量
select * from areas where id in (4, 5, 6);
--没有关联字段的情况:这样exists对子查询,要么全true,要么全false
select * from areas where id in (select city_id from deals where deals.name = 'xxx');
举个相关exists的sql优化例子:
9、用exists替代in(发现好多程序员不知道这个怎么用):
在许多基于基础表的查询中,为了满足一个条件,往往需要对另一个表进行联接。
在这种情况下,使用exists(或not exists)通常将提高查询的效率。
举例:
(低效)
select ... from table1 t1 where t1.id > 10 and pno in (select no from table2 where name like 'www%');
(高效)
select ... from table1 t1 where t1.id > 10 and exists (select 1 from table2 t2 where t1.pno = t2.no and name like 'www%');
10、用not exists替代not in:
在子查询中,not in子句将执行一个内部的排序和合并。
无论在哪种情况下,not in都是最低效的 (因为它对子查询中的表执行了一个全表遍历)。
为了避免使用not in,我们可以把它改写成外连接(Outer Joins)或not exists。
11、用exists替换distinct:
当提交一个包含一对多表信息的查询时,避免在select子句中使用distinct. 一般可以考虑用exists替换
举例:
(低效)
select distinct d.dept_no, d.dept_name from t_dept d, t_emp e where d.dept_no = e.dept_no;
(高效)
select d.dept_no, d.dept_name from t_dept d where exists (select 1 from t_emp where d.dept_no = e.dept_no);
exists使查询更为迅速,因为RDBMS核心模块将在子查询的条件一旦满足后,立刻返回结果.
12、用表连接替换exists:
通常来说,采用表连接的方式比exists更有效率。
举例:
(低效)
select ename from emp e where exists (select 1 from dept where dept_no = e.dept_no and dept_cat = 'W');
SELECT ENAME
(高效)
select ename from dept d, emp e where e.dept_no = d.dept_no and dept_cat = 'W';
R
R
R
R
R
R
数据库——SQL中EXISTS怎么用2(转)的更多相关文章
- 数据库——SQL中EXISTS怎么用3(转)
有一个查询如下: 1 SELECT c.CustomerId, CompanyName 2 FROM Customers c 3 WHERE EXISTS( 4 SELECT Or ...
- 数据库——SQL中EXISTS怎么用1(转)
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False 方法/步骤 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返 ...
- sql 中 exists用法
SQL中EXISTS的用法 比如在Northwind数据库中有一个查询为SELECT c.CustomerId,CompanyName FROM Customers cWHERE EXISTS(S ...
- SQL中EXISTS怎么用[转]
SQL中EXISTS怎么用 1 2 3 4 分步阅读 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False 方法/步骤 1 EXISTS用于 ...
- SQL中EXISTS和IN用法
SQL中EXISTS的用法 指定一个子查询,检测行的存在. 语法:EXISTS subquery 参数:subquery 是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INT ...
- sql中exists和not exists的用法
该文转载自:http://www.cnblogs.com/mytechblog/articles/2105785.html sql中exists,not exists的用法 exists : 强调的是 ...
- 十、SQL中EXISTS的用法 十三、sql server not exists
十.SQL中EXISTS的用法 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False EXISTS 指定一个子查询,检测 行 的存在. 语法 ...
- 数据库SQL中case when函数的用法
Case具有两种格式,简单Case函数和Case搜索函数.这两种方式,可以实现相同的功能.简单Case函数的写法相对比较简洁,但是和Case搜索函数相比,功能方面会有些限制,比如写判断式. 简单Cas ...
- sql中exists和notexists用法总结(并和in的比较)
首先头脑中有三点概念: 1.EXISTS子查询找到的提交 NOT EXISTS 子查询中 找不到的提交 说明:不要去翻译为存在和不存在,把脑袋搞晕. 2.建立程序循环的概念,这是一个动态的查询过程.如 ...
随机推荐
- [转]Vue生态系统中的库
Vue UI组件库 Vuex vux github ui demo:https://github.com/airyland/vux Mint UI 项目主页:http://mint-ui.github ...
- 【LeetCode】128. Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- Android JUnit 入门指南
自动化单元测试可以做许多的事,并帮你节省时间.它也可以被用作快速检验新建工程或进行冒烟测试.始终,单元测试是作为一种有效的.系统的检验应用程序各功能执行的方式.Android SDK支持JUnit的自 ...
- js 停止事件冒泡 阻止浏览器的默认行为(阻止超连接 #)
在前端开发工作中,由于浏览器兼容性等问题,我们会经常用到“停止事件冒泡”和“阻止浏览器默认行为”. 1..停止事件冒泡 JavaScript代码 //如果提供了事件对象,则这是一个非IE浏览器if ( ...
- go学习笔记(3) -- package fmt
package fmt fmt包实现了格式化的I/O函数,这点类似C语言中的printf和scanf,但是更加简单,其中的格式“占位符”衍生自 C 占位符 一般占位符 布尔占位符 浮点数及其复合构成占 ...
- C#开发Windows Services服务--服务安装失败的解决办法
问题1:“System.Security.SecurityException:未找到源,但未能搜索某些或全部事件日志.不可访问的日志: Security.” 正在运行事务处理安装. 正在开始安装的“安 ...
- DBA_实践指南系列3_Oracle Erp R12系统克隆Clone(案例)
2013-12-03 Created By BaoXinjian
- Python min() 方法
描述 Python min() 方法返回字符串中最小的字母(26个字母中最小的是A). 语法 min() 方法语法: min(S) 参数 S -- 字符串. 返回值 返回字符串中最小的字母. 实例 以 ...
- docker 概念初识
1 docker 优势,一次创建或配置可以在任意时间.任意环境让应用正常的运行. 快速交付和部署,开发人员可以使用镜像快速构建一套标准的开发环境:开发完后测试和运维人员可以直接使用相同的环境来部署代码 ...
- mysql-5.7 innodb_file_per_table 详解
一.innodb_file_per_table 的简要说明: 在很久很久以前也就是说还没有innodb_file_per_table 的那个年代,所有的innodb表的数据都是保存在innodb系统表 ...