<?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/"
		>
<channel>
	<title>Comments on: Whether to use UNION or OR in SQL Server Queries</title>
	<atom:link href="http://www.sql-server-performance.com/2011/union-or-sql-server-queries/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/</link>
	<description>SQL Server Performance Tuning</description>
	<lastBuildDate>Fri, 14 Jun 2013 19:09:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: Chris</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-455</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Mon, 11 Jul 2011 14:12:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-455</guid>
		<description><![CDATA[Scenario 1:
&quot;the UNION query is not performing well as the  OR query though both are retuning same no of rows (3854).These results indicate that, for the above scenario UNION should be used.&quot;
I think you mean OR should be used in this case.]]></description>
		<content:encoded><![CDATA[<p>Scenario 1:</p>
<p>&#8220;the UNION query is not performing well as the  OR query though both are retuning same no of rows (3854).These results indicate that, for the above scenario UNION should be used.&#8221;</p>
<p>I think you mean OR should be used in this case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bret</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-454</link>
		<dc:creator>Bret</dc:creator>
		<pubDate>Mon, 11 Jul 2011 12:35:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-454</guid>
		<description><![CDATA[The analysis is too simplistic to be useful in any real world scenario. Also, the missing index warning in your query plans (the green text) means the whole exercise was executed on a non-optimal design. Perhaps the speed differences between UNION and OR were entirely due to the missing index.]]></description>
		<content:encoded><![CDATA[<p>The analysis is too simplistic to be useful in any real world scenario. Also, the missing index warning in your query plans (the green text) means the whole exercise was executed on a non-optimal design. Perhaps the speed differences between UNION and OR were entirely due to the missing index.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Øyvind</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-450</link>
		<dc:creator>Øyvind</dc:creator>
		<pubDate>Sun, 10 Jul 2011 07:54:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-450</guid>
		<description><![CDATA[in this example, there would be no difference at all between UNION and UNION ALL, but the latter option would perform better. Choosing normal union (&#039;set union&#039;) in this case, is wrong.]]></description>
		<content:encoded><![CDATA[<p>in this example, there would be no difference at all between UNION and UNION ALL, but the latter option would perform better. Choosing normal union (&#8216;set union&#8217;) in this case, is wrong.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christine</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-446</link>
		<dc:creator>Christine</dc:creator>
		<pubDate>Fri, 08 Jul 2011 17:57:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-446</guid>
		<description><![CDATA[thanks. Just curious how you got the Reads and Duration numbers?]]></description>
		<content:encoded><![CDATA[<p>thanks. Just curious how you got the Reads and Duration numbers?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AGod</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-444</link>
		<dc:creator>AGod</dc:creator>
		<pubDate>Fri, 08 Jul 2011 15:00:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-444</guid>
		<description><![CDATA[How did you generate the tables that compare CPU, Reads, Duration and Row Count? An article explaining how to would be extremely useful.]]></description>
		<content:encoded><![CDATA[<p>How did you generate the tables that compare CPU, Reads, Duration and Row Count? An article explaining how to would be extremely useful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hermes</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-438</link>
		<dc:creator>Hermes</dc:creator>
		<pubDate>Thu, 07 Jul 2011 11:37:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-438</guid>
		<description><![CDATA[Hi all,
please consider that if you have many values for the same condition, you have to dinamically create a select string concatenating many UNION. this statement  cannot be used with, i.e., sp_executesql or stored procedure because the command is every time generated at &quot;runtime&quot; (not compiled?).
sometimes performance are less important than clarity and readness specially when you need to solve problems in production. (consider the effort to analyze many selct in union vs a more simple and clear IN )]]></description>
		<content:encoded><![CDATA[<p>Hi all,<br />
please consider that if you have many values for the same condition, you have to dinamically create a select string concatenating many UNION. this statement  cannot be used with, i.e., sp_executesql or stored procedure because the command is every time generated at &#8220;runtime&#8221; (not compiled?).<br />
sometimes performance are less important than clarity and readness specially when you need to solve problems in production. (consider the effort to analyze many selct in union vs a more simple and clear IN )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AG</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-436</link>
		<dc:creator>AG</dc:creator>
		<pubDate>Thu, 07 Jul 2011 03:46:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-436</guid>
		<description><![CDATA[Regarding Scenario 1, you state that UNION should be used?  Why is this, when it has higher CPU and Reads.
Also the screenshots for the plans in Scenario 3 &amp; 4 look like they need to be swapped around?  nonclustered vs clustered index use]]></description>
		<content:encoded><![CDATA[<p>Regarding Scenario 1, you state that UNION should be used?  Why is this, when it has higher CPU and Reads.</p>
<p>Also the screenshots for the plans in Scenario 3 &amp; 4 look like they need to be swapped around?  nonclustered vs clustered index use</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shash</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-434</link>
		<dc:creator>Shash</dc:creator>
		<pubDate>Wed, 06 Jul 2011 20:25:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-434</guid>
		<description><![CDATA[IN is a fancier way for OR.  Although it reduces the amount of code you have to write, it stills works as OR internally.  With that being said, you will see no or very little difference in performance while using (IN or OR).]]></description>
		<content:encoded><![CDATA[<p>IN is a fancier way for OR.  Although it reduces the amount of code you have to write, it stills works as OR internally.  With that being said, you will see no or very little difference in performance while using (IN or OR).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: piersy</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-431</link>
		<dc:creator>piersy</dc:creator>
		<pubDate>Wed, 06 Jul 2011 14:23:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-431</guid>
		<description><![CDATA[Eek. UNION is most certainly not a like for like replacement for an OR operator.
UNION (minus the ALL) is the equivalent of a SELECT DISTINCT, so could end up with unexpected results, depending on what you are doing.
If ever I see a DISTINCT in a query, alarm bells automatically start ringing.
Not because it doesn&#039;t have its uses, but that in the vast majority of cases when people use it to eliminate duplicates they&#039;ve done so because they didn&#039;t fully understand what they were trying to achieve or have some misunderstanding about the data model.
So assuming this is about the UNION ALL vs OR, short of any huge gain in performance I&#039;d say the OR is both aesthetically more pleasing (less code) and more obvious as to what the semantics of the query are about.
As others have said an IN clause will usually be optimised out identically to a simple OR clause and is even neater.
On a slight aside, in a partitioned view which is effectively a UNION ALL query against identical tables with different check constraints, the optimiser would automatically discount any values that weren&#039;t in the OR clause.]]></description>
		<content:encoded><![CDATA[<p>Eek. UNION is most certainly not a like for like replacement for an OR operator.</p>
<p>UNION (minus the ALL) is the equivalent of a SELECT DISTINCT, so could end up with unexpected results, depending on what you are doing.</p>
<p>If ever I see a DISTINCT in a query, alarm bells automatically start ringing.<br />
Not because it doesn&#8217;t have its uses, but that in the vast majority of cases when people use it to eliminate duplicates they&#8217;ve done so because they didn&#8217;t fully understand what they were trying to achieve or have some misunderstanding about the data model.</p>
<p>So assuming this is about the UNION ALL vs OR, short of any huge gain in performance I&#8217;d say the OR is both aesthetically more pleasing (less code) and more obvious as to what the semantics of the query are about.</p>
<p>As others have said an IN clause will usually be optimised out identically to a simple OR clause and is even neater.</p>
<p>On a slight aside, in a partitioned view which is effectively a UNION ALL query against identical tables with different check constraints, the optimiser would automatically discount any values that weren&#8217;t in the OR clause.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mtassin</title>
		<link>http://www.sql-server-performance.com/2011/union-or-sql-server-queries/#comment-429</link>
		<dc:creator>mtassin</dc:creator>
		<pubDate>Wed, 06 Jul 2011 12:56:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=2504#comment-429</guid>
		<description><![CDATA[I would have to disagree. The OR option doesn’t detect duplicates and eliminate them. Yet the UNION operator is doing this. UNION ALL would be the equivalent to the OR statement.
To make the first query the equivalent of a UNION, not a UNION ALL you would need to rewrite the first query to
SELECT DISTINCT ProductID,SalesOrderID, SalesOrderDetailID FROM Sales.SalesOrderDetail
WHERE ProductID = 714 OR ProductID =709
OR ProductID =998 OR ProductID =875
OR ProductID =976 OR ProductID =874
Your comment is awaiting moderation.]]></description>
		<content:encoded><![CDATA[<p>I would have to disagree. The OR option doesn’t detect duplicates and eliminate them. Yet the UNION operator is doing this. UNION ALL would be the equivalent to the OR statement.</p>
<p>To make the first query the equivalent of a UNION, not a UNION ALL you would need to rewrite the first query to </p>
<p>SELECT DISTINCT ProductID,SalesOrderID, SalesOrderDetailID FROM Sales.SalesOrderDetail<br />
WHERE ProductID = 714 OR ProductID =709<br />
OR ProductID =998 OR ProductID =875<br />
OR ProductID =976 OR ProductID =874</p>
<p>Your comment is awaiting moderation.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
