ISNULL(电子邮件,“”)=“”为(电子邮件为空或电子邮件=“”)不是由它的自我解释?

StackOverflow https://stackoverflow.com/questions/1745113

为执行一个事实哪一个更好?是否有实际的3个三个版本的SQL服务器(2000/2005/2008)?

之间的差异
有帮助吗?

解决方案

您一定要避免使用任何定制或内置功能包的一个在过滤列 - 这严重限制了什么优化器可以在索引使用和seekability方面为你做。你应该使用平等运营商和/或联合方式时可能的习惯,比如这里的情况。将在ISNULL(被非常优选)或聚结()以下的方法:

where   (
            (t.email is null)
            or
            (t.email = '')
        )

或概述如下能够更好地工作,以及,尝试一下在您的环境,以确定哪个选项最工会的做法。

一个简单的例子将演示的急剧差异可以在性能见:

use tempdb;
go
if object_id('tempdb..#testTable') > 0
    drop table #testTable;
go
-- Build the dataset
select  top 10000000
        cast(cast(a.name as varchar(100)) + '@' + cast(row_number() over (order by a.object_id) as varchar(15)) + '.com' as varchar(150)) as email, 
        row_number() over (order by a.object_id) as id
into    #testTable
from    sys.columns a
cross join sys.columns b
cross join sys.columns c
go
-- Create some nulls
update  #testTable
set     email = null
where   id % 1000 = 0
go
-- Index
create unique clustered index ixc__dbo_testTable__temp__nc1 on #testTable (email,id) on [default];
go
set statistics io on;
set statistics time on;
go
-- Try with isnull - ~cost of about 44.7 on my machine, ~2900ms to execute, and about 49,200 logical reads
select  *
from    #testTable t
where   isnull(t.email,'') = '';
go
-- Try with 'or' - ~cost of about .049 on my machine, ~643ms to execute, about 31 logical reads
select  *
from    #testTable t
where   (
            (t.email is null)
            or
            (t.email = '')
        );
go
-- Try with union approach - ~cost of about .054 on my machine, ~751ms to execute, ~30 logical reads
select  *
from    #testTable t
where   t.email is null
union all
select  *
from    #testTable t
where   t.email = '';
go
if object_id('tempdb..#testTable') > 0
    drop table #testTable;
go

其他提示

如果你要看到性能上的差异,他们将分。

相信优选的风格是

ISNULL(email, '') = ''
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top