Log in  ::  Register  ::  Not logged in

 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 Wednesday, December 29, 2010 8:26 PM
 SSCertifiable Group: General Forum Members Last Login: Tuesday, November 22, 2016 6:32 AM Points: 5,519, Visits: 13,287
 Comments posted to this topic are about the item Understanding T-SQL Expression Short-CircuitingI would like to acknowledge and thank Gus Gwynne, Jeff Moden and Paul White for their review and the constructive feedback that they gave for this article.For some reason my thanks did not make it to the article page. My humble apologies to all of you. --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba
Post #1040686
 Posted Thursday, December 30, 2010 2:43 AM
 Say Hey Kid Group: General Forum Members Last Login: 2 days ago @ 3:10 PM Points: 662, Visits: 2,926
 This is a great bit of research, with some timely warnings from Gianluca for anyone who makes assumptions about TSQL based on experience with a procedural language. Best wishes,Phil FactorSimple Talk
Post #1040780
 Posted Thursday, December 30, 2010 7:58 AM
 SSCommitted Group: General Forum Members Last Login: 2 days ago @ 3:15 PM Points: 1,860, Visits: 2,656
 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)?
Post #1040910
 Posted Thursday, December 30, 2010 8:23 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Monday, May 12, 2014 11:48 AM Points: 175, Visits: 86
 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.
Post #1040928
 Posted Thursday, December 30, 2010 8:33 AM
 SSCertifiable Group: General Forum Members Last Login: Tuesday, November 22, 2016 6:32 AM Points: 5,519, Visits: 13,287
 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. --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba
Post #1040939
 Posted Thursday, December 30, 2010 8:33 AM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, November 15, 2016 2:01 PM Points: 1, Visits: 68
 Excellent article. Thanks for sharing!
Post #1040941
 Posted Thursday, December 30, 2010 8:55 AM
 SSCertifiable Group: General Forum Members Last Login: Tuesday, November 22, 2016 6:32 AM Points: 5,519, Visits: 13,287
 Phil Factor (12/30/2010)This is a great bit of research, with some timely warnings from Gianluca for anyone who makes assumptions about TSQL based on experience with a procedural language.Thanks, Phil. --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba
Post #1040960
 Posted Thursday, December 30, 2010 9:01 AM
 SSCertifiable Group: General Forum Members Last Login: Tuesday, November 22, 2016 6:32 AM Points: 5,519, Visits: 13,287
 CELKO (12/30/2010)Another short-circuit (or McCarthy) evaluation problem from Algol was functions with side effects:IF a=b OR Boolean_function_with side_effect (x) THEN ..If the function is skipped, then there was no side effect; if it was executed, then a or b might be changed. Thius is why functional programming disallows aside effects.Unfortunately, many programming languages don't disallow side-effects inside functions. It's up to the programmer to produce reliable code and avoid "dirty tricks".As a side note, a CLR function can update data. --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba
Post #1040963
 Posted Thursday, December 30, 2010 9:17 AM
 SSCertifiable Group: General Forum Members Last Login: Tuesday, November 22, 2016 6:32 AM Points: 5,519, Visits: 13,287
 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. --Gianluca SartoriHow to post T-SQL questionsspaghettidba.com@spaghettidba
Post #1040974
 Posted Thursday, December 30, 2010 10:12 AM
 Old Hand Group: General Forum Members Last Login: Thursday, May 8, 2014 10:08 PM Points: 358, Visits: 397
 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?
Post #1041009

 Permissions