<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Order of Conditions in WHERE Clauses</title>
	<atom:link href="http://blog.sqlauthority.com/2008/06/08/sql-server-order-of-conditions-in-where-clauses/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/06/08/sql-server-order-of-conditions-in-where-clauses/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Thu, 09 Feb 2012 11:29:19 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Ranjan</title>
		<link>http://blog.sqlauthority.com/2008/06/08/sql-server-order-of-conditions-in-where-clauses/#comment-212989</link>
		<dc:creator><![CDATA[Ranjan]]></dc:creator>
		<pubDate>Wed, 07 Dec 2011 21:40:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=639#comment-212989</guid>
		<description><![CDATA[This is an interesting debate.  I am a PeopleSoft developer and have worked extensivvely on SQLs.  I think order of clause has very less impact.

To justify what I say I have a simple example:
There is a big query which joins many tables and so has various mappings.  Now I want this query to run conditionally. So I declare a variable outside the SQL, and use it in the SQL.  Where ever I place this fairly simple condition ( substitued condition looks like &#039;C&#039; = &#039;C&#039; or &#039;C&#039; = &#039;F&#039;) the query exectution doesnt change much.  It takes aprox 4 secs where a false boolean is encountered.

When the amount of data to be fetched is less, on re-executing the same query many times we get different timings.  This has various parameters like was the Query compiled/executed recnetly, etc.]]></description>
		<content:encoded><![CDATA[<p>This is an interesting debate.  I am a PeopleSoft developer and have worked extensivvely on SQLs.  I think order of clause has very less impact.</p>
<p>To justify what I say I have a simple example:<br />
There is a big query which joins many tables and so has various mappings.  Now I want this query to run conditionally. So I declare a variable outside the SQL, and use it in the SQL.  Where ever I place this fairly simple condition ( substitued condition looks like &#8216;C&#8217; = &#8216;C&#8217; or &#8216;C&#8217; = &#8216;F&#8217;) the query exectution doesnt change much.  It takes aprox 4 secs where a false boolean is encountered.</p>
<p>When the amount of data to be fetched is less, on re-executing the same query many times we get different timings.  This has various parameters like was the Query compiled/executed recnetly, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gaurang Soni</title>
		<link>http://blog.sqlauthority.com/2008/06/08/sql-server-order-of-conditions-in-where-clauses/#comment-58053</link>
		<dc:creator><![CDATA[Gaurang Soni]]></dc:creator>
		<pubDate>Tue, 01 Dec 2009 06:35:24 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=639#comment-58053</guid>
		<description><![CDATA[Also SQL Server 2005 does support Short circuit conditions. I think it was not supported in SQL Server 2000.]]></description>
		<content:encoded><![CDATA[<p>Also SQL Server 2005 does support Short circuit conditions. I think it was not supported in SQL Server 2000.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henry</title>
		<link>http://blog.sqlauthority.com/2008/06/08/sql-server-order-of-conditions-in-where-clauses/#comment-45898</link>
		<dc:creator><![CDATA[Henry]]></dc:creator>
		<pubDate>Thu, 22 Jan 2009 13:12:22 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=639#comment-45898</guid>
		<description><![CDATA[I have been trying to research this, issue as well as the effect of the order of conditions in a join (separated by AND).   

For example, if Table b is partitioned on Col3, could we see a performance improvement by listing a.Col3 = b.Col3 first in the following join?

SELECT a.Col1, b.col1
FROM TableA AS a
INNER JOIN TableB AS b
    ON a.Col1 = b.Col1
AND a.col2 = b.Col2
AND a.Col3 = b.Col3

Some people say that the optimizer will do this internally.   I asked this question of another SQL Server Expert and was told that they are processed in order, so it does make a difference.  He provided the  following queries as evidence:  

This query will not error.  The first condition is met, so the 2nd condition is never processed.  

select 1 
where 1=0
  and cast(&#039;bob&#039; as datetime)=&#039;hello&#039;

This query will error since the first condition is impossible.

select 1 
where 1=1
  and cast(&#039;bob&#039; as datetime)=&#039;hello&#039;

This was shown as proof that conditions are processed in order.  He said that join conditions are processed in the same way.

My initial thinking is that if the column on which the table is partitioned is listed first, SQL Server would only have consider that single partition, saving on reads of the others disks and therefore improving performance of that stored procedure and of the database overall.

When I tested this however, I was unable to see any difference in the execution plan.  

If there is no difference, can you explain to me how SQL Server does this, and why the above queries operate as they do? 

Thank you]]></description>
		<content:encoded><![CDATA[<p>I have been trying to research this, issue as well as the effect of the order of conditions in a join (separated by AND).   </p>
<p>For example, if Table b is partitioned on Col3, could we see a performance improvement by listing a.Col3 = b.Col3 first in the following join?</p>
<p>SELECT a.Col1, b.col1<br />
FROM TableA AS a<br />
INNER JOIN TableB AS b<br />
    ON a.Col1 = b.Col1<br />
AND a.col2 = b.Col2<br />
AND a.Col3 = b.Col3</p>
<p>Some people say that the optimizer will do this internally.   I asked this question of another SQL Server Expert and was told that they are processed in order, so it does make a difference.  He provided the  following queries as evidence:  </p>
<p>This query will not error.  The first condition is met, so the 2nd condition is never processed.  </p>
<p>select 1<br />
where 1=0<br />
  and cast(&#8216;bob&#8217; as datetime)=&#8217;hello&#8217;</p>
<p>This query will error since the first condition is impossible.</p>
<p>select 1<br />
where 1=1<br />
  and cast(&#8216;bob&#8217; as datetime)=&#8217;hello&#8217;</p>
<p>This was shown as proof that conditions are processed in order.  He said that join conditions are processed in the same way.</p>
<p>My initial thinking is that if the column on which the table is partitioned is listed first, SQL Server would only have consider that single partition, saving on reads of the others disks and therefore improving performance of that stored procedure and of the database overall.</p>
<p>When I tested this however, I was unable to see any difference in the execution plan.  </p>
<p>If there is no difference, can you explain to me how SQL Server does this, and why the above queries operate as they do? </p>
<p>Thank you</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sdesai</title>
		<link>http://blog.sqlauthority.com/2008/06/08/sql-server-order-of-conditions-in-where-clauses/#comment-39394</link>
		<dc:creator><![CDATA[sdesai]]></dc:creator>
		<pubDate>Fri, 20 Jun 2008 16:34:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=639#comment-39394</guid>
		<description><![CDATA[I disagree.

case in point:

If you have a column which contains both text and integers and you try to query only int. using ismumeric funtion, it matters where you place this check in your where clause.

Following will fail:

select
from
where convert(int, txtCol) = 1
and isnumeric(txtCol) = 1

Following will work:

select
from
where isnumeric(txtCol) = 1
and convert(int, txtCol) = 1


At least this is my obersvation.]]></description>
		<content:encoded><![CDATA[<p>I disagree.</p>
<p>case in point:</p>
<p>If you have a column which contains both text and integers and you try to query only int. using ismumeric funtion, it matters where you place this check in your where clause.</p>
<p>Following will fail:</p>
<p>select<br />
from<br />
where convert(int, txtCol) = 1<br />
and isnumeric(txtCol) = 1</p>
<p>Following will work:</p>
<p>select<br />
from<br />
where isnumeric(txtCol) = 1<br />
and convert(int, txtCol) = 1</p>
<p>At least this is my obersvation.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

