Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Understanding T-SQL Expression Short-Circuiting Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, December 30, 2010 10:26 AM
 SSCommitted Group: General Forum Members Last Login: Wednesday, November 30, 2016 6:02 AM Points: 1,858, Visits: 2,652
 Gianluca Sartori (12/30/2010)sknox (12/30/2010)Any boolean expression is capable of being short-circuited, in the right circumstances.So under what circumstances can you short-circuit an XOR? (i.e, if either A or B but not both then C)?T-SQL lacks a XOR logical operator, but it can be implemented from its definition:`A XOR B = (A AND NOT B) OR (NOT A AND B)`Sorry for the stupid example, I can't think of a better one right now: to find all users with NULL first_name (expression A) or NULL middle_name (expression B) but not both you could write:`-- This is how you would do it if T-SQL had a XOR operator.SELECT *FROM userWHERE (first_name IS NULL) XOR (middle_name IS NULL)-- This is how you have to code it with AND, OR and NOT operatorsSELECT *FROM userWHERE (first_name IS NULL AND middle_name IS NOT NULL) OR (first_name IS NOT NULL AND middle_name IS NULL)`Any boolean operator can be rewritten using AND, OR and NOT.I know how to write an XOR using AND/OR/NOT. But while you can write it, you can't short-circuit it:`(first_name IS NULL AND middle_name IS NOT NULL) OR (first_name IS NOT NULL AND middle_name IS NULL)`In that code, both first_name and middle_name have to be evaluated. First we must evaluate first_name. If it's not NULL, then we can, yes, ignore middle_name here and short-circuit the first AND. But then we return false to the first part of the OR so we must evaluate the second part. Since first_name is not NULL, we know we must evaluate the second part of second AND, which evaluates middle_name. So you have to evaluate both sides of the XOR.You can reorder the AND and OR operators, but since the two sides are mutually exclusive, you will always have to evaluate both of the original expressions. So not all boolean expressions can be short-circuited.
Post #1041020
 Posted Thursday, December 30, 2010 10:31 AM
 SSCertifiable Group: General Forum Members Last Login: Wednesday, February 10, 2016 11:50 AM Points: 6,897, Visits: 13,559
 Gianluca, thanx for sharing an EXCELLENT article!!! LutzA pessimist is an optimist with experience. How to get fast answers to your question How to post performance related questionsLinks for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1041024
 Posted Thursday, December 30, 2010 11:04 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Monday, May 12, 2014 11:48 AM Points: 175, Visits: 86
 Gianluca Sartori (12/30/2010)Daniel Ruehle (12/30/2010)One easy way to make sure short circuiting works the way you want it is using case statements:`select *from Personwhere 1 = 1 and CreateDateTime > getdate() - 30 and case when Age > 90 then 1 when Age < 5 then 0 when Gender = 'Male' then 1 when LastName like 'SAM%' then 1 else 0 end = 1`This gets records for all people over the age of 90, males of age 5 or more and anyone with a last name that starts with the letters SAM. Notice that the integer checks are done first as they are the easiest to evaluate and the expensive like expression is last. The documentation for the case statement explicity says:Evaluates, in the order specified, Boolean_expression for each WHEN clause.so this is like an explicit short circuit if you would like.You're right, Daniel. CASE is guranteed to evaluate expressions in the exact order they appear.What is questionable is the time you save by pushing "expensive tests" down. Unless you're working with billion row tables, you wouldn't even notice the difference. It's the query plan that decides how fast the query will run, not the number of expressions to evaluate.Agree, but if the query plan says its going to scan the table, then it does come down to how long does it take to process each row. Since you aren't guaranteed the order that SQL Server will evaluate the conditions when just using boolean logic, it can choose to do then in an inefficient manner, which I believe was the jist of the article. In scenarios where it might matter, this gives you absolute control the order.
Post #1041059
 Posted Thursday, December 30, 2010 12:07 PM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Tuesday, November 29, 2016 11:37 AM Points: 526, Visits: 2,894
 Gianluca Sartori (12/30/2010)As a side note, a CLR function can update data. Hello Gianluca. Thanks for a great and thorough article :).This is slightly off topic, but how do you get a CLR Function to be able to alter the state of the DB? I have always seen this error:System.Data.SqlClient.SqlException: Invalid use of a side-effecting operator 'INSERT' within a function.I certainly don't think this is a good idea (to alter the state of the DB in a function), but you mention it can be done so I was curious.Take care,Solomon... SQL# - http://www.SQLsharp.com/
Post #1041114
 Posted Thursday, December 30, 2010 12:39 PM
 SSCommitted Group: General Forum Members Last Login: Wednesday, November 30, 2016 6:02 AM Points: 1,858, Visits: 2,652
 Daniel Ruehle (12/30/2010)` ... case when Age > 90 then 1 when Age < 5 then 0 when Gender = 'Male' then 1 when LastName like 'SAM%' then 1 else 0 end = 1`This gets records for all people over the age of 90, males of age 5 or more and anyone with a last name that starts with the letters SAM.Not quite. This gets records for all people over the age of 90, males of age 5 or more, and anyone age 5 or more with a last name that starts with the letters SAM. This will not retrieve a record for someone under age 5 with a last name starting with SAM.Your point about using CASE for explicit short-circuiting is good, but your explanation is a perfect example of how careful you have to be when using CASE, for the same reason.
Post #1041142
 Posted Thursday, December 30, 2010 12:48 PM
 SSCommitted Group: General Forum Members Last Login: Wednesday, November 30, 2016 6:02 AM Points: 1,858, Visits: 2,652
 magarity kerns (12/30/2010)Excellent article - For more fun, check other DBMSes. I checked on Oracle and "select 'A' from dual where 1=0 or 1/0 = 1;" gives a division by zero error. (although it may need to be in a procedure on Oracle to do it with the IF statement). Anyone have DB/2 or Teradata handy?You did the wrong test. You should have done "where 1=0 AND 1/0 = 1" or "where 1=1 or 1/0 = 1". Remember: AND short-circuits on false, OR short-circuits on true. I am, however, interested in what you find out -- and we shouldn't necessarily limit the testing to DBMSes -- any system or language that evaluates boolean expressions can be tested for this behavior.
Post #1041150
 Posted Thursday, December 30, 2010 1:16 PM
 SSCrazy Eights Group: General Forum Members Last Login: Friday, November 11, 2016 6:39 AM Points: 9,932, Visits: 11,346
 sknox (12/30/2010)I know how to write an XOR using AND/OR/NOT.Yes, but Gianluca wasn't to know that. You might have been asking a sensible question, rather than just making the point that in the sentence "Any boolean expression is capable of being short-circuited, in the right circumstances." it would have been more accurate to say 'many' rather than 'any'...But while you can write it, you can't short-circuit it:`(first_name IS NULL AND middle_name IS NOT NULL) OR (first_name IS NOT NULL AND middle_name IS NULL)`In that code, both first_name and middle_name have to be evaluated. First we must evaluate first_name. If it's not NULL, then we can, yes, ignore middle_name here and short-circuit the first AND. But then we return false to the first part of the OR so we must evaluate the second part. Since first_name is not NULL, we know we must evaluate the second part of second AND, which evaluates middle_name. So you have to evaluate both sides of the XOR.You can reorder the AND and OR operators, but since the two sides are mutually exclusive, you will always have to evaluate both of the original expressions. So not all boolean expressions can be short-circuited.So, you're saying that an XOR written in T-SQL can be short-circuited? Or just a bit? Paul WhiteSQLPerformance.comSQLblog.com@SQL_Kiwi
Post #1041170
 Posted Thursday, December 30, 2010 2:30 PM
 SSCertifiable Group: General Forum Members Last Login: Tuesday, November 22, 2016 6:32 AM Points: 5,519, Visits: 13,287
 sknox (12/30/2010)So you have to evaluate both sides of the XOR.When first name is null and middle name is not null, the second part of the or expression does not get evaluated. --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba
Post #1041208
 Posted Thursday, December 30, 2010 2:34 PM
 SSCertifiable Group: General Forum Members Last Login: Tuesday, November 22, 2016 6:32 AM Points: 5,519, Visits: 13,287
 LutzM (12/30/2010)Gianluca, thanx for sharing an EXCELLENT article!!!Thanks, Lutz. --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba
Post #1041210
 Posted Thursday, December 30, 2010 2:34 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Monday, May 12, 2014 11:48 AM Points: 175, Visits: 86
 sknox (12/30/2010)Daniel Ruehle (12/30/2010)` ... case when Age > 90 then 1 when Age < 5 then 0 when Gender = 'Male' then 1 when LastName like 'SAM%' then 1 else 0 end = 1`This gets records for all people over the age of 90, males of age 5 or more and anyone with a last name that starts with the letters SAM.Not quite. This gets records for all people over the age of 90, males of age 5 or more, and anyone age 5 or more with a last name that starts with the letters SAM. This will not retrieve a record for someone under age 5 with a last name starting with SAM.Your point about using CASE for explicit short-circuiting is good, but your explanation is a perfect example of how careful you have to be when using CASE, for the same reason.Yes indeed, you need to be VERY careful when using this, especially when using an exclusionary case. With great power comes great responsibility!
Post #1041211

 Permissions