{"id":1075,"date":"2016-03-26T10:31:01","date_gmt":"2016-03-26T17:31:01","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/glenn\/?p=1075"},"modified":"2017-04-13T12:28:58","modified_gmt":"2017-04-13T19:28:58","slug":"eight-different-ways-to-clear-the-sql-server-plan-cache","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/","title":{"rendered":"Eight Different Ways to Clear the SQL Server Plan Cache"},"content":{"rendered":"<p>Nearly anytime you see the command DBCC FREEPROCCACHE mentioned in a blog post, magazine article or book, you usually get some sort of a scary warning about how you should not use it on a production system, or else life as we know it will end. For example, <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/database-console-commands\/dbcc-freeproccache-transact-sql\">Books Online says this<\/a>:<\/p>\n<blockquote><p>Use DBCC FREEPROCCACHE to clear the plan cache carefully. Freeing the plan cache causes, for example, a stored procedure to be recompiled instead of reused from the cache. This can cause a sudden, temporary decrease in query performance. For each cleared cachestore in the plan cache, the SQL Server error log will contain the following informational message: &#8220;SQL Server has encountered %d occurrence(s) of cachestore flush for the \u2018%s\u2019 cachestore (part of plan cache) due to \u2018DBCC FREEPROCCACHE\u2019 or \u2018DBCC FREESYSTEMCACHE\u2019 operations.&#8221; This message is logged every five minutes as long as the cache is flushed within that time interval.<\/p><\/blockquote>\n<p>I would argue that running DBCC FREEPROCCACHE does not cause that much distress with a modern processor, even on a very busy OLTP system. It will cause a pretty minor CPU spike for a few seconds on most systems as the query plans get recompiled as they are executed. It can actually be pretty useful for resetting the cached_time time for <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/system-dynamic-management-views\/sys-dm-exec-procedure-stats-transact-sql\">sys.dm_exec_procedure_stats<\/a> so that it is the same for most of the stored procedures in your normal workload. That makes it easier to pick out your most expensive queries or stored procedures on a cumulative basis when you are looking at things like total worker time or total logical reads.<\/p>\n<p>Having said all that, I want to show a few methods for clearing all or part of the plan cache that are somewhat less impactful on the system. Running DBCC FREEPROCCACHE is kind of a brute force approach, so if you are concerned about that, you can run one of the variations shown below:<\/p>\n<pre class=\"csharpcode\"><span class=\"rem\">-- Eight different ways to clear the plan cache<\/span>\r\n<span class=\"rem\">-- Glenn Berry<\/span>\r\n<span class=\"rem\">-- SQLskills.com<\/span>\r\n    \r\n\r\n\r\n<span class=\"rem\">-- Example 1 ***********************<\/span>\r\n<span class=\"rem\">-- Remove all elements from the plan cache for the entire instance <\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE;\r\n\r\n\r\n<span class=\"rem\">-- Example 2 ***********************<\/span>\r\n<span class=\"rem\">-- Flush the plan cache for the entire instance and suppress the regular completion message<\/span>\r\n<span class=\"rem\">-- \"DBCC execution completed. If DBCC printed error messages, contact your system administrator.\" <\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE <span class=\"kwrd\">WITH<\/span> NO_INFOMSGS;\r\n\r\n\r\n<span class=\"rem\">-- Example 3 ***********************<\/span>\r\n<span class=\"rem\">-- Flush the ad hoc and prepared plan cache for the entire instance<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREESYSTEMCACHE (<span class=\"str\">'SQL Plans'<\/span>);\r\n\r\n\r\n<span class=\"rem\">-- Example 4 ***********************<\/span>\r\n<span class=\"rem\">-- Flush the ad hoc and prepared plan cache for one resource pool<\/span>\r\n\r\n<span class=\"rem\">-- Get Resource Pool information<\/span>\r\n<span class=\"kwrd\">SELECT<\/span> name <span class=\"kwrd\">AS<\/span> [Resource Pool Name], cache_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [cache_memory (MB)], \r\n        used_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [used_memory (MB)]\r\n<span class=\"kwrd\">FROM<\/span> sys.dm_resource_governor_resource_pools;\r\n\r\n<span class=\"rem\">-- Flush the ad hoc and prepared plan cache for one resource pool<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREESYSTEMCACHE (<span class=\"str\">'SQL Plans'<\/span>, <span class=\"str\">'LimitedIOPool'<\/span>);\r\n\r\n\r\n<span class=\"rem\">-- Example 5 **********************<\/span>\r\n<span class=\"rem\">-- Flush the entire plan cache for one resource pool<\/span>\r\n\r\n<span class=\"rem\">-- Get Resource Pool information<\/span>\r\n<span class=\"kwrd\">SELECT<\/span> name <span class=\"kwrd\">AS<\/span> [Resource Pool Name], cache_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [cache_memory (MB)], \r\n        used_memory_kb\/1024.0 <span class=\"kwrd\">AS<\/span> [used_memory (MB)]\r\n<span class=\"kwrd\">FROM<\/span> sys.dm_resource_governor_resource_pools;\r\n\r\n\r\n<span class=\"rem\">-- Flush the plan cache for one resource pool<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE (<span class=\"str\">'LimitedIOPool'<\/span>);\r\n<span class=\"kwrd\">GO<\/span>\r\n\r\n\r\n<span class=\"rem\">-- Example 6 **********************<\/span>\r\n<span class=\"rem\">-- Remove all elements from the plan cache for one database (does not work in SQL Azure) <\/span>\r\n\r\n<span class=\"rem\">-- Get DBID from one database name first<\/span>\r\n<span class=\"kwrd\">DECLARE<\/span> @intDBID <span class=\"kwrd\">INT<\/span>;\r\n<span class=\"kwrd\">SET<\/span> @intDBID = (<span class=\"kwrd\">SELECT<\/span> [dbid] \r\n                <span class=\"kwrd\">FROM<\/span> master.dbo.sysdatabases \r\n                <span class=\"kwrd\">WHERE<\/span> name = N<span class=\"str\">'AdventureWorks2014'<\/span>);\r\n\r\n<span class=\"rem\">-- Flush the plan cache for one database only<\/span>\r\n<span class=\"kwrd\">DBCC<\/span> FLUSHPROCINDB (@intDBID);\r\n\r\n\r\n\r\n<span class=\"rem\">-- Example 7 **********************<\/span>\r\n<span class=\"rem\">-- Clear plan cache for the current database<\/span>\r\n\r\n<span class=\"kwrd\">USE<\/span> AdventureWorks2014;\r\n<span class=\"kwrd\">GO<\/span>\r\n<span class=\"rem\">-- Clear plan cache for the current database<\/span>\r\n<span class=\"rem\">-- New in SQL Server 2016 and SQL Azure<\/span>\r\n<span class=\"kwrd\">ALTER<\/span> <span class=\"kwrd\">DATABASE<\/span> SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE;\r\n\r\n\r\n\r\n<span class=\"rem\">-- Example 8 **********************<\/span>\r\n<span class=\"rem\">-- Remove one query plan from the cache<\/span>\r\n\r\n<span class=\"kwrd\">USE<\/span> AdventureWorks2014;\r\n<span class=\"kwrd\">GO<\/span>\r\n\r\n<span class=\"rem\">-- Run a stored procedure or query<\/span>\r\n<span class=\"kwrd\">EXEC<\/span> dbo.uspGetEmployeeManagers 9;\r\n\r\n<span class=\"rem\">-- Find the plan handle for that query <\/span>\r\n<span class=\"rem\">-- OPTION (RECOMPILE) keeps this query from going into the plan cache<\/span>\r\n<span class=\"kwrd\">SELECT<\/span> cp.plan_handle, cp.objtype, cp.usecounts, \r\nDB_NAME(st.dbid) <span class=\"kwrd\">AS<\/span> [DatabaseName]<\/pre>\n<p>[text][\/text]<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">FROM<\/span> sys.dm_exec_cached_plans <span class=\"kwrd\">AS<\/span> cp <span class=\"kwrd\">CROSS<\/span> APPLY sys.dm_exec_sql_text(plan_handle) <span class=\"kwrd\">AS<\/span> st \r\n<span class=\"kwrd\">WHERE OBJECT_NAME (st.objectid)<\/span><\/pre>\n<p>[text][\/text]<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">LIKE<\/span> N<span class=\"str\">'%uspGetEmployeeManagers%'<\/span> <span class=\"kwrd\">OPTION<\/span> (RECOMPILE); \r\n\r\n<span class=\"rem\">-- Remove the specific query plan from the cache using the plan handle from the above query<\/span> \r\n<span class=\"kwrd\">DBCC<\/span> FREEPROCCACHE (0x050011007A2CC30E204991F30200000001000000000000000000000000000000000000000000000000000000);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nearly anytime you see the command DBCC FREEPROCCACHE mentioned in a blog post, magazine article or book, you usually get some sort of a scary warning about how you should not use it on a production system, or else life as we know it will end. For example, Books Online says this: Use DBCC FREEPROCCACHE [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,156,246],"tags":[281],"class_list":["post-1075","post","type-post","status-publish","format-standard","hentry","category-sql-server-2012","category-sql-server-2014","category-sql-server-2016","tag-sql-server-plan-cache"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Eight Different Ways to Clear the SQL Server Plan Cache - Glenn Berry<\/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\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Eight Different Ways to Clear the SQL Server Plan Cache - Glenn Berry\" \/>\n<meta property=\"og:description\" content=\"Nearly anytime you see the command DBCC FREEPROCCACHE mentioned in a blog post, magazine article or book, you usually get some sort of a scary warning about how you should not use it on a production system, or else life as we know it will end. For example, Books Online says this: Use DBCC FREEPROCCACHE [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/\" \/>\n<meta property=\"og:site_name\" content=\"Glenn Berry\" \/>\n<meta property=\"article:published_time\" content=\"2016-03-26T17:31:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-04-13T19:28:58+00:00\" \/>\n<meta name=\"author\" content=\"Glenn Berry\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Glenn Berry\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/\",\"name\":\"Eight Different Ways to Clear the SQL Server Plan Cache - Glenn Berry\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website\"},\"datePublished\":\"2016-03-26T17:31:01+00:00\",\"dateModified\":\"2017-04-13T19:28:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Eight Different Ways to Clear the SQL Server Plan Cache\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/\",\"name\":\"Glenn Berry\",\"description\":\"Semi-random musings about SQL Server performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7\",\"name\":\"Glenn Berry\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g\",\"caption\":\"Glenn Berry\"},\"sameAs\":[\"https:\/\/www.sqlskills.com\/blogs\/glenn\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/glenn\/author\/glenn\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Eight Different Ways to Clear the SQL Server Plan Cache - Glenn Berry","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\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/","og_locale":"en_US","og_type":"article","og_title":"Eight Different Ways to Clear the SQL Server Plan Cache - Glenn Berry","og_description":"Nearly anytime you see the command DBCC FREEPROCCACHE mentioned in a blog post, magazine article or book, you usually get some sort of a scary warning about how you should not use it on a production system, or else life as we know it will end. For example, Books Online says this: Use DBCC FREEPROCCACHE [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/","og_site_name":"Glenn Berry","article_published_time":"2016-03-26T17:31:01+00:00","article_modified_time":"2017-04-13T19:28:58+00:00","author":"Glenn Berry","twitter_misc":{"Written by":"Glenn Berry","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/","url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/","name":"Eight Different Ways to Clear the SQL Server Plan Cache - Glenn Berry","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website"},"datePublished":"2016-03-26T17:31:01+00:00","dateModified":"2017-04-13T19:28:58+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/eight-different-ways-to-clear-the-sql-server-plan-cache\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/glenn\/"},{"@type":"ListItem","position":2,"name":"Eight Different Ways to Clear the SQL Server Plan Cache"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/","name":"Glenn Berry","description":"Semi-random musings about SQL Server performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/glenn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/57a8972435106bac7970692fcf5edfa7","name":"Glenn Berry","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/glenn\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/64bdac8830f25f2f8cc780f8a1286c66ff1182218009271e7a953639596f7e25?s=96&d=mm&r=g","caption":"Glenn Berry"},"sameAs":["https:\/\/www.sqlskills.com\/blogs\/glenn\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/glenn\/author\/glenn\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts\/1075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/comments?post=1075"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/posts\/1075\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/media?parent=1075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/categories?post=1075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/glenn\/wp-json\/wp\/v2\/tags?post=1075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}