問題描述
來自這個問題, 關于使用的簡潔答案COALESCE 來簡化復雜的邏輯樹.我考慮過短路的問題.
From this question, a neat answer about using COALESCE to simplify complex logic trees. I considered the problem of short circuiting.
例如,在大多數語言的函數中,參數會被完全評估,然后被傳遞到函數中.在 C:
For instance, in functions in most languages, arguments are fully evaluated and are then passed into the function. In C:
int f(float x, float y) {
return x;
}
f(a, a / b) ; // This will result in an error if b == 0
這似乎不是 SQL Server 中 COALESCE
函數"的限制:
That does not appear to be a limitation of the COALESCE
"function" in SQL Server:
CREATE TABLE Fractions (
Numerator float
,Denominator float
)
INSERT INTO Fractions VALUES (1, 1)
INSERT INTO Fractions VALUES (1, 2)
INSERT INTO Fractions VALUES (1, 3)
INSERT INTO Fractions VALUES (1, 0)
INSERT INTO Fractions VALUES (2, 0)
INSERT INTO Fractions VALUES (3, 0)
SELECT Numerator
,Denominator
,COALESCE(
CASE WHEN Denominator = 0 THEN 0 ELSE NULL END,
CASE WHEN Numerator <> 0 THEN Numerator / Denominator ELSE NULL END,
0
) AS TestCalc
FROM Fractions
DROP TABLE Fractions
如果它在 Denominator = 0 時評估第二種情況,我希望看到如下錯誤:
If it were evaluating the second case when Denominator = 0, I would expect to see an error like:
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
我發現了一些提及 與 Oracle 相關.還有一些使用 SQL 服務器.當您包含用戶定義的函數時,看起來短路可能會中斷.
I found some mentions related to Oracle. And some tests with SQL Server. Looks like the short-circuiting might break down when you include user-defined functions.
那么,ANSI 標準是否應該保證這種行為?
So, is this behavior supposed to be guaranteed by the ANSI standard?
推薦答案
我剛剛看了鏈接的文章,可以確認 COALESCE 和 ISNULL 的短路都可能失敗.
I just had a look at the linked article and can confirm short circuiting can fail for both COALESCE and ISNULL.
如果涉及任何子查詢,它似乎會失敗,但它適用于標量函數和硬編碼值.
It seems to fail if you have any sub-query involved, but it works fine for scalar functions and hard coded values.
例如
DECLARE @test INT
SET @test = 1
PRINT 'test2'
SET @test = COALESCE(@test, (SELECT COUNT(*) FROM sysobjects))
SELECT 'test2', @test
-- OUCH, a scan through sysobjects
COALESCE 根據 ANSI 標準實現.它只是 CASE 語句的簡寫.ISNULL 不是 ANSI 標準的一部分.第 6.9 節似乎沒有明確要求短路,但它確實暗示應該返回 when
語句中的第一個 true 子句.
COALESCE is implemented according to the ANSI standard. It is simply a shorthand for a CASE statement. ISNULL is not part of the ANSI standard. Section 6.9 does not seem to require short circuiting explicitly, but it does imply that the first true clause in the when
statement should be returned.
這是一些適用于基于標量的函數的證明(我在 SQL Server 2005 上運行它):
Here is some proof that is works for scalar based functions (I ran it on SQL Server 2005):
CREATE FUNCTION dbo.evil
(
)
RETURNS int
AS
BEGIN
-- Create an huge delay
declare @c int
select @c = count(*) from sysobjects a
join sysobjects b on 1=1
join sysobjects c on 1=1
join sysobjects d on 1=1
join sysobjects e on 1=1
join sysobjects f on 1=1
return @c / 0
END
go
select dbo.evil()
-- takes forever
select ISNULL(1, dbo.evil())
-- very fast
select COALESCE(1, dbo.evil())
-- very fast
這里有一些證據表明 CASE 的底層實現將執行子查詢.
Here is some proof that the underlying implementation with CASE will execute sub queries.
DECLARE @test INT
SET @test = 1
select
case
when @test is not null then @test
when @test = 2 then (SELECT COUNT(*) FROM sysobjects)
when 1=0 then (SELECT COUNT(*) FROM sysobjects)
else (SELECT COUNT(*) FROM sysobjects)
end
-- OUCH, two table scans. If 1=0, it does not result in a table scan.
這篇關于COALESCE - 保證短路?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!