{"id":125,"date":"2012-04-20T15:03:04","date_gmt":"2012-04-20T14:03:04","guid":{"rendered":"http:\/\/www.darrencoupland.com\/blog\/?p=125"},"modified":"2012-08-14T14:39:33","modified_gmt":"2012-08-14T13:39:33","slug":"show-sql-plans-in-plan-cache-for-a-table-name-or-matching-partial-query-text","status":"publish","type":"post","link":"https:\/\/www.darrencoupland.com\/blog\/2012\/04\/show-sql-plans-in-plan-cache-for-a-table-name-or-matching-partial-query-text\/","title":{"rendered":"Show SQL Plans in Plan Cache for a table name or matching partial query text"},"content":{"rendered":"<p>Ever wondered what queries are calling what tables or do you have a query and want to see how many times it&#8217;s been called (since last plan cache flush) and view the query plan for it.<\/p>\n<p>These queries are a good to use when performance tuning indexes and should be a part of your toolbox. \u00a0You need to know what queries are using your table before you can start to plan the best indexing strategy for your table.<\/p>\n<p>Note: Query#2 will only work in SQL Server Management Studio 2008 onwards.<\/p>\n<p>Here is a quick and easy way to do this:<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Query#1<\/span>\u00a0<\/strong><\/p>\n<p>This query will show you the plan handle value you need to extract the query plan from the plan cache, it will also show you how\u00a0many times the query has been executed using that plan handle.<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">-- Shows SQL plan handles in plan cache for a specific piece of SQL text\r\nSELECT plan_handle, usecounts AS ExecutionCount, st.text\r\nFROM sys.dm_exec_cached_plans\r\nCROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st\r\nWHERE text LIKE N&#039;%&lt;Enter Table name or partial Query here&gt;%&#039;\r\nORDER BY usecounts DESC<\/pre>\n<p><strong>\u00a0<span style=\"text-decoration: underline;\">Query#2\u00a0<\/span><\/strong><\/p>\n<p>Using the plan_handle value from Query#1 insert it into the where clause and this will give you XML result which you can click on to view the graphical query plan.<\/p>\n<pre class=\"brush: sql; gutter: true; first-line: 1\">-- Show XML query plan ** Only works with SSMS2008 **\r\nSELECT *\r\nFROM\r\nsys.dm_exec_query_plan(&lt;Paste Query &quot;plan_handle&quot; value from previous query here&gt;)<\/pre>\n<p>&nbsp;<\/p>\n<p>Example:<\/p>\n<p>Query#1 Results of &#8216;%EVENTINBOX%&#8217;\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-127\" title=\"planhandle_ex1\" src=\"http:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex1.jpg\" alt=\"\" width=\"839\" height=\"220\" srcset=\"https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex1.jpg 839w, https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex1-300x78.jpg 300w, https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex1-500x131.jpg 500w\" sizes=\"auto, (max-width: 839px) 100vw, 839px\" \/><\/p>\n<p>Query#2 &#8211; XML result of plan_handle (0x06001100A23AD31D40031579060000000000000000000000) from highest exection count of Query#1<img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-128\" title=\"planhandle_ex2\" src=\"http:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex2.jpg\" alt=\"\" width=\"592\" height=\"45\" srcset=\"https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex2.jpg 592w, https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex2-300x22.jpg 300w, https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex2-500x38.jpg 500w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Query#2 &#8211; Visual Query Plan after clicking on XML result show above<img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-129\" title=\"planhandle_ex3\" src=\"http:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex3.jpg\" alt=\"\" width=\"1148\" height=\"120\" srcset=\"https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex3.jpg 1148w, https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex3-300x31.jpg 300w, https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex3-1024x107.jpg 1024w, https:\/\/www.darrencoupland.com\/blog\/wp-content\/uploads\/2012\/04\/planhandle_ex3-500x52.jpg 500w\" sizes=\"auto, (max-width: 1148px) 100vw, 1148px\" \/><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Ever wondered what queries are calling what tables or do you have a query and want to see how many times it&#8217;s been called (since last plan cache flush) and view the query plan for it.<\/p>\n<p>These queries are a good to use when performance tuning indexes and should be a part of your toolbox.  You need to know what queries are using your table before you can start to plan the best indexing strategy for your table.<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[8,22,24,23,60],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-dynamics-ax-2009","tag-ax2009","tag-performance","tag-plan-cache","tag-query-plans","tag-sql-server"],"_links":{"self":[{"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.darrencoupland.com\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}