SQLServer CASE表达式-短路评估? [英] SQLServer CASE expressions - short circuit evaluation?

查看:76
本文介绍了SQLServer CASE表达式-短路评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试优化执行以下操作的查询:

I am trying to optimize a query that does something like this:

SELECT
   ...
   CASE WHEN (condition) THEN (expensive function call #1)
   ELSE (expensive function call #2) 
   END
...

查询计划表明,即使在100%的行满足WHEN子句的情况下,ELSE分支中也有相当一部分时间用于结果调用.

The query plan shows that for even in cases where 100% of rows satisfy the WHEN clause, a substantial portion of the time is spent in calls to the result in the ELSE branch.

我能理解的唯一方法是假定SQLServer正在评估两个结果,然后仅基于对WHEN条件的评估来选择一个,但是我找不到关于结果是否是否有任何明确的参考在条件语句之前先评估CASE语句的.任何人都可以澄清或指向我参考吗?

The only way I can make sense of this is to presume that SQLServer is evaluating both results, then only selecting one based on evaluation of the WHEN condition, but I can't find any definitive reference as to whether or not the results of a CASE statement are evaluated before the conditionals. Can anyone please clarify or point me to a reference?

推荐答案

这是实际计划还是估计计划? Sql Server根据收集到的统计数据期望执行的计划来构建计划,而这并不总是与您为查询运行的一个实例发送给它的特定条件相对应.

Is that an actual or estimated plan? Sql Server builds plans based on what it expects to do based on collected statistics, and that doesn't always jibe with what specific conditions you send it for one instance of a query run.

这篇关于SQLServer CASE表达式-短路评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆