0

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

4

2 回答 2

3

您绝对希望避免使用将列包装在过滤器中的任何自定义或内置函数- 它严重限制了优化器在索引使用和可搜索性方面可以为您做的事情。您应该养成尽可能使用相等运算符和或/联合方法的习惯,就像这里的情况一样。与 isnull() 或 coalesce() 方法相比,以下方法更受欢迎:

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
于 2009-11-16T22:22:29.860 回答
0

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

我相信首选的风格是

ISNULL(email, '') = ''
于 2009-11-16T22:00:53.103 回答