<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx</link><description>Almost six years ago--in November of 2004--I posted what would turn out to be one of my most popular blog posts in terms of number of reads, " Performance: ISNULL vs. COALESCE ." (If you're curious, the post is dated July 2006 because I was too lazy it</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#26624</link><pubDate>Wed, 30 Jun 2010 19:43:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26624</guid><dc:creator>Robert L Davis</dc:creator><description>&lt;p&gt;Reminds me of a discussion on Twitter recently that stemmed out of me not specifying a length for the varchar data type in an example of converting a date to a string.&lt;/p&gt;
&lt;p&gt;They said they had been yelled at more than once for this practice because it was a performance hit. I challenged the person to prove it to me, and in the end, he said that the difference was so slight to be barely detectable.&lt;/p&gt;
</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#26625</link><pubDate>Wed, 30 Jun 2010 20:11:47 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26625</guid><dc:creator>mjswart</dc:creator><description>&lt;p&gt;Regarding the typical abundance of low hanging fruit: Shhh!! you'll spoil it for the rest of us. &lt;/p&gt;
&lt;p&gt;Along similar lines, it's about time we stopped worrying about user defined stored procedures with names that start with &amp;quot;sp&amp;quot;.&lt;/p&gt;
</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#26630</link><pubDate>Wed, 30 Jun 2010 20:29:53 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26630</guid><dc:creator>Mateus</dc:creator><description>&lt;p&gt;That was a nice read, thanks!&lt;/p&gt;
</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#26638</link><pubDate>Wed, 30 Jun 2010 21:46:15 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26638</guid><dc:creator>Brad Schulz</dc:creator><description>&lt;p&gt;I guess the reason that ISNULL works for creating non-null columns with SELECT...INFO and COALESCE doesn't is because COALESCE is really just blindly translated into a CASE expression by the engine.&lt;/p&gt;
&lt;p&gt;In other words... &lt;/p&gt;
&lt;p&gt;SELECT COALESCE(Color,'')&lt;/p&gt;
&lt;p&gt;is translated to...&lt;/p&gt;
&lt;p&gt;SELECT CASE WHEN Color IS NOT NULL THEN Color ELSE '' END&lt;/p&gt;
&lt;p&gt;And I guess it's harder to parse out that CASE expression to determine nullability than it is to interpret a simple ISNULL.&lt;/p&gt;
&lt;p&gt;Just my guess...&lt;/p&gt;
&lt;p&gt;--Brad&lt;/p&gt;
</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#26766</link><pubDate>Tue, 06 Jul 2010 16:56:04 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26766</guid><dc:creator>NULLgarity</dc:creator><description>&lt;p&gt;Excellent post... &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#26768</link><pubDate>Tue, 06 Jul 2010 18:20:25 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26768</guid><dc:creator>Marc Brooks</dc:creator><description>&lt;p&gt;The reason COALESCE doesn't work in the SELECT INTO is that at the &amp;quot;meta level&amp;quot;, there's no guarantee that COALESCE's output value is non-null. e.g in this the COALESCE is NULL for the first row:&lt;/p&gt;
&lt;p&gt;WITH Point(x,y) AS&lt;/p&gt;
&lt;p&gt;(&lt;/p&gt;
&lt;p&gt;	SELECT NULL AS x, NULL AS y&lt;/p&gt;
&lt;p&gt;	UNION ALL &lt;/p&gt;
&lt;p&gt;	SELECT NULL, 1&lt;/p&gt;
&lt;p&gt;	UNION ALL&lt;/p&gt;
&lt;p&gt;	SELECT 2, NULL&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;SELECT ISNULL(x,0) AS NotNullX&lt;/p&gt;
&lt;p&gt; &amp;nbsp;, ISNULL(y,0) AS NotNullY&lt;/p&gt;
&lt;p&gt; &amp;nbsp;, COALESCE(x,y) AS IsNullXY&lt;/p&gt;
&lt;p&gt; FROM Point;&lt;/p&gt;
</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#26770</link><pubDate>Tue, 06 Jul 2010 19:34:58 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:26770</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Marc: Your COALESCE returns a NULL for that row only because you're not using a constant for the final argument. If you were to use a constant, COALESCE would never return a NULL.&lt;/p&gt;</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#27134</link><pubDate>Mon, 19 Jul 2010 12:29:07 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27134</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;Excellent post today, Adam!&lt;/p&gt;
&lt;p&gt;The behaviour seen in the Color example is fascinating. &amp;nbsp;It doesn't surprise me that only ISNULL works to generate a NOT NULL column with SELECT...INTO since that's the same arrangement that works for computed columns, and that's a documented feature (&amp;quot;Computed Columns&amp;quot; in Books Online, right at the bottom of the entry).&lt;/p&gt;
&lt;p&gt;What is *interesting* is that the &amp;quot;WHERE Color IS NOT NULL&amp;quot; part is insufficient to produce a NOT NULL column without the ISNULL.&lt;/p&gt;
&lt;p&gt;The optimiser tracks column NULLability as it explores plan alternatives via 'properties', which can result in extra optimisation opportunities. &amp;nbsp;The &amp;quot;WHERE Color IS NOT NULL&amp;quot; predicate sets a QO property bit to let later operators know that NULLs are not possible in that column from that point forward. &amp;nbsp;(So far so good.)&lt;/p&gt;
&lt;p&gt;You can see that in action since the query plan for the ISNULL version _does not contain_ an ISNULL expression anywhere! &amp;nbsp;The ISNULL was in the Compute Scalar, but was optimised away by later application of a rule recognising that performing an ISNULL operation on an attribute that cannot be NULL is redundant and wasteful.&lt;/p&gt;
&lt;p&gt;Nevertheless, even though the ISNULL was optimised away, the fact that it was there originally was enough to trigger the rule that defines the destination column as NOT NULL.&lt;/p&gt;
&lt;p&gt;What I find surprising is that the SELECT...INTO doesn't use the QO property information when deciding if the column should be created as NULLable or not, without the ISNULL. &amp;nbsp;Seems like a bug (of omission).&lt;/p&gt;
&lt;p&gt;Something else just to illustrate the point. &amp;nbsp;If we run:&lt;/p&gt;
&lt;p&gt;DECLARE @C TABLE (Color NVARCHAR(15) NOT NULL); -- Note NOT NULL&lt;/p&gt;
&lt;p&gt;INSERT &amp;nbsp;@C (Color)&lt;/p&gt;
&lt;p&gt;SELECT &amp;nbsp;Color&lt;/p&gt;
&lt;p&gt;FROM &amp;nbsp; &amp;nbsp;Production.Product;&lt;/p&gt;
&lt;p&gt;...there is a RaiseIfNullInsert in the predicate property of the Table Insert iterator, to check for NULLs. &amp;nbsp;If we add the WHERE Color IS NOT NULL clause, the RaiseIfNullInsert disappears - optimised away by the knowledge that Color cannot be NULL by then. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;So, the property is there, the optimiser 'knows' the column can't contain NULLs and can optimise on that basis. &amp;nbsp;Double weird that the optimiser doesn't reason that in the SELECT...INTO case.&lt;/p&gt;
&lt;p&gt;Apologies for the long comment :)&lt;/p&gt;
&lt;p&gt;Paul&lt;/p&gt;
</description></item><item><title>re: Revisiting ISNULL, COALESCE, and the Perils of Micro-Optimization</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2010/06/30/isnull-coalesce-and-micro-optimization.aspx#28767</link><pubDate>Tue, 14 Sep 2010 19:04:17 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:28767</guid><dc:creator>Bob Frasca</dc:creator><description>&lt;p&gt;Great post Adam. &amp;nbsp;I particularly found your Color example interesting. &amp;nbsp;It just goes to show that every tool has value when used appropriately.&lt;/p&gt;
</description></item></channel></rss>