{"id":1104,"date":"2020-03-23T08:50:57","date_gmt":"2020-03-23T15:50:57","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/erin\/?p=1104"},"modified":"2021-02-17T18:50:50","modified_gmt":"2021-02-18T02:50:50","slug":"how-was-a-plan-forced","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/","title":{"rendered":"How was a plan forced?"},"content":{"rendered":"\n<p>If you use Automatic Plan Correction, and thus also Query Store, you may wonder how was a plan forced: manually or automatically with APC?&nbsp; The type of forced plan is tracked in sys.query_store_plan, and you can use this simple query to determine how a plan was forced:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT\n   p.is_forced_plan,\n   p.plan_forcing_type,\n   p.plan_forcing_type_desc\nFROM sys.query_store_plan p\nWHERE p.is_forced_plan = 1;\nGO<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/howforced.jpg\"><img fetchpriority=\"high\" decoding=\"async\" width=\"742\" height=\"137\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/howforced.jpg\" alt=\"Manual vs. Automatically Forced\" class=\"wp-image-1105\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/howforced.jpg 742w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/howforced-300x55.jpg 300w\" sizes=\"(max-width: 742px) 100vw, 742px\" \/><\/a><figcaption>Manual vs. Automatically Forced<\/figcaption><\/figure><\/div>\n\n\n\n<p>Now, this information is a good start, but we can do better if we join to sys.dm_db_tuning_recommendations and the other Query Store view.&nbsp; The query below is not pretty I know \ud83d\ude42 but it does the job:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>;WITH ForcedPlanInfo\nAS\n   (SELECT\n      &#91;FP].&#91;QueryId] &#91;QueryID],\n      &#91;Reason],\n      JSON_VALUE(state, '$.currentValue') &#91;CurrentValue],\n      &#91;FP].&#91;RegressedPlanId] &#91;RegressedPlanID],\n      &#91;FP].&#91;ForcedPlanID] &#91;ForcedPlanID],\n      &#91;execute_action_start_time],\n      JSON_VALUE(details, '$.implementationDetails.script') &#91;Script]\n   FROM &#91;sys].&#91;dm_db_tuning_recommendations]\n   CROSS APPLY OPENJSON (Details, '$.planForceDetails')\n   WITH (\n      &#91;QueryId] INT '$.queryId',\n      &#91;RegressedPlanId] INT '$.regressedPlanId',\n      &#91;ForcedPlanID] INT '$.recommendedPlanId'\n   ) AS FP\n)\nSELECT\n   qt.query_text_id,\n   q.query_id,\n   p.plan_id,\n   p.is_forced_plan,\n   p.plan_forcing_type,\n   p.plan_forcing_type_desc,\n   qt.query_sql_text,\n   TRY_CAST(p.query_plan AS XML),\n   tr.QueryID,\n   tr.Reason,\n   tr.CurrentValue,\n   tr.RegressedPlanID,\n   tr.ForcedPlanID,\n   tr.execute_action_start_time,\n   tr.Script\nFROM sys.query_store_plan p\nLEFT OUTER JOIN ForcedPlanInfo tr\n   ON p.plan_id = tr.ForcedPlanID\nJOIN sys.query_store_query q\n   ON p.query_id = q.query_id\nJOIN sys.query_store_query_text qt\n   ON q.query_text_id = qt.query_text_id\nWHERE p.is_forced_plan = 1;\nGO<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output1.jpg\"><img decoding=\"async\" width=\"1024\" height=\"45\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output1-1024x45.jpg\" alt=\"Plan Forcing Output Part 1\" class=\"wp-image-1106\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output1-1024x45.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output1-300x13.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output1-768x34.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>Plan Forcing Output Part 1<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output2.jpg\"><img decoding=\"async\" width=\"1024\" height=\"48\" src=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output2-1024x48.jpg\" alt=\"Plan Forcing Output Part 2\" class=\"wp-image-1107\" srcset=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output2-1024x48.jpg 1024w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output2-300x14.jpg 300w, https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/output2-768x36.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>Plan Forcing Output Part 2<\/figcaption><\/figure><\/div>\n\n\n\n<p>As you can see, I broke the query output into two parts because it&#8217;s fairly wide, and for the plan that was forced manually, there are columns that are not relevant.<\/p>\n\n\n\n<p>The only additional item I would want to display in this output is the user that forced the plan manually.&nbsp; Unfortunately, this information is not currently captured by SQL Server.&nbsp; You will not find it in an Extended Event or in the SQL Server ERRORLOG; hopefully that will be addressed soon.<\/p>\n\n\n\n<p>Hope this helps those of you that are using Query Store and Automatic Plan Correction!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you use Automatic Plan Correction, and thus also Query Store, you may wonder how was a plan forced: manually or automatically with APC?&nbsp; The type of forced plan is tracked in sys.query_store_plan, and you can use this simple query to determine how a plan was forced: Now, this information is a good start, but [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How was a plan forced? - Erin Stellato<\/title>\n<meta name=\"description\" content=\"If you are using Query Store and forcing plans, both manually and with APC, you may wonder how was a plan forced: manually or automatically.\" \/>\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\/erin\/how-was-a-plan-forced\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How was a plan forced? - Erin Stellato\" \/>\n<meta property=\"og:description\" content=\"If you are using Query Store and forcing plans, both manually and with APC, you may wonder how was a plan forced: manually or automatically.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/\" \/>\n<meta property=\"og:site_name\" content=\"Erin Stellato\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-23T15:50:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-18T02:50:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/howforced.jpg\" \/>\n<meta name=\"author\" content=\"Erin Stellato\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Erin Stellato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/\",\"name\":\"How was a plan forced? - Erin Stellato\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\"},\"datePublished\":\"2020-03-23T15:50:57+00:00\",\"dateModified\":\"2021-02-18T02:50:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\"},\"description\":\"If you are using Query Store and forcing plans, both manually and with APC, you may wonder how was a plan forced: manually or automatically.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How was a plan forced?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/\",\"name\":\"Erin Stellato\",\"description\":\"The SQL Sequel\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158\",\"name\":\"Erin Stellato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g\",\"caption\":\"Erin Stellato\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/erin\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How was a plan forced? - Erin Stellato","description":"If you are using Query Store and forcing plans, both manually and with APC, you may wonder how was a plan forced: manually or automatically.","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\/erin\/how-was-a-plan-forced\/","og_locale":"en_US","og_type":"article","og_title":"How was a plan forced? - Erin Stellato","og_description":"If you are using Query Store and forcing plans, both manually and with APC, you may wonder how was a plan forced: manually or automatically.","og_url":"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/","og_site_name":"Erin Stellato","article_published_time":"2020-03-23T15:50:57+00:00","article_modified_time":"2021-02-18T02:50:50+00:00","og_image":[{"url":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-content\/uploads\/2020\/03\/howforced.jpg"}],"author":"Erin Stellato","twitter_misc":{"Written by":"Erin Stellato","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/","name":"How was a plan forced? - Erin Stellato","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website"},"datePublished":"2020-03-23T15:50:57+00:00","dateModified":"2021-02-18T02:50:50+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158"},"description":"If you are using Query Store and forcing plans, both manually and with APC, you may wonder how was a plan forced: manually or automatically.","breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/how-was-a-plan-forced\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/erin\/"},{"@type":"ListItem","position":2,"name":"How was a plan forced?"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/erin\/","name":"Erin Stellato","description":"The SQL Sequel","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/erin\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/76170223ffffa1df03fd9be5b66cb158","name":"Erin Stellato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/erin\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0c8b485bd54ea26b57e99f79b525f409?s=96&d=mm&r=g","caption":"Erin Stellato"},"sameAs":["http:\/\/3.209.169.194\/blogs\/erin"],"url":"https:\/\/www.sqlskills.com\/blogs\/erin\/author\/erin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/1104"}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/comments?post=1104"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/posts\/1104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/media?parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/categories?post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/erin\/wp-json\/wp\/v2\/tags?post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}