<?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>T-SQL User-Defined Functions: the good, the bad, and the ugly (part 3)</title><link>http://www2.sqlblog.com/blogs/hugo_kornelis/archive/2012/06/29/t-sql-user-defined-functions-the-good-the-bad-and-the-ugly-part-3.aspx</link><description>I showed why T-SQL scalar user-defined functions are bad for performance in two previous posts. In this post, I will show that CLR scalar user-defined functions are bad as well (though not always quite as bad as T-SQL scalar user-defined functions). I</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: T-SQL User-Defined Functions: the good, the bad, and the ugly (part 3)</title><link>http://www2.sqlblog.com/blogs/hugo_kornelis/archive/2012/06/29/t-sql-user-defined-functions-the-good-the-bad-and-the-ugly-part-3.aspx#44147</link><pubDate>Fri, 29 Jun 2012 20:10:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:44147</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Nice post, Hugo!&lt;/p&gt;
</description></item><item><title>re: T-SQL User-Defined Functions: the good, the bad, and the ugly (part 3)</title><link>http://www2.sqlblog.com/blogs/hugo_kornelis/archive/2012/06/29/t-sql-user-defined-functions-the-good-the-bad-and-the-ugly-part-3.aspx#44184</link><pubDate>Tue, 03 Jul 2012 08:38:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:44184</guid><dc:creator>Jānis</dc:creator><description>&lt;p&gt;T-SQL function is not determined as it don't have &amp;quot;With Schemabinding&amp;quot;.. &lt;/p&gt;
</description></item><item><title>re: T-SQL User-Defined Functions: the good, the bad, and the ugly (part 3)</title><link>http://www2.sqlblog.com/blogs/hugo_kornelis/archive/2012/06/29/t-sql-user-defined-functions-the-good-the-bad-and-the-ugly-part-3.aspx#44227</link><pubDate>Mon, 09 Jul 2012 15:53:13 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:44227</guid><dc:creator>Alejandro Mesa</dc:creator><description>&lt;p&gt;Nice series, Hugo!&lt;/p&gt;
&lt;p&gt;Would you mind testing it one more time, but this time making the UDF determistic by adding the function option SCHEMABINDING, as Jānis suugested?&lt;/p&gt;
&lt;p&gt;May be that is the cause for the marginal difference between both elapsed times.&lt;/p&gt;
&lt;p&gt;Improving query plans with the SCHEMABINDING option on T-SQL UDFs&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/b/sqlprogrammability/archive/2006/05/12/596424.aspx"&gt;http://blogs.msdn.com/b/sqlprogrammability/archive/2006/05/12/596424.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: T-SQL User-Defined Functions: the good, the bad, and the ugly (part 3)</title><link>http://www2.sqlblog.com/blogs/hugo_kornelis/archive/2012/06/29/t-sql-user-defined-functions-the-good-the-bad-and-the-ugly-part-3.aspx#44230</link><pubDate>Mon, 09 Jul 2012 22:12:49 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:44230</guid><dc:creator>Hugo Kornelis</dc:creator><description>&lt;p&gt;Jānis and Alejandro make a great point. I had indeed overlooked the missing schemabinding, that makes the dbo.Distance function non-deterministic. To check the effect, I created another function, with the same body but &amp;quot;WITH SCHEMABINDING&amp;quot; added. Then I tested both versions repeatedly.&lt;/p&gt;
&lt;p&gt;The first thing that struck me was that the results appear to fluctuate. Many tests (for either of the functions) took about 15 seconds, but every so often, suddenly a few test runs would jump to around 30 seconds. That would then last for two or three tests before falling back to the normal 15-second time.&lt;/p&gt;
&lt;p&gt;So I ran a lot of iterations of the two tests. 56, to be precise. Then, I tallied all results. For the original (not schembound, not deterministic) version, the average CPU time was 16.83 seconds, and the average elapsed time was 18.36 second. For the schemabound and deterministic alternative, those numbers were 16.89 and 18.39, so the schemabound version appears to be a tad slower. However, this difference is so small that I think it is just statistical error.&lt;/p&gt;
&lt;p&gt;For the record, the minimum CPU and elapsed time for the functions were 12.78, 14.06, 12.64 and 13.99; and the maximum values were 31.79, 34.84, 32.93 and 35.89. Just to indicate how wide apart some observation were.&lt;/p&gt;
</description></item><item><title>re: T-SQL User-Defined Functions: the good, the bad, and the ugly (part 3)</title><link>http://www2.sqlblog.com/blogs/hugo_kornelis/archive/2012/06/29/t-sql-user-defined-functions-the-good-the-bad-and-the-ugly-part-3.aspx#44917</link><pubDate>Thu, 30 Aug 2012 01:39:28 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:44917</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;Hi Hugo,&lt;/p&gt;
&lt;p&gt;Regarding the sentence, &amp;quot;The only explanation I can come up with is that invoking a T-SQL involves a lot more overhead than invoking a CLR function.&amp;quot;. &amp;nbsp;See the Books Online :Performance of CLR Integration&amp;quot; topic (&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/ms131075.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms131075.aspx&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;There's some useful information there, including the statement, &amp;quot;CLR functions benefit from a quicker invocation path than that of Transact-SQL user-defined functions.&amp;quot;.&lt;/p&gt;
&lt;p&gt;SQLCLR scalar functions without data access are indeed highly preferable to T-SQL scalar functions, and can often perform better than native T-SQL code (without function calls) for surprisingly simple activities. &amp;nbsp;One recent example that springs to mind was implementing DATEFROMPARTS in CLR versus a bunch of nested T-SQL functions.&lt;/p&gt;
&lt;p&gt;I do think your summary statement, &amp;quot;As a rule of thumb, those [CLR scalar functions] are bad for performance too&amp;quot; is over-stated. &amp;nbsp;Avoiding a function altogether (or using an in-line TVF) will often out-perform a 'pointless' CLR scalar function, but failing to apply common sense should not really result in something as strong as a &amp;quot;rule of thumb&amp;quot;, should it?&lt;/p&gt;
&lt;p&gt;Paul&lt;/p&gt;
</description></item><item><title>re: T-SQL User-Defined Functions: the good, the bad, and the ugly (part 3)</title><link>http://www2.sqlblog.com/blogs/hugo_kornelis/archive/2012/06/29/t-sql-user-defined-functions-the-good-the-bad-and-the-ugly-part-3.aspx#45332</link><pubDate>Mon, 24 Sep 2012 20:59:30 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:45332</guid><dc:creator>Robert Folkerts</dc:creator><description>&lt;p&gt;What on Earth does, &amp;quot;lying until it is black in its face&amp;quot; mean? &amp;nbsp;If I lie a great deal, I may turn red in the face from a lack of breathing or embarrassment, but how do you lie until you are black in the face?&lt;/p&gt;
</description></item></channel></rss>