sql中的in与not in,exists与not exists的区别

1、in和exists

in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的。如果查询的两个表大小相当,那么用in和exists差别不大;如果两个表中一个较小一个较大,则子查询表大的用exists,子查询表小的用in;

例如:表A(小表),表B(大表)

select * from A where cc in(select cc from B)  -->效率低,用到了A表上cc列的索引;

select * from A where exists(select cc from B where cc=A.cc)  -->效率高,用到了B表上cc列的索引。

相反的:

select * from B where cc in(select cc from A)  -->效率高,用到了B表上cc列的索引

select * from B where exists(select cc from A where cc=B.cc)  -->效率低,用到了A表上cc列的索引。

2、not in 和not exists

not in 逻辑上不完全等同于not exists,如果你误用了not in,小心你的程序存在致命的BUG,请看下面的例子:

create table #t1(c1 int,c2 int);

create table #t2(c1 int,c2 int);

insert into #t1 values(1,2);

insert into #t1 values(1,3);

insert into #t2 values(1,2);

insert into #t2 values(1,null);

select * from #t1 where c2 not in(select c2 from #t2);  -->执行结果:无

select * from #t1 where not exists(select 1 from #t2 where #t2.c2=#t1.c2)  -->执行结果:1  3

正如所看到的,not in出现了不期望的结果集,存在逻辑错误。如果看一下上述两个select 语句的执行计划,也会不同,后者使用了hash_aj,所以,请尽量不要使用not in(它会调用子查询),而尽量使用not exists(它会调用关联子查询)。如果子查询中返回的任意一条记录含有空值,则查询将不返回任何记录。如果子查询字段有非空限制,这时可以使用not in,并且可以通过提示让它用hasg_aj或merge_aj连接。

如果查询语句使用了not in,那么对内外表都进行全表扫描,没有用到索引;而not exists的子查询依然能用到表上的索引。所以无论哪个表大,用not exists都比not in 要快。

3、in 与 = 的区别

select name from student where name in(‘zhang‘,‘wang‘,‘zhao‘);

select name from student where name=‘zhang‘ or name=‘wang‘ or name=‘zhao‘

的结果是相同的。

时间: 2024-08-11 03:37:12

sql中的in与not in,exists与not exists的区别的相关文章

sql中update,alter,modify,delete,drop的区别和使用(整理)(转)

关于update和alter: 百度知道上关于update和alter有一个很形象的总结: 一个表有很多字段,一个字段里有很多数据. 一个家有很多房间,一个房间里有很多家具. update是用来将衣柜改成书架的. alter是用来将厨房改成厕所的. 把卧室改成厕所: alter table 你家 change 厨房 厕所 varchar(8); 在你的家里面加一个厕所: alter table 你家add 厕所 varchar(8);(8表示厕所8平米) 修改厕所大小: alter table

sql中update,alert,modify,delete,drop的区别和使用(整理)

关于update和alert: 百度知道上关于update和alert有一个很形象的总结: 一个表有很多字段,一个字段里有很多数据. 一个家有很多房间,一个房间里有很多家具. update是用来将衣柜改成书架的. alert是用来将厨房改成厕所的. 把卧室改成厕所: alert table 你家 change 厨房 厕所 varchar(8); 在你的家里面加一个厕所: alert table 你家add 厕所 varchar(8);(8表示厕所8平米) 修改厕所大小: alert table

sql的left join 、right join 、inner join之间的区别

sql中left join .right join .inner join之间的区别 left join (左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 : right join (右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录: inner join (等值连接) 只返回两个表中联结字段相等的行. 原文地址:https://www.cnblogs.com/baixiuhua/p/8862076.html

SQL中if exists用法细节

用if exists建表[转] 1 判断数据库是否存在 Sql代码 if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名]  if exists (select * from sys.databases where name = ’数据库名’) drop database [数据库名] 2 判断表是否存在 Sql代码 if exists (select * from sysobjects w

sql中exists,not exists的用法

转 sql中exists,not exists的用法 exists : 强调的是是否返回结果集,不要求知道返回什么, 比如:  select name from student where sex = 'm' and mark exists(select 1 from grade where ...) ,只要exists引导的子句有结果集返回,那么exists这个条件就算成立了,大家注意返回的字段始终为1,如果改成"select 2 from grade where ...",那么返回

SQL中in和exists的用法和效率区别

1. sql中in和exists的区别效率问题 2. SQL中IN和EXISTS用法的区别

SQL中EXISTS怎么用[转]

SQL中EXISTS怎么用 1 2 3 4 分步阅读 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False 方法/步骤 1 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或FalseEXISTS 指定一个子查询,检测 行 的存在. 语法: EXISTS subquery参数: subquery 是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INTO 关键字).

数据库——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 *

数据库——SQL中EXISTS怎么用3(转)

有一个查询如下: 1 SELECT c.CustomerId, CompanyName   2 FROM Customers c   3 WHERE EXISTS(   4     SELECT OrderID FROM Orders o   5     WHERE o.CustomerID = cu.CustomerID)   这里面的EXISTS是如何运作呢?子查询返回的是OrderId字段,可是外面的查询要找的是CustomerID和CompanyName字段,这两个字段肯定不在Order

sql中 in 、not in 、exists、not exists 用法和差别

exists (sql 返回结果集为真) not exists (sql 不返回结果集为真) 如下: 表A ID NAME 1    A1 2    A2 3  A3 表B ID AID NAME 1    1 B1 2    2 B2 3    2 B3 表A和表B是1对多的关系 A.ID => B.AID SELECT ID,NAME FROM A WHERE EXIST (SELECT * FROM B WHERE A.ID=B.AID) 执行结果为 1 A1 2 A2 原因可以按照如下分