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 «««34567»»

Understanding T-SQL Expression Short-Circuiting Expand / Collapse
Author
Message
Posted Monday, March 7, 2011 10:30 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:11 AM
Points: 4,373, Visits: 10,653
Thanks for clarifying, Bart.
I filed a Connect item here:
https://connect.microsoft.com/SQLServer/feedback/details/649957/case-expression-evaluates-else-branch-at-compile-time.

Let's see what happens.


--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1074310
Posted Monday, March 21, 2011 7:44 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, June 18, 2014 2:29 PM
Points: 6, Visits: 21
Update to anyone following this: this compile-time exception to normal T-SQL CASE short circuiting is now scheduled to be fixed in an upcoming SQL release, thanks to Gianluca for filing the Connect bug. For now the problem behavior still exists in SQL2005 through SQL2008R2 -- and maybe SQL2000 -- so keep an eye out for it in existing releases as you use CASE for short-circuiting.
Post #1081706
Posted Tuesday, March 22, 2011 1:30 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:11 AM
Points: 4,373, Visits: 10,653
Thanks for the feedback, Bart.
It's nice to see a problem fixed in such a short time.


--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1081768
Posted Saturday, October 22, 2011 4:59 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, October 6, 2014 3:12 AM
Points: 321, Visits: 698
Nice article and nice work on the floating-point exception bug.

Now, to give this dead horse the final whiplash, I was intrigued by the IN handling and modified the set (30,20,10,1) into (30,20,30,10,1). Sure enough, the IN expression was optimized down to 4 ordered OR subexpressions in the query plan like before. Distinct sort seems to be the easiest way to expand only distinct values in the set into OR expressions. It just happens to also sort the values as a benign side-effect.
Post #1194896
Posted Friday, June 21, 2013 2:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, October 9, 2014 10:42 AM
Points: 6,731, Visits: 8,480
I'm glad SSC republished your fine article, Gianluca.
Nice work, great descussions.


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #1466094
Posted Friday, June 21, 2013 3:02 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:11 AM
Points: 4,373, Visits: 10,653
Thanks a lot, Johan.
It's amazing how time passes: it was first published three years ago!


--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #1466095
Posted Friday, June 21, 2013 7:46 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 11:01 AM
Points: 2,006, Visits: 3,420
I must have missed it first time around too. Great article Gianluca, thanks!
Post #1466187
Posted Friday, June 21, 2013 7:47 AM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Thursday, September 4, 2014 2:36 PM
Points: 136, Visits: 324
Thanks for writing this very interesting article!
Post #1466189
Posted Friday, June 21, 2013 9:12 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 10:15 PM
Points: 18, Visits: 841
Very interesting article. I don't think that T-SQL's short-circuiting or lack thereof has ever caused a problem for me. Because most of my coding has been in some version of Visual Basic, which does not short-circuit expressions, if I know that the order of the expressions matters, I tend to write my expressions to avoid problems. For example:

-- IF epressionA AND expressionB THEN Statement1 ELSE Statement2
IF expressionA
BEGIN
IF expressionB
EXEC Statement1
END
ELSE
EXEC Statement2

However, when I am writing in C, I am confident about short-circuiting, so if I have a pointer p that may be NULL, I will happily write:
if (p != NULL && p->field1 == someValue)

Post #1466243
Posted Friday, June 21, 2013 12:31 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Friday, April 4, 2014 4:40 PM
Points: 751, Visits: 917
This was a fantastic article. It was well written, well researched, and useful. Thank you for providing it.

I do have one nitpick and one small thing to add though. You say that tautologies are saying the same thing twice. This is true of rhetorical tautologies. But this is at least highly misleading when talking about logical tautologies. Things like de Morgan's law and the law of the execluded middle carry significant information that is highly useful and while (arguably) each contain within them the same statement twice, the statements as a whole are significant and cannot be stated in any simpler form.

As for the thing to add, towards the end you imply that it is rarely worth explicitly concerning yourself with the short circuiting in SQL Server. I fully agree. But I want to emphasize that this is not true in many procedural languages. In Python I have had cases where I gained very significant speed ups by properly constructing the conditional to take advantage of short circuiting.

It was a really great article, thanks for providing it.


---
Timothy A Wiseman
SQL Blog: http://timothyawiseman.wordpress.com/
Post #1466339
« Prev Topic | Next Topic »

Add to briefcase «««34567»»

Permissions Expand / Collapse