{"id":2496,"date":"2021-01-19T16:30:00","date_gmt":"2021-01-19T14:30:00","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=2496"},"modified":"2021-01-18T00:19:22","modified_gmt":"2021-01-17T22:19:22","slug":"what-is-deferred-compilation","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2021\/01\/19\/what-is-deferred-compilation\/","title":{"rendered":"What is deferred compilation?"},"content":{"rendered":"\n<p>When I talked about row estimations for table variables, I mentioned &#8216;deferred compile&#8217;, but didn&#8217;t give a whole lot of details. What, then, is a deferred compilation? Let&#8217;s start with how batches work normally.<\/p>\n\n\n\n<p>T-SQL is an interpreted language. While we talk about compiles, they&#8217;re not compilations in the sense of what happens to C++. There&#8217;s no conversion of the script to a machine language or intermediate language which is used from that point onwards. Every time a batch executes, it has to be parsed, bound and have an execution plan generated or fetched from cache.<\/p>\n\n\n\n<p>When a batch is parsed, the entire batch is parsed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSET STATISTICS TIME ON\nGO\n\nSELECT * FROM dbo.Clients\n\nSELECT * FROM dbo.StarSystems ss \nINNER JOIN dbo.Stations s ON s.StarSystemID = ss.StarSystemID\n\nSELECT GETDATE()\nGO\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"375\" data-attachment-id=\"2497\" data-permalink=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2021\/01\/19\/what-is-deferred-compilation\/2021-01-13_23-02-34\/\" data-orig-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-13_23-02-34.png\" data-orig-size=\"378,375\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"2021-01-13_23-02-34\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-13_23-02-34-300x298.png\" data-large-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-13_23-02-34.png\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-13_23-02-34.png\" alt=\"\" class=\"wp-image-2497\" srcset=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-13_23-02-34.png 378w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-13_23-02-34-300x298.png 300w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-13_23-02-34-150x150.png 150w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/figure>\n\n\n\n<p>Ignoring the SET STATISTICS, we&#8217;ve got there one batch with two queries in it. Two queries, two execution times but only one parse and compile time. The entire batch was compiled and then the batch was executed.<\/p>\n\n\n\n<p>If we set up an XE session to track compiles, it shows similar<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"239\" data-attachment-id=\"2498\" data-permalink=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2021\/01\/19\/what-is-deferred-compilation\/2021-01-14_00-57-38\/\" data-orig-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38.png\" data-orig-size=\"1031,241\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"2021-01-14_00-57-38\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38-300x70.png\" data-large-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38-1024x239.png\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38-1024x239.png\" alt=\"\" class=\"wp-image-2498\" srcset=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38-1024x239.png 1024w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38-300x70.png 300w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38-768x180.png 768w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38-765x179.png 765w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-14_00-57-38.png 1031w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The XE session shows that the compilations for both queries were completed before either started execution.<\/p>\n\n\n\n<p>But what happens if we reference a table that doesn&#8217;t exist?<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSET STATISTICS TIME ON\nGO\n\nSELECT * FROM dbo.Clients\n\nSELECT * FROM dbo.StarSystems_DoesNotExist ss \nINNER JOIN dbo.Stations s ON s.StarSystemID = ss.StarSystemID\n\nSELECT GETDATE()\nGO\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"937\" height=\"166\" data-attachment-id=\"2500\" data-permalink=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2021\/01\/19\/what-is-deferred-compilation\/2021-01-17_22-16-19\/\" data-orig-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19.png\" data-orig-size=\"937,166\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"2021-01-17_22-16-19\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19-300x53.png\" data-large-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19.png\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19.png\" alt=\"\" class=\"wp-image-2500\" srcset=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19.png 937w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19-300x53.png 300w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19-768x136.png 768w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-17_22-16-19-765x136.png 765w\" sizes=\"auto, (max-width: 937px) 100vw, 937px\" \/><\/figure>\n\n\n\n<p>The first of the queries is still compiled, but not the second. The second can&#8217;t be compiled because it references an object that doesn&#8217;t exist. The first query then executes, and right before when the second query would execute, it&#8217;s sent back to be bound and optimised. In this case, the object still doesn&#8217;t exist and so we get an error.<\/p>\n\n\n\n<p><code><span class=\"has-inline-color has-vivid-red-color\">Msg 208, Level 16, State 1, Line 6<br>Invalid object name 'dbo.StarSystems_DoesNotExist'<\/span><\/code>.<\/p>\n\n\n\n<p>But if the object was created between the start of the batch and the query that uses it, we get a still different result<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nSET STATISTICS TIME ON\nGO\n\nSELECT * FROM dbo.Clients\n\nSELECT * INTO #StarSystems FROM dbo.StarSystems\n\nSELECT * FROM #StarSystems ss \nINNER JOIN dbo.Stations s ON s.StarSystemID = ss.StarSystemID\n\nSELECT GETDATE()\nGO\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"332\" data-attachment-id=\"2501\" data-permalink=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2021\/01\/19\/what-is-deferred-compilation\/2021-01-18_00-13-31\/\" data-orig-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31.png\" data-orig-size=\"1033,335\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"2021-01-18_00-13-31\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31-300x97.png\" data-large-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31-1024x332.png\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31-1024x332.png\" alt=\"\" class=\"wp-image-2501\" srcset=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31-1024x332.png 1024w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31-300x97.png 300w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31-768x249.png 768w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31-765x248.png 765w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2021\/01\/2021-01-18_00-13-31.png 1033w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This time we have three queries. Two of them get an execution plan generated when the batch starts, but the third can&#8217;t, because the table it references doesn&#8217;t exist. Instead, the statement starts executing but can&#8217;t execute because there&#8217;s no plan. It gets sent back to the optimiser to be compiled, then the query executes.<\/p>\n\n\n\n<p>This is a deferred compile (also called deferred resolution). A compile that does not happen when the batch starts, but is rather deferred until the point that the query itself executes, usually because the table does not exist at the point the batch starts..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I talked about row estimations for table variables, I mentioned &#8216;deferred compile&#8217;, but didn&#8217;t give a whole lot of details. What, then, is a deferred compilation? Let&#8217;s start with how batches work normally. T-SQL is an interpreted language. While&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2021\/01\/19\/what-is-deferred-compilation\/\">(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":"Blog post: What is deferred compilation?","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":[23,29,15],"tags":[],"class_list":["post-2496","post","type-post","status-publish","format-standard","hentry","category-execution-plans","category-internals","category-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-Eg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/2496","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=2496"}],"version-history":[{"count":2,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/2496\/revisions"}],"predecessor-version":[{"id":2502,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/2496\/revisions\/2502"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=2496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=2496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=2496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}