SQL OR 和 AND 运算符的行为

发布于 2024-10-18 03:35:39 字数 196 浏览 6 评论 0原文

我们有以下表达式作为 T-Sql 查询:

Exp1 OR Exp2

当 Exp1 为 True 时,是否计算 Exp2?我认为没有必要去评价。

相似地;例如,

当 Exp1 为 false 时, Exp1 AND Exp2

是否计算 Exp2?

We have following expression as T-Sql Query:

Exp1 OR Exp2

Is Exp2 evaluated when Exp1 is True? I think there is no need to evaluate it.

Similarly; for,

Exp1 AND Exp2

is Exp2 evaluated when Exp1 is false?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2024-10-25 03:35:39

与某些编程语言不同,您无法计数 关于 T-SQL WHERE短路 > 条款。 这可能会发生,也可能会发生不是。

Unlike in some programming languages, you cannot count on short-circuiting in T-SQL WHERE clauses. It might happen, or it might not.

唯憾梦倾城 2024-10-25 03:35:39

SQL Server 不一定按从左到右的顺序计算表达式。评估顺序由执行计划控制,并且根据整个查询的总体估计成本来选择计划。因此,不能确定 SQL 是否会执行您所描述的那种短路优化。这种灵活性使得优化器变得有用。例如,每种情况下的第二个表达式可能比第一个表达式更有效地计算(例如,如果它被索引或受到某些约束)。

SQL 还使用三值逻辑,这意味着二值逻辑中使用的一些等价规则不适用(尽管这不会改变您描述的具体示例)。

SQL Server doesn't necessarily evaluate expressions in left to right order. Evaluation order is controlled by the execution plan and the plan is chosen based on the overall estimated cost for the whole of a query. So there is no certainty that SQL will perform the kind of short circuit optimisation you are describing. That flexibility is what makes the opimiser useful. For example it could be that the second expression in each case can be evaluated more efficiently than the first (if it is indexed or subject to some constraint for example).

SQL also uses three-value logic, which means that some of the equivalence rules used in two-value logic don't apply (although that doesn't alter the specific example you describe).

黄昏下泛黄的笔记 2024-10-25 03:35:39

SQL Server 查询运算符 OR 和 AND 是可交换的。没有固有的顺序,查询优化器可以自由选择成本最低的路径来开始评估。一旦计划确定,如果结果是预先确定的,则不评估其他部分。

查询。

select * from master..spt_values
where (type = 'P' or 1=@param1)
  and (1=@param2 or number < 1000)
option (recompile)

这些知识允许像当 @param 设置为 1 时评估模式保证短路这样的 这种模式是可选过滤器的典型模式。请注意,@params 是在其他部分之前还是之后测试并不重要。

如果您非常擅长 SQL,并且知道查询最好强制执行某个计划,那么您可以使用 CASE 语句来玩弄 SQL Server,这些语句始终按嵌套顺序进行计算。下面的示例将强制首先评估 type='P' 始终

select *
from master..spt_values
where
    case when type='P' then
        case when number < 100 then 1
    end end = 1

如果您不相信最后一个查询的求值顺序,请尝试这个

select *
from master..spt_values
where
    case when type='P' then
        case when number < 0 then
            case when 1/0=1 then 1
    end end end = 1

即使表达式 1/0=1 中的常量是求值成本最低的,但它永远不会求值 - 否则查询会导致被零除而不是返回任何行(master..spt_values 中没有行匹配这两个条件)。

SQL Server query operators OR and AND are commutative. There is no inherent order and the query optimizer is free to choose the path of least cost to begin evaluation. Once the plan is set, the other part is not evaluated if a result is pre-determined.

This knowledge allows queries like

select * from master..spt_values
where (type = 'P' or 1=@param1)
  and (1=@param2 or number < 1000)
option (recompile)

Where the pattern of evaluation is guaranteed to short circuit when @param is set to 1. This pattern is typical of optional filters. Notice that it does not matter whether the @params are tested before or after the other part.

If you are very good with SQL and know for a fact that the query is best forced down a certain plan, you can game SQL Server using CASE statements, which are always evaluated in nested order. Example below will force type='P' to always be evaluated first.

select *
from master..spt_values
where
    case when type='P' then
        case when number < 100 then 1
    end end = 1

If you don't believe order of evaluation of the last query, try this

select *
from master..spt_values
where
    case when type='P' then
        case when number < 0 then
            case when 1/0=1 then 1
    end end end = 1

Even though the constants in the expression 1/0=1 is the least cost to evaluate, it is NEVER evaluated - otherwise the query would have resulted in divide-by-zero instead of returning no rows (there are no rows in master..spt_values matching both conditions).

心头的小情儿 2024-10-25 03:35:39

SQL Server 有时会执行布尔短路,有时则不会。

这取决于生成的查询执行计划。选择的执行计划取决于几个因素,包括 WHERE 子句中列的选择性、表大小、可用索引等。

SQL Server sometimes performs boolean short circuiting, and sometimes does not.

It depends upon the query execution plan that is generated. The execution plan chosen depends on several factors, including the selectivity of the columns in the WHERE clause, table size, available indexes etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文