SQL中的EXISTS子句和IN子句有什么区别?

什么时候应该使用EXISTS,什么时候应该使用IN?


当前回答

如果你可以用where in代替where exists,那么where in可能更快。

使用where in或where exists 将遍历父结果的所有结果。不同之处在于where exists将导致大量依赖子查询。如果你可以防止依赖子查询,那么where in将是更好的选择。

例子

假设我们有10,000家公司,每家公司有10个用户(因此我们的用户表有100,000个条目)。现在假设您希望通过用户名或公司名查找用户。

下面使用were exists查询的执行时间为141ms:

select * from `users` 
where `first_name` ='gates' 
or exists 
(
  select * from `companies` 
  where `users`.`company_id` = `companies`.`id`
  and `name` = 'gates'
)

这是因为对每个用户执行一个依赖子查询:

然而,如果我们避免exists查询并使用:

select * from `users` 
where `first_name` ='gates' 
or users.company_id in  
(
    select id from `companies` 
    where  `name` = 'gates'
)

然后避免依赖子查询,查询将在0,012毫秒内运行

其他回答

基于规则优化器:

当子查询结果非常大时,EXISTS比IN快得多。 当子查询结果很小时,IN比EXISTS快。

基于成本优化器:

没有区别。

哪个更快取决于内部查询获取的查询数量:

当您的内部查询获取数千行,那么EXIST将是更好的选择 当您的内部查询获取少量行时,那么IN将更快

EXIST对true或false进行评估,但在比较多个值。当你不知道记录是否存在时,你应该选择exist

EXISTS的性能比in快。 如果大多数过滤条件在子查询中,那么最好使用in,如果大多数过滤条件在主查询中,那么最好使用EXISTS。

exists关键字可以这样使用,但实际上它是为了避免计数:

--this statement needs to check the entire table
select count(*) from [table] where ...

--this statement is true as soon as one match is found
exists ( select * from [table] where ... )

这是最有用的if条件语句,as exists可以比count快得多。

in最好用在你有一个静态列表要传递的地方:

 select * from [table]
 where [field] in (1, 2, 3)

当在in语句中有一个表时,使用连接更有意义,但大多数情况下这并不重要。无论哪种方式,查询优化器都应该返回相同的计划。在一些实现中(大多数是旧的,如Microsoft SQL Server 2000),查询将总是获得嵌套的连接计划,而连接查询将适当地使用嵌套、合并或散列。更现代的实现更智能,甚至可以在使用in时调整计划。

不同之处在于:

select * 
from abcTable
where exists (select null)

上面的查询将返回所有记录,而下面的查询将返回空。

select *
from abcTable
where abcTable_ID in (select null)

尝试一下并观察输出。