带有Null值的Sql Server In语句

时间:2013-12-09 19:49:20

标签: sql sql-server null

我有以下内容:

select Firstname, LastName, Department
from Employee
where 
     Department is null
  or Department in (
       select Department
       from Employee
       group by Department
       having COUNT(1) < 5)

这需要一个/很长时间(超过10分钟),我假设它最终会超时。

我可以将其更改为:

select Firstname, LastName, Department
from Employee
where 
  coalesce(Department,'') in (
       select coalesce(Department,'')
       from Employee
       group by Department
       having COUNT(1) < 5)

在这种情况下,它给了我所需要的东西。它会在2秒后返回。

如果我只是使用where子句的任一部分进行第一次查询,则返回快速。我也加入了它们,它也很快回归。当我将它们结合起来时,有什么能够解决这个问题的原因吗?

0 个答案:

没有答案