我们在 SQL Server 2008 (SP1) - 10.0.2531.0 (X64) - Win2008 SP2 (X64) 上遇到了一个奇怪的情况。
这是一个繁重的查询:
select t1.id, t2.id
from t1, t2
where
t1.id = t2.ext_id
and isnull(t1.vchCol1, 'Null') = isnull(t2.vchCol1, 'Null')
and isnull(t1.vchCol2, 'Null') = isnull(t2.vchCol2, 'Null')
.... and about 10 more comparisons with Isnull
UPD:比较的所有列(ID 除外)为varchar
(~30...200)
T1 为~1.3 亿行,T2 为~300k 行。
这些查询在相当大的开发服务器上运行约 5 小时- 这很慢,但我们能做什么?
虽然我们研究了可能的优化方式——我们发现,在上面的查询中将“isnull”更改为“coalesce”可以带来双倍的性能提升——并且查询现在运行约 2 小时
UPD:当我们删除所有ISNULL
检查并使用t1.vchCol1 = t2.vchCol1
时,查询会在 40 分钟后完成。
问题是:这是已知的行为,我们应该避免在任何地方使用IsNull吗?