{"id":507,"date":"2010-04-22T17:50:00","date_gmt":"2010-04-22T17:50:00","guid":{"rendered":"\/blogs\/kimberly\/post\/Procedure-cache-and-optimizing-for-adhoc-workloads.aspx"},"modified":"2015-11-03T10:39:28","modified_gmt":"2015-11-03T18:39:28","slug":"plan-cache-and-optimizing-for-adhoc-workloads","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/plan-cache-and-optimizing-for-adhoc-workloads\/","title":{"rendered":"Plan cache and optimizing for adhoc workloads"},"content":{"rendered":"<p>I mentioned that servers receiving a lot of dynamic constructs (typically those generated by client SQL apps\/ORMs\/LINQ, etc.) can start to consume too much plan cache and have problems with &#8220;single-use plans&#8221; in my last post titled: <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/statement-execution-and-why-you-should-use-stored-procedures\/\">Statement execution and why you should use stored procedures<\/a>. I also mentioned that SQL Server 2008 has an option\/feature specifically to help reduce the bloat on the plan cache by only storing a query plan hash the first time a plan is created.<\/p>\n<p>First &#8211; let&#8217;s check to see how your plan cache is currently allocated:<\/p>\n<p>(<strong>note:<\/strong> updated to decimal(18,2) as a few of you had overflow errors due to high use counts!)<\/p>\n<p>SELECT objtype AS [CacheType],<br \/>\n    COUNT_BIG(*) AS [Total Plans],<br \/>\n    SUM(CAST(size_in_bytes AS DECIMAL(18, 2))) \/ 1024 \/ 1024 AS [Total MBs],<br \/>\n    AVG(usecounts) AS [Avg Use Count],<br \/>\n    SUM(CAST((CASE WHEN usecounts = 1 THEN size_in_bytes<br \/>\n        ELSE 0<br \/>\n        END) AS DECIMAL(18, 2))) \/ 1024 \/ 1024 AS [Total MBs \u2013 USE Count 1],<br \/>\n    SUM(CASE WHEN usecounts = 1 THEN 1<br \/>\n        ELSE 0<br \/>\n        END) AS [Total Plans \u2013 USE Count 1]<br \/>\nFROM sys.dm_exec_cached_plans<br \/>\nGROUP BY objtype<br \/>\nORDER BY [Total MBs \u2013 USE Count 1] DESC<br \/>\nGO<\/p>\n<p><span style=\"color: #0000ff;\"><span style=\"color: #000000;\">This statement will show you how much of your cache is allocated to single use plans&#8230; this query works in 2005 and 2008; however, the primary solution I&#8217;m describing here (optimize for adhoc workloads) will only work in SQL Server 2008 and higher.<\/span><\/span><\/p>\n<p style=\"padding-left: 30px;\"><strong>IMPORTANT NOTE \/ UPDATE:<\/strong>\u00a0The most important point about this post is that you should be using resources more efficiently by regularly (and programmatically) checking \/ clearing the ad hoc \/ prepared plan cache using DBCC FREESYSTEMCACHE (\u2018SQL Plans\u2019)\u2026 So, this post is really more about\u00a0understanding the problem. Check out this post for more information on these numbers as well as cleaning the plan cache:\u00a0<a style=\"font-weight: inherit; font-style: inherit;\" href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/plan-cache-adhoc-workloads-and-clearing-the-single-use-plan-cache-bloat\/\">Plan cache, adhoc workloads and clearing the single-use plan cache bloat<\/a>.<\/p>\n<p>If you have a lot of your cache going to plans that are only executed once, then it&#8217;s time to clean up the cache and take better advantage of it with plans that are more consistent and more stable. Ideally, this means using more stored procedures and writing these stored procedures effectively for better performance (I&#8217;m currently in the midst of doing this in my <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/category\/optimizing-procedural-code\/\" target=\"_blank\">Optimizing Procedural Code category<\/a>). However, if you absolutely must use a lot of adhoc SQL, then you should consider upgrading and turning on this new option. It&#8217;s an advanced configuration option so you won&#8217;t see it until you &#8220;show advanced options&#8221; and it&#8217;s set using sp_configure. There have been some other really good posts out there on how to use this and what this is so I&#8217;m just going to bring together some great resources for you to read. The most important post to read (and it&#8217;s especially important for those on you on versions of SQL Server 2000 or SQL Server 2005 RTM\/SP1 [er&#8230; why aren&#8217;t you on SP2?]) is that\u00a0plan cache can get out of control. 2005 SP2 and 2008 reduce the total size but there no upper limit (which is again &#8211; a GREAT reason for the addition of &#8220;optimize for adhoc workloads&#8221;). Kalen Delaney talks about how things really work in her SQL Server 2005 SP2 post titled: <a href=\"http:\/\/sqlblog.com\/blogs\/kalen_delaney\/archive\/2007\/11\/04\/did-you-know-sp2-does-not-limit-the-amount-of-plan-cache-you-can-have.aspx\" target=\"_blank\" class=\"broken_link\">Did You Know? SP2 does NOT limit the amount of plan cache you can have<\/a>\u00a0(key word there is LIMIT). Again, SQL Server doesn&#8217;t set an upper limit but it does [drastically] reduce the total size that&#8217;s possible (as of SP2). As for even more details on plan caching, recompilation and SQL Server 2008&#8217;s better cache control &#8211; check out Greg Low&#8217;s (<a href=\"http:\/\/sqlblog.com\/blogs\/greg_low\/default.aspx\" target=\"_blank\" class=\"broken_link\">blog<\/a>|<a href=\"http:\/\/twitter.com\/greglow\" target=\"_blank\" class=\"broken_link\">twitter<\/a>)\u00a0whitepaper titled: <a href=\"http:\/\/msdn.microsoft.com\/Areas\/Epx\/Content\/500.htm?aspxerrorpath=\/en-us\/library\/ee343986.aspx\" target=\"_blank\">Plan Caching in SQL Server 2008<\/a>. Taken STRAIGHT from the first section of the whitepaper:<\/p>\n<blockquote><p><strong>SQL Server 2008 and SQL Server 2005 SP2 <\/strong><br \/>\n* 75% of visible target memory from 0-4GB + <span style=\"text-decoration: underline;\"><strong>10%<\/strong><\/span> of visible target memory from 4Gb-64GB + <strong><span style=\"text-decoration: underline;\">5%<\/span><\/strong> of visible target memory &gt; 64GB<\/p>\n<p><strong>SQL Server 2005 RTM and SQL Server 2005 SP1<br \/>\n<\/strong>* 75% of visible target memory from 0-8GB + <strong><span style=\"text-decoration: underline;\">50%<\/span><\/strong> of visible target memory from 8Gb-64GB + <strong><span style=\"text-decoration: underline;\">25%<\/span><\/strong>\u00a0of visible target memory &gt; 64GB<\/p>\n<p><strong>SQL Server 2000<br \/>\n<\/strong>*\u00a0SQL Server 2000 4GB upper cap on the plan cache<\/p><\/blockquote>\n<p>Finally, lots of additional posts on this topic will give you even more details:<\/p>\n<ul>\n<li>Bob Beauchemin (<a href=\"http:\/\/www.SQLskills.com\/blogs\/bobb\/\" target=\"_blank\" class=\"broken_link\">blog<\/a>) did a simple post on this while it was still in RC0 here: <a href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/performance-features-in-sql-server-2008-rc0-optimize-for-adhoc-workloads\/\">Performance features in SQL Server 2008 RC0 &#8211; Optimize for Adhoc Workloads<\/a>.<\/li>\n<li>Jose Barreto (<a href=\"http:\/\/blogs.technet.com\/b\/josebda\/\" target=\"_blank\">blog<\/a>|<a href=\"http:\/\/twitter.com\/josebarreto\" target=\"_blank\" class=\"broken_link\">twitter<\/a>) did a simple post on this and pulls together a few nice resources as well here: <a href=\"http:\/\/blogs.technet.com\/b\/josebda\/archive\/2009\/03\/19\/optimize-for-ad-hoc-workloads-in-sql-server-2008.aspx\" target=\"_blank\">SQL Server 2008 Optimize for Ad Hoc Workloads<\/a>.<\/li>\n<li>Kalen Delaney wrote an article on <a href=\"http:\/\/www.sqlmag.com\" target=\"_blank\">SQL Server Magazine<\/a> (<strong>note:<\/strong> this article requires a SQL Server Magazine subscription) titled: <a href=\"http:\/\/www.sqlmag.com\/article\/quering\/managing-plan-cache-size\" target=\"_blank\">Managing Plan Cache Size<\/a> and after the article, she posted a Q&amp;A on the questions she received from the article in a blog post titled: <a href=\"http:\/\/sqlblog.com\/blogs\/kalen_delaney\/archive\/2007\/12\/15\/QA-on-my-plan-cache-sizing-article-in-sql-server-magazine.aspx\" target=\"_blank\" class=\"broken_link\">Geek City: Q &amp; A on my Plan Cache Sizing article in SQL Server Magazine<\/a>.<\/li>\n<li>Books online topics:\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/Areas\/Epx\/Content\/500.htm?aspxerrorpath=\/en-us\/library\/cc645587.aspx\" target=\"_blank\">Optimize for adhoc workloads<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/Areas\/Epx\/Content\/500.htm?aspxerrorpath=\/en-us\/library\/ms189631.aspx\" target=\"_blank\">Setting Server Configuration Options<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/Areas\/Epx\/Content\/500.htm?aspxerrorpath=\/en-us\/library\/ms188265.aspx\" target=\"_blank\">Show advanced options<\/a><\/li>\n<\/ul>\n<\/li>\n<li>Glenn Berry (<a href=\"https:\/\/profile.live.com\/cid-45041418eccaa960\/whatsnew\/\" target=\"_blank\" class=\"broken_link\">blog<\/a>|<a href=\"http:\/\/twitter.com\/GlennAlanBerry\" target=\"_blank\" class=\"broken_link\">twitter<\/a>) has a post in his DMV A Day series on looking at the plan cache and seeing which statements are causing you grief. Once you see how much cache is being wasted with your single use plans (with the above query) &#8211; it&#8217;s time to start looking and seeing what they are. His blog post is titled: <a href=\"http:\/\/sqlserverperformance.wordpress.com\/2010\/04\/15\/a-dmv-a-day-%e2%80%93-day-16\/\" target=\"_blank\">A DMV A Day &#8211; Day 16<\/a>.<\/li>\n<\/ul>\n<p><strong>NOTE: Be sure to read the subsequent post on these numbers and how to deal with it when your cache is bloated!<\/strong><\/p>\n<p style=\"padding-left: 30px;\">Next couple of posts: (read this first) <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/plan-cache-adhoc-workloads-and-clearing-the-single-use-plan-cache-bloat\/\"><span style=\"color: #333333;\">Plan cache, adhoc workloads and clearing the single-use plan cache bloat<\/span><\/a>\u00a0and then read this: <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/clearing-the-cache-are-there-other-options\/\"><span style=\"color: #333333;\">Clearing the cache \u2013 are there other options?<\/span><\/a><\/p>\n<p><span style=\"color: #0000ff;\"><span style=\"color: #000000;\">Thanks for reading!<br \/>\nkt<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I mentioned that servers receiving a lot of dynamic constructs (typically those generated by client SQL apps\/ORMs\/LINQ, etc.) can start to consume too much plan cache and have problems with &#8220;single-use plans&#8221; in my last post titled: Statement execution and why you should use stored procedures. I also mentioned that SQL Server 2008 has an [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,53,66],"tags":[],"class_list":["post-507","post","type-post","status-publish","format-standard","hentry","category-optimizing-procedural-code","category-plan-cache","category-sql-server-2008"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=507"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/507\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}