gpt4 book ai didi

sql - 使用 ISNULL 与使用 COALESCE 来检查特定条件?

转载 作者:行者123 更新时间:2023-12-03 05:24:09 24 4
gpt4 key购买 nike

我知道可以将多个参数传递给COALESCE,但是当您想要时仅检查一个表达式以查看它是否不存在,您是否使用默认值,或者使用 ISNULL 代替是更好的做法?

两者之间有性能提升吗?

最佳答案

This problem reported on Microsoft Connect揭示了 COALESCEISNULL 之间的一些差异:

an early part of our processing rewrites COALESCE( expression1, expression2 ) as CASE WHEN expression1 IS NOT NULL THEN expression1 ELSE expression2 END. In [this example]:

COALESCE ( ( SELECT Nullable
FROM Demo
WHERE SomeCol = 1 ), 1 )

we generate:

SELECT CASE
WHEN (SELECT Nullable FROM Demo WHERE SomeCol = 1) IS NOT NULL
THEN (SELECT Nullable FROM Demo WHERE SomeCol = 1)
ELSE 1
END

Later stages of query processing don't understand that the two subqueries were originally the same expression, so they execute the subquery twice...

One workaround, though I hate to suggest it, is to change COALESCE to ISNULL, since the latter doesn't duplicate the subquery.

关于sql - 使用 ISNULL 与使用 COALESCE 来检查特定条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7408893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com