Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Understanding T-SQL Expression Short-Circuiting Expand / Collapse
Author
Message
Posted Thursday, December 30, 2010 10:12 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld 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
Posted Thursday, December 30, 2010 10:26 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 4:55 PM
Points: 1,293, Visits: 1,645
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 user
WHERE (first_name IS NULL) XOR (middle_name IS NULL)

-- This is how you have to code it with AND, OR and NOT operators
SELECT *
FROM user
WHERE (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

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 1:11 PM
Points: 7,019, Visits: 12,909
Gianluca, thanx for sharing an EXCELLENT article!!!



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1041024
Posted Thursday, December 30, 2010 11:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, May 12, 2014 11:48 AM
Points: 161, 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
Person
where
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


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 9, 2014 3:30 PM
Points: 352, Visits: 1,868
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
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 4:55 PM
Points: 1,293, Visits: 1,645
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
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 4:55 PM
Points: 1,293, Visits: 1,645
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


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:21 PM
Points: 11,192, Visits: 11,090
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 White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1041170
Posted Thursday, December 30, 2010 2:30 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:00 AM
Points: 5,014, Visits: 10,513
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 Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1041208
Posted Thursday, December 30, 2010 2:34 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:00 AM
Points: 5,014, Visits: 10,513
LutzM (12/30/2010)
Gianluca, thanx for sharing an EXCELLENT article!!!

Thanks, Lutz.


--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1041210
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse