<?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: Efficient T-SQL Code – A Case of Incorrect Use of a UNION Operator</title>
	<atom:link href="http://www.sql-server-performance.com/2013/tsql-incorrect-union-operator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sql-server-performance.com/2013/tsql-incorrect-union-operator/</link>
	<description>SQL Server Performance Tuning</description>
	<lastBuildDate>Fri, 17 May 2013 13:31:24 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
	<item>
		<title>By: Mike Barron</title>
		<link>http://www.sql-server-performance.com/2013/tsql-incorrect-union-operator/#comment-3011</link>
		<dc:creator>Mike Barron</dc:creator>
		<pubDate>Mon, 28 Jan 2013 13:06:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=3540#comment-3011</guid>
		<description><![CDATA[An NP-Complete problem! Thanks Joe, that lead some interesting research.]]></description>
		<content:encoded><![CDATA[<p>An NP-Complete problem! Thanks Joe, that lead some interesting research.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Celko</title>
		<link>http://www.sql-server-performance.com/2013/tsql-incorrect-union-operator/#comment-2970</link>
		<dc:creator>Joe Celko</dc:creator>
		<pubDate>Mon, 21 Jan 2013 15:58:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=3540#comment-2970</guid>
		<description><![CDATA[This UNION vs OR problem has been in SQL for along time, not just T-SQL. The one-table VIEW with the OR is updatable; the UNION version counts as two tables, so the VIEW is not updateable, even tho it is logically equivalent.
When we wrote the Standards, we knew that VIEW updatign is an NP-Complete problem, so we went with the easiest definition for the Standards.]]></description>
		<content:encoded><![CDATA[<p>This UNION vs OR problem has been in SQL for along time, not just T-SQL. The one-table VIEW with the OR is updatable; the UNION version counts as two tables, so the VIEW is not updateable, even tho it is logically equivalent. </p>
<p>When we wrote the Standards, we knew that VIEW updatign is an NP-Complete problem, so we went with the easiest definition for the Standards.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John McVicker</title>
		<link>http://www.sql-server-performance.com/2013/tsql-incorrect-union-operator/#comment-2969</link>
		<dc:creator>John McVicker</dc:creator>
		<pubDate>Mon, 21 Jan 2013 14:38:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.sql-server-performance.com/?p=3540#comment-2969</guid>
		<description><![CDATA[Problem here is this:
student_name like &#039;%N%&#039;
It requires an index scan for the entire leaf-level of the IDX_STUDENT_NAME_CITY  index.  It&#039;s expensive either before the collapse of the union or after.  This use of a leading % negates the use of an index &quot;well&quot; but can be done with an index scan rather than a table scan.  For a 1GB Customer table and 100MB index - it&#039;s better to scan the index, right?  But still poor query processing as a CSR doing a query with %N% for name is a drag on the system if they do it because they are too lazy to type in a name.  Also, some developers pre-load the leading % in some systems (I contract at one of these places).  That is fine with today&#039;s large caches and fast processors - but 10 years ago, this code wouldn&#039;t have been allowed.
We do see the improper use of UNION at customer sites a lot when they use:
SELECT DISTINCT ....
UNION
SELECT DISTINCT ....
UNION
SELECT DISTINCT ....
I continuously have to tell developers not to use DISTINCT when paired with UNION since the UNION itself is a distinct-resulting operator.]]></description>
		<content:encoded><![CDATA[<p>Problem here is this:</p>
<p>student_name like &#8216;%N%&#8217;</p>
<p>It requires an index scan for the entire leaf-level of the IDX_STUDENT_NAME_CITY  index.  It&#8217;s expensive either before the collapse of the union or after.  This use of a leading % negates the use of an index &#8220;well&#8221; but can be done with an index scan rather than a table scan.  For a 1GB Customer table and 100MB index &#8211; it&#8217;s better to scan the index, right?  But still poor query processing as a CSR doing a query with %N% for name is a drag on the system if they do it because they are too lazy to type in a name.  Also, some developers pre-load the leading % in some systems (I contract at one of these places).  That is fine with today&#8217;s large caches and fast processors &#8211; but 10 years ago, this code wouldn&#8217;t have been allowed.</p>
<p>We do see the improper use of UNION at customer sites a lot when they use:</p>
<p>SELECT DISTINCT &#8230;.<br />
UNION<br />
SELECT DISTINCT &#8230;.<br />
UNION<br />
SELECT DISTINCT &#8230;.</p>
<p>I continuously have to tell developers not to use DISTINCT when paired with UNION since the UNION itself is a distinct-resulting operator.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
