Is the SQL WHERE clause short-circuit evaluated?

前端 未结 14 2463
时光取名叫无心
时光取名叫无心 2020-11-22 04:31

Are boolean expressions in SQL WHERE clauses short-circuit evaluated ?

For example:

SELECT * 
FROM Table t 
WHERE @key IS NULL OR (@key IS NOT NULL          


        
相关标签:
14条回答
  • 2020-11-22 04:47

    It is but obvious that MS Sql server supports Short circuit theory, to improve the performance by avoiding unnecessary checking,

    Supporting Example:

    SELECT 'TEST'
    WHERE 1 = 'A'
    
    SELECT 'TEST'
    WHERE 1 = 1 OR 1 = 'A'
    

    Here, the first example would result into error 'Conversion failed when converting the varchar value 'A' to data type int.'

    While the second runs easily as the condition 1 = 1 evaluated to TRUE and thus the second condition doesn't ran at all.

    Further more

    SELECT 'TEST'
    WHERE 1 = 0 OR 1 = 'A'
    

    here the first condition would evaluate to false and hence the DBMS would go for the second condition and again you will get the error of conversion as in above example.

    NOTE: I WROTE THE ERRONEOUS CONDITION JUST TO REALIZE WEATHER THE CONDITION IS EXECUTED OR SHORT-CIRCUITED IF QUERY RESULTS IN ERROR MEANS THE CONDITION EXECUTED, SHORT-CIRCUITED OTHERWISE.

    SIMPLE EXPLANATION

    Consider,

    WHERE 1 = 1 OR 2 = 2
    

    as the first condition is getting evaluated to TRUE, its meaningless to evaluate the second condition because its evaluation in whatever value would not affect the result at all, so its good opportunity for Sql Server to save Query Execution time by skipping unnecessary condition checking or evaluation.

    in case of "OR" if first condition is evaluated to TRUE the entire chain connected by "OR" would considered as evaluated to true without evaluating others.

    condition1 OR condition2 OR ..... OR conditionN
    

    if the condition1 is evaluated to true, rest all of the conditions till conditionN would be skipped. In generalized words at determination of first TRUE, all other conditions linked by OR would be skipped.

    Consider the second condition

    WHERE 1 = 0 AND 1 = 1
    

    as the first condition is getting evalutated to FALSE its meaningless to evaluate the second condition because its evaluation in whatever value would not affect the result at all, so again its good opportunity for Sql Server to save Query Execution time by skipping unnecessary condition checking or evaluation.

    in case of "AND" if first condition is evaluated to FALSE the entire chain connected with the "AND" would considered as evaluated to FALSE without evaluating others.

    condition1 AND condition2 AND ..... conditionN
    

    if the condition1 is evaluated to FALSE, rest all of the conditions till conditionN would be skipped. In generalized words at determination of first FALSE, all other conditions linked by AND would be skipped.

    THEREFOR, A WISE PROGRAMMER SHOULD ALWAYS PROGRAM THE CHAIN OF CONDITIONS IN SUCH A WAY THAT, LESS EXPENSIVE OR MOST ELIMINATING CONDITION GETS EVALUATED FIRST, OR ARRANGE THE CONDITION IN SUCH A WAY THAT CAN TAKE MAXIMUM BENEFIT OF SHORT CIRCUIT

    0 讨论(0)
  • 2020-11-22 04:48

    This takes an extra 4 seconds in query analyzer, so from what I can see IF is not even shorted...

    SET @ADate = NULL
    
    IF (@ADate IS NOT NULL)
    BEGIN
        INSERT INTO #ABla VALUES (1)
            (SELECT bla from a huge view)
    END
    

    It would be nice to have a guaranteed way!

    0 讨论(0)
  • 2020-11-22 04:50

    i don't know about short circuting, but i'd write it as an if-else statement

    if (@key is null)
    begin
    
         SELECT * 
         FROM Table t 
    
    end
    else
    begin
    
         SELECT * 
         FROM Table t 
         WHERE t.Key=@key
    
    end
    

    also, variables should always be on the right side of the equation. this makes it sargable.

    http://en.wikipedia.org/wiki/Sargable

    0 讨论(0)
  • 2020-11-22 04:54

    Main characteristic of short circuit evaluation is that it stops evaluating the expression as soon as the result can be determined. That means that rest of expression can be ignored because result will be same regardless it is evaluated or not.

    Binary boolean operators are comutative, meaning:

    a AND b == b AND a
    a OR  b == b OR  a
    a XOR b == b XOR a
    

    so there is no guarantee on order of evaluation. Order of evaluation will be determined by query optimizer.

    In languages with objects there can be situations where you can write boolean expressions that can be evaluated only with short circuit evaluation. Your sample code construction is often used in such languages (C#, Delphi, VB). For example:

    if(someString == null | someString.Length == 0 )
      printf("no text in someString");
    

    This C# example will cause exception if someString == null because it will be fully evaluated. In short circuit evaluation, it will work every time.

    SQL operates only on scalar variables (no objects) that cannot be uninitialized, so there is no way to write boolean expression that cannot be evaluated. If you have some NULL value, any comparison will return false.

    That means that in SQL you cannot write expression that is differently evaluated depending on using short circuit or full evaluation.

    If SQL implementation uses short circuit evaluation, it can only hopefully speed up query execution.

    0 讨论(0)
  • 2020-11-22 04:58

    I think this is one of the cases where I'd write it as if it didn't short-circuit, for three reasons.

    1. Because for MSSQL, it's not resolved by looking at BOL in the obvious place, so for me, that makes it canonically ambiguous.

    2. because at least then I know my code will work. And more importantly, so will those who come after me, so I'm not setting them up to worry through the same question over and over again.

    3. I write often enough for several DBMS products, and I don't want to have to remember the differences if I can work around them easily.

    0 讨论(0)
  • 2020-11-22 05:02

    From the above, short circuiting is not really available.

    If you need it, I suggest a Case statement:

    Where Case when Expr1 then Expr2 else Expr3 end = desiredResult
    

    Expr1is always evaluated, but only one of Expr2 and Expr3 will be evaluated per row.

    0 讨论(0)
提交回复
热议问题