{"id":1746,"date":"2016-01-12T16:30:00","date_gmt":"2016-01-12T14:30:00","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=1746"},"modified":"2015-12-31T13:56:09","modified_gmt":"2015-12-31T11:56:09","slug":"natively-compiled-user-defined-functions","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2016\/01\/12\/natively-compiled-user-defined-functions\/","title":{"rendered":"Natively compiled user-defined functions"},"content":{"rendered":"<p>One new thing that SQL Server 2016 has added is the ability to natively compile user-defined functions. Previously, native compilation, part of the hekaton feature, was limited to stored procedures.<\/p>\n<p>When I saw that, the first question that came to mind is whether natively compiling a scalar function reduces the overhead when calling that function within another query. I\u2019m not talking about data-accessing scalar UDFs, since natively compiled functions can only access in-memory tables, but functions that do simple manipulation of the parameters passed in. String formatting, for example, or date manipulation.<\/p>\n<p>While not as harmful as data-accessing scalar UDFs, there\u2019s still overhead as these are not inline functions, they\u2019re called for each row in the resultset (as a look at the Stored Procedure Completed XE event would show), and the call to the function takes time. Admittedly not a lot of time, but when it\u2019s on each row of a large resultset the total can be noticeable.<\/p>\n<p>I decided to do a simple test. A query against a table with ~600k rows, one query with a traditional scalar function, one with a natively compiled function and one with the function\u2019s contents in the query as a column.<\/p>\n<p>The two functions:<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">CREATE FUNCTION dbo.DateOnly (@Input DATETIME)\r\nRETURNS DATETIME\r\nAS\r\nBEGIN\r\n  RETURN DATEADD(dd, DATEDIFF (dd, 0, @Input), 0);\r\nEND\r\nGO<\/pre>\n<p>and<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">CREATE FUNCTION dbo.DateOnlyHekaton (@Input DATETIME)\r\nRETURNS DATETIME\r\nWITH NATIVE_COMPILATION, SCHEMABINDING\r\nAS\r\nBEGIN ATOMIC WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'English')\r\n  RETURN DATEADD(dd,DATEDIFF(dd,0,@Input),0);\r\nEND\r\nGO<\/pre>\n<p>And the query without the function would be a simple<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">SELECT DATEADD(dd, DATEDIFF(dd, 0, SomeDate), 0)\r\nFROM SomeTable;<\/pre>\n<p>Durations and CPU usage were caught with Extended Events. I ran each query 25 times and aggregated the results.<\/p>\n<table border=\"0\" width=\"473\" cellspacing=\"0\" cellpadding=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"190\"><\/td>\n<td valign=\"top\" width=\"136\">Average CPU (ms)<\/td>\n<td valign=\"top\" width=\"145\">Average Duration (ms)<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"199\">In-line expression<\/td>\n<td valign=\"top\" width=\"136\">289<\/td>\n<td valign=\"top\" width=\"145\">294<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"201\">Normal function<\/td>\n<td valign=\"top\" width=\"136\">3555<\/td>\n<td valign=\"top\" width=\"145\">3814<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"201\">Natively Compiled Function<\/td>\n<td valign=\"top\" width=\"136\">3318<\/td>\n<td valign=\"top\" width=\"145\">3352<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Not quite what I hoped. While the overhead of the natively compiled function is lower, it\u2019s lower only by about 10%, which really is not worth it, now when we\u2019re talking about an order of magnitude difference from the query without the function call.<\/p>\n<p>Looks like the guidance is still going to be to not use scalar UDFs within other queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One new thing that SQL Server 2016 has added is the ability to natively compile user-defined functions. Previously, native compilation, part of the hekaton feature, was limited to stored procedures. When I saw that, the first question that came to&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2016\/01\/12\/natively-compiled-user-defined-functions\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New blog post: Natively compiled user-defined functions","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[25,15,16],"tags":[],"class_list":["post-1746","post","type-post","status-publish","format-standard","hentry","category-performance","category-sql-server","category-syndication"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-sa","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=1746"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1746\/revisions"}],"predecessor-version":[{"id":1751,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1746\/revisions\/1751"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=1746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=1746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=1746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}