Does Oracle use short-circuit evaluation?

前端 未结 4 452
庸人自扰
庸人自扰 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条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 13:30

    Regardless of what the optimizer may or may not do with AND and OR, if for any reason you must enforce a specific order of evaluation, you can rewrite the query, using other tools where short-circuit evaluation is guaranteed.

    For example:

    select * from table 1
    where case when X = 'true' then 1
               when Y in (select ....)   then 1
          end  = 1
    

    If X is 'true' then the case expression evaluates to 1, the second "when" is skipped and the condition evaluates to TRUE. If X is not 'true' then the IN condition is evaluated.

提交回复
热议问题