{"id":1770,"date":"2014-01-21T14:51:37","date_gmt":"2014-01-21T22:51:37","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/bobb\/?p=1770"},"modified":"2014-01-21T15:01:40","modified_gmt":"2014-01-21T23:01:40","slug":"compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/","title":{"rendered":"Compiled T-SQL? Don&#8217;t throw away all your SQLCLR code just yet"},"content":{"rendered":"<p>The speed increase from memory-optimized tables in SQL Server 2014 is nice, but I thought, to get the most bang for your buck for really need to use compiled stored procedures in addition. After looking at some of the C code these produce, it almost looked like I was doing direct vector branching into the table&#8217;s C-based specific access routines. But, in the meantime, I&#8217;d worked with a friend of mine who was trying to convert some custom SQLCLR code to compiled T-SQL. He was using table variables in place of CLR objects and, although the code run way faster than the non-compiled T-SQL code, it was still 90% slower than his SQLCLR equivalent. <\/p>\n<p>In general, you&#8217;ll get the biggest speed increase from a compiled sproc if you use do lots of logic in code, or use nested loops in plans that return lots of rows. I thought I had just the use case. <\/p>\n<p>Once upon a time, someone had given me code that did a lot of calculations in SQLCLR and in T-SQL. The SQLCLR code uses 2-and-3-dimensional arrays as well as some data access. The T-SQL code was an unoptimized, line-by-line port of the SQLCLR code used multiple temporary tables and a cursor over outer iterator. You could remove the outer cursor and still SQLCLR performed 100 iterations (100 products in this case) 7-8 times faster than T-SQL performed 1 iteration (1 product). In other words, SQLCLR was about 700-800X faster than T-SQL. Folks used to tell me this was &#8220;cheating&#8221; because of the large number of SQL queries the T-SQL code did (100s of queries, many using INSERT INTO a temp table). Nevertheless, it was worth a try. <\/p>\n<p>Converting the code was pretty straightforward.<br \/>\n  Replace temp tables with strongly-typed in-memory table variables<br \/>\n  Make sure each table variable has a key and index (they&#8217;re required in in-memory table variables)<br \/>\n  Change a subquery into another select into table variable + main query (compiled sprocs don&#8217;t support subqueries yet)<br \/>\n  Make sure everything follows the rules for schemabinding<br \/>\n  Didn&#8217;t use the outer cursor, so the compiled T-SQL code either performed a single iteration or called the compiled proc from within non-compiled T-SQL cursor code<\/p>\n<p>I also had to wait until CTP2, because the procedure used DATEADD. That&#8217;s wasn&#8217;t supported until CTP2 (good choice of what to support sooner, SQL team, thanks for adding date functions).<\/p>\n<p>The results for switching to in-memory tables were encouraging:<br \/>\nSQLCLR &#8211; standard tables &#8211; 5 sec<br \/>\nTSQL   &#8211; standard tables &#8211; 37 sec (x100 = 370 sec) &#8211; 740X slower<br \/>\nTSQL   &#8211; in-memory tables &#8211; 10 sec (x100 = 100 sec) &#8211; 200x slower<br \/>\nSQLCLR doesn&#8217;t support in-memory tables through context connection. I didn&#8217;t try it with an external connection back into the same instance.<\/p>\n<p>Switching to compiled sproc (and pre-compiling the sprocs to factor out the first-time compile overhead for each)<br \/>\nTSQL   &#8211; in-memory tables &#8211; 10 sec (x100 = 100 sec)<br \/>\nCompiled TSQL (which only supports in-memory tables) &#8211; 10 sec (x100 &#8211; 100 sec)<\/p>\n<p>So for this particular use case (just a straight port, didn&#8217;t try to optimize the code), the difference was minimal. BTW, I&#8217;m not pretending this is a benchmark or anywhere near, just a quick test and some round numbers for comparison.<\/p>\n<p>Then I took a harder look at my code. The SQL code was doing a large *number* of SQL statements, each of which used and returned a small number of rows. The computations were done, not in program logic, but with iterations over SQL statements (not a good way to do custom computations in ANY case). Horrible use case, I guess. Lessons learned:<br \/>\n -In-memory tables are not multidimensional arrays or custom &#8220;objects&#8221; as far as speed is concerned.<br \/>\n -The SQL statement still has overhead over in-memory operations even with simple SQL in a compiled sproc<br \/>\n -Iterating with SQL statements doesn&#8217;t substitute for aggregations and computations done in a single SQL statement (I knew that one already).<br \/>\n -Remember where the sweet spot is for compiled stored procedures.<\/p>\n<p>So folks, don&#8217;t throw away your custom SQLCLR sprocs that do custom computations just yet. But do give it a test. And push as much of the computation as possible into the SQL statement and optimize those SQL statements.<\/p>\n<p>Cheers, Bob<br \/>\n@bobbeauch<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The speed increase from memory-optimized tables in SQL Server 2014 is nice, but I thought, to get the most bang for your buck for really need to use compiled stored procedures in addition. After looking at some of the C code these produce, it almost looked like I was doing direct vector branching into the [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,41,38],"tags":[],"class_list":["post-1770","post","type-post","status-publish","format-standard","hentry","category-hekaton","category-sql-server-2014","category-sqlclr"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Compiled T-SQL? Don&#039;t throw away all your SQLCLR code just yet - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Compiled T-SQL? Don&#039;t throw away all your SQLCLR code just yet - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"The speed increase from memory-optimized tables in SQL Server 2014 is nice, but I thought, to get the most bang for your buck for really need to use compiled stored procedures in addition. After looking at some of the C code these produce, it almost looked like I was doing direct vector branching into the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2014-01-21T22:51:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-01-21T23:01:40+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/\",\"name\":\"Compiled T-SQL? Don't throw away all your SQLCLR code just yet - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2014-01-21T22:51:37+00:00\",\"dateModified\":\"2014-01-21T23:01:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hekaton\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/hekaton\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Compiled T-SQL? Don&#8217;t throw away all your SQLCLR code just yet\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Compiled T-SQL? Don't throw away all your SQLCLR code just yet - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/","og_locale":"en_US","og_type":"article","og_title":"Compiled T-SQL? Don't throw away all your SQLCLR code just yet - Bob Beauchemin","og_description":"The speed increase from memory-optimized tables in SQL Server 2014 is nice, but I thought, to get the most bang for your buck for really need to use compiled stored procedures in addition. After looking at some of the C code these produce, it almost looked like I was doing direct vector branching into the [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/","og_site_name":"Bob Beauchemin","article_published_time":"2014-01-21T22:51:37+00:00","article_modified_time":"2014-01-21T23:01:40+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/","name":"Compiled T-SQL? Don't throw away all your SQLCLR code just yet - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2014-01-21T22:51:37+00:00","dateModified":"2014-01-21T23:01:40+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/compiled-t-sql-dont-throw-away-all-your-sqlclr-code-just-yet\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Hekaton","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/hekaton\/"},{"@type":"ListItem","position":3,"name":"Compiled T-SQL? Don&#8217;t throw away all your SQLCLR code just yet"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/1770","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=1770"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/1770\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=1770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=1770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=1770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}