久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

COALESCE - 保證短路?

COALESCE - guaranteed to short-circuit?(COALESCE - 保證短路?)
本文介紹了COALESCE - 保證短路?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

來自這個問題, 關于使用的簡潔答案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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Sql server table usage statistics(Sql server 表使用情況統計)
Relative path in t sql?(t sql中的相對路徑?)
Getting the last record in SQL in WHERE condition(在 WHERE 條件下獲取 SQL 中的最后一條記錄)
Query to get XML output for hierarchical data using FOR XML PATH in SQL Server(在 SQL Server 中使用 FOR XML PATH 查詢以獲取分層數據的 XML 輸出)
T-SQL IF statement embedded in a sum() function(嵌入在 sum() 函數中的 T-SQL IF 語句)
Table vs Temp Table Performance(表與臨時表性能)
主站蜘蛛池模板: 免费啪啪 | 日本三级网站在线观看 | 黑人中文字幕一区二区三区 | 一区二区三区视频在线观看 | 成人一区二区在线 | 亚洲欧美中文日韩在线v日本 | a级性视频 | 在线观看av网站 | 亚洲美女一区 | 亚洲国产精品视频一区 | 国产亚洲一区二区三区 | 欧美福利| 韩日一区二区 | 亚洲午夜av久久乱码 | 99精品欧美一区二区三区综合在线 | 国产福利在线小视频 | 日韩av免费在线观看 | 伊人久久综合 | 国产传媒在线播放 | 久久久久一区 | 久久高清 | 欧美电影一区 | 亚洲精品久久久久国产 | 午夜婷婷激情 | 午夜欧美 | 卡通动漫第一页 | www.国产| 欧美日韩综合精品 | www.黄色网| 日本手机看片 | 中国av在线免费观看 | 日本三级在线网站 | 夜夜爽99久久国产综合精品女不卡 | 在线免费av电影 | 日韩成人在线免费观看 | 成人精品视频免费 | 国产精品日韩一区二区 | 一区二区国产在线 | 日韩一区二区三区在线观看 | 欧日韩在线观看 | 欧美欧美欧美 |