0

最近我们发现了一个长时间运行的过程,我的一位同事指出,由于这篇文章中指出的原因,在 where 子句中删除 ISNULL 会加快速度。在 SQL Server 中使用 ISNULL() 时是否存在任何性能问题?. 我做了一些谷歌搜索,但无法确定丢失数据是否有任何危险。我知道如果你说这样的话

WHERE ID NOT IN (SELECT MemberID FROM Person WHERE MemberId IS NOT Null)

并且 MemberId 是可空的,如果您不包含“IS NOT NULL”,您将不会得到任何匹配,但我认为这是一个不同的问题?也许不吧?

这是我的同事提议的改变。

where ISNULL(ct.DisabilityBenefitsConnectAuthorized,0) = 1

会成为

where ct.DisabilityBenefitsConnectAuthorized = 1

它似乎没有影响结果,但我不能 100% 确定它不会,我们只是不知道。

4

3 回答 3

3

为什么不通过一个简化的示例自己尝试一下,例如:

declare @x bit

--@x is currently null
if isnull(@x,0)=1 begin print 'is one!' end else begin print 'not one!' end
if @x=1 begin print 'is one!' end else begin print 'not one!' end

set @x=0
if isnull(@x,0)=1 begin print 'is one!' end else begin print 'not one!' end
if @x=1 begin print 'is one!' end else begin print 'not one!' end

set @x=1
if isnull(@x,0)=1 begin print 'is one!' end else begin print 'not one!' end
if @x=1 begin print 'is one!' end else begin print 'not one!' end

输出:

not one!
not one!
not one!
not one!
is one!
is one!

基于以上,看起来你可以删除 ISNULL()

于 2012-11-08T14:37:32.387 回答
1

这种改变会很好。

既不也不NULL等于01因此结果将是相同的。

于 2012-11-08T14:22:21.940 回答
0

MemberId 是可空的,如果您不包含“IS NOT NULL”,您将不会得到任何匹配,但我认为这是一个不同的问题?也许不吧?

不它不是。您必须在内部查询的子句中测试可空MemberID值。WHERE如果表中有任何NULLMemberID,您将不会获得任何值PersonLEFT JOIN如果您使用withWHERE leftids IS NULL而不是谓词可能会更好IN,它更安全。

于 2012-11-08T14:22:35.623 回答