Is Sql Server's ISNULL() function lazy/short-circuited?

独自空忆成欢 提交于 2019-12-08 17:03:54

问题


TIs ISNULL() a lazy function?

That is, if i code something like the following:

SELECT ISNULL(MYFIELD, getMyFunction()) FROM MYTABLE

will it always evaluate getMyFunction() or will it only evaluate it in the case where MYFIELD is actually null?


回答1:


It's whichever it thinks will work best.

Now it's functionally lazy, which is the important thing. E.g. if col1 is a varchar which will always contain a number when col2 is null, then

isnull(col2, cast(col1 as int))

Will work.

However, it's not specified whether it will try the cast before or simultaneously with the null-check and eat the error if col2 isn't null, or if it will only try the cast at all if col2 is null.

At the very least, we would expect it to obtain col1 in any case because a single scan of a table obtaining 2 values is going to be faster than two scans obtaining one each.

The same SQL commands can be executed in very different ways, because the instructions we give are turned into lower-level operations based on knowledge of the indices and statistics about the tables.

For that reason, in terms of performance, the answer is "when it seems like it would be a good idea it is, otherwise it isn't".

In terms of observed behaviour, it is lazy.

Edit: Mikael Eriksson's answer shows that there are cases that may indeed error due to not being lazy. I'll stick by my answer here in terms of the performance impact, but his is vital in terms of correctness impact across at least some cases.




回答2:


This works fine

declare @X int
set @X = 1
select isnull(@X, 1/0)

But introducing an aggregate will make it fail and proving that the second argument could be evaluated before the first, sometimes.

declare @X int
set @X = 1
select isnull(@X, min(1/0))



回答3:


Judging from the different behavior of

SELECT ISNULL(1, 1/0)

SELECT ISNULL(NULL, 1/0)

the first SELECT returns 1, the second raises a Msg 8134, Level 16, State 1, Line 4 Divide by zero error encountered. error.




回答4:


This "lazy" feature you are referring to is in fact called "short-circuiting"
And it does NOT always work especially if you have a udf in the ISNULL expression.
Check this article where tests were run to prove it:
Short-circuiting (mainly in VB.Net and SQL Server)

T-SQL is a declarative language hence it cannot control the algorithm used to get the results.. it just declares what results it needs. It is upto the query engine/optimizer to figure out the cost-effective plan. And in SQL Server, the optimizer uses "contradiction detection" which will never guarantee a left-to-right evaluation as you would assume in procedural languages.


For your example, did a quick test:
Created the scalar-valued UDF to invoke the Divide by zero error:

CREATE FUNCTION getMyFunction
( @MyValue INT )
RETURNS INT
AS
BEGIN
    RETURN (1/0)
END
GO

Running the below query did not give me a Divide by zero error encountered error.

DECLARE @test INT
SET @test = 1
SET @test = ISNULL(@test, (dbo.getMyFunction(1)))
SELECT @test

Changing the SET to the below statement did give me the Divide by zero error encountered. error. (introduced a SELECT in ISNULL)

SET @test = ISNULL(@test, (SELECT dbo.getMyFunction(1)))

But with values instead of variables, it never gave me the error.

SELECT ISNULL(1, (dbo.getMyFunction(1)))
SELECT ISNULL(1, (SELECT dbo.getMyFunction(1)))

So unless you really figure out how the optimizer is evaluating these expressions for all permutations, it would be safe to not rely on the short-circuit capabilities of T-SQL.



来源:https://stackoverflow.com/questions/12219397/is-sql-servers-isnull-function-lazy-short-circuited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!