作为性能的事实,哪一个更好?sql-server的实际3个版本(2000 / 2005 / 2008)之间有区别吗?
发布于 2009-11-17 06:22:30
您肯定希望避免使用任何custom or built-in functions wrapping a column in a filter -它严重限制了优化器在索引使用和可查找性方面为您所做的事情。您应该养成在可能的情况下使用相等运算符和/或联合方法的习惯,就像这里的情况一样。下面的方法比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-17 06:00:53
如果您将看到性能差异,它们将是微小的。
我认为首选的风格是
ISNULL(email, '') = ''https://stackoverflow.com/questions/1745113
复制相似问题