首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >isnull (email,'') = '‘本身不会被解释为( email is null or email= '')?

isnull (email,'') = '‘本身不会被解释为( email is null or email= '')?
EN

Stack Overflow用户
提问于 2009-11-17 05:56:43
回答 2查看 303关注 0票数 0

作为性能的事实,哪一个更好?sql-server的实际3个版本(2000 / 2005 / 2008)之间有区别吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-11-17 06:22:30

您肯定希望避免使用任何custom or built-in functions wrapping a column in a filter -它严重限制了优化器在索引使用和可查找性方面为您所做的事情。您应该养成在可能的情况下使用相等运算符和/或联合方法的习惯,就像这里的情况一样。下面的方法比isnull()或coalesce()方法更可取:

代码语言:javascript
复制
where   (
            (t.email is null)
            or
            (t.email = '')
        )

或者,下面列出的联合方法也可以更好地工作,请在您的环境中尝试它,以确定哪个选项是最好的。

一个简单的示例将演示您在性能方面可以看到的巨大差异:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2009-11-17 06:00:53

如果您将看到性能差异,它们将是微小的。

我认为首选的风格是

代码语言:javascript
复制
ISNULL(email, '') = ''
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1745113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档