为什么 IsNull 比合并慢两倍(相同查询)?
我们在 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 约为 30 万行。
这些查询在相当大的开发服务器上运行〜5小时 - 这很慢,但我们能做什么?
虽然我们研究了可能的优化方法 - 我们发现,在上面的查询中将 “isnull” 更改为 “coalesce” 可带来双倍的性能增益 - 并且查询现在运行 >~2 小时
UPD:当我们删除所有 ISNULL
检查并仅使用 t1.vchCol1 = t2.vchCol1
时,查询完成40 分钟后。
问题是:这是已知行为吗?我们应该避免在任何地方使用IsNull吗?
We met a strange situation on SQL Server 2008 (SP1) - 10.0.2531.0 (X64) - Win2008 SP2 (X64).
Here is a one heavy query:
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: All columns in comparison (except IDs) are varchar
(~30...200)
T1 is ~130mln rows, T2 is ~300k rows.
These query on rather big Dev server run ~5 hours - this is slow, but what we can do?
And while we investigated possible ways of optimisation - we found, that changing "isnull" to "coalesce" in query above gives double performance gain - and query now run for ~2 hours
UPD: When we remove all ISNULL
checks and use just t1.vchCol1 = t2.vchCol1
the query finishes after 40mins.
Question is: Is this known behavior and we should avoid using IsNull everywhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想知道通过明确地拆分案例您是否会看到改进:
I wonder if you'd see an improvement by splitting the cases out explicitly:
您会发现的有关该主题的大多数文章似乎都与此相矛盾。
ISNULL
比COALESCE
(稍微)快。之间的差异ISNULL
和COALESCE
ISNULL
与.合并
COALESCE
与ISNULL
代码> vs <代码>IS NULL ORMost of the articles you'll find on this subject seem to contradict this.
ISNULL
is (marginally) faster thanCOALESCE
.Differences between
ISNULL
andCOALESCE
ISNULL
vs.COALESCE
COALESCE
vsISNULL
vsIS NULL OR
您可能需要考虑向每个保存校验和值的表添加一个计算列。然后,在ID列和校验和值上创建索引,最后在连接中使用校验和值。像这样的事情:
那么你的查询将变成...
这当然会使用额外的索引空间,但它只是 2 个整数,应该非常有效。每次插入、更新和删除也会有性能损失,因为需要维护另一个索引。但是,我怀疑这会对性能产生很大影响。
You may want to consider adding a computed column to each table that holds a checksum value. Then, create an index on the ID column and the checksum value, and finally use the checksum value in the join. Something like this:
Then your query would become...
This will, of course, use extra index space, but it's simply 2 integers which should be very efficient. There will also be a performance penalty for each insert, update and delete because another index needs to be maintained. However, I suspect this will have a large impact on performance.
我意识到这是一年后的事了,但是......
对于这种逐列比较,您可能会考虑使用 EXCEPT。另外,EXCEPT 将 NULL 视为另一个值,而不是“它可以是任何东西!”,我喜欢这样称呼它。
“当您比较行以确定不同值时,两个 NULL 值被视为相等。”
--来自 http://msdn.microsoft.com/en-us/library/ ms188055.aspx
I realize this is a year later, but...
For this kind of column-by-column comparison, you might consider using EXCEPT. Also, EXCEPT treats NULL like another value instead of "It could be anything!", as I like to call it.
"When you compare rows for determining distinct values, two NULL values are considered equal."
--from http://msdn.microsoft.com/en-us/library/ms188055.aspx