Does Oracle use short-circuit evaluation?

前端 未结 4 465
庸人自扰
庸人自扰 2020-11-28 13:00

I have an Oracle query that is structured as followed:

SELECT   *
FROM     table
WHERE    X=\'true\' OR
         Y IN (complicated subquery)
<
4条回答
  •  萌比男神i
    2020-11-28 13:14

    Caveat: Oracle is not my primary area of expertise.

    The cost-based optimizer should know that the cost of X = 'true' is less than the sub-query, so it will likely evaluate the simpler alternative first. But the AND and OR conditions in SQL are not short-circuited like && and || are in C and its derivatives.

    The sub-query can be one of two forms: correlated and non-correlated.

    • A correlated sub-query must be executed many times (which is why they are dangerous to performance) because the correlation means that the sub-query result depends in some way on the row 'currently being evaluated').
    • A non-correlated sub-query will only be executed once.

    Example correlated sub-query:

    SELECT *
      FROM Table1
     WHERE X = 'true'
        OR Y IN (SELECT Z FROM Table2 WHERE Table2.A = Table1.B)
    

    Example non-correlated sub-query:

    SELECT *
      FROM Table1
     WHERE X = 'true'
        OR Y IN (SELECT Z FROM Table2 WHERE Table2.A > 13)
    

提交回复
热议问题