Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Clear out the procedure cache

From Wiki

Jump to: navigation, search

If you have a lot of ad hoc queries with changing values or dynamic SQL that doesn't use sp_executesql with proper parameters then your procedure cache can become bloated with plans that are only used once and less RAM will be available for data. If this is the case then you can empty the procedure cash by executing the following command

  1. DBCC FREEPROCCACHE

Before doing that make sure that you are suffering from procedure cache bloat because all your plans will be gone To see how big you procedure cache is you can execute the following command on SQL Server 2000, 2005 and 2008

  1. DBCC PROCCACHE


DBCC FREEPROCCACHE returns the following info

Column nameDescription
num proc buffsNumber of possible stored procedures that could be in the procedure cache.
num proc buffs usedNumber of cache slots holding stored procedures.
num proc buffs activeNumber of cache slots holding stored procedures that are currently executing.
proc cache sizeTotal size of the procedure cache
proc cache usedAmount of the procedure cache holding stored procedures
proc cache activeAmount of the procedure cache holding stored procedures that are currently executing


On SQL Server 2005 and 2008 you can use the sys.dm_exec_cached_plans dynamic management view to see what is in the procedure cache

  1. SELECT * FROM sys.dm_exec_cached_plans

This dmv returns the following information

Column name Data typeDescription
bucketidint

ID of the hash bucket in which the entry is cached. The value indicates a range from 0 through the hash table size for the type of cache. For the SQL Plans and Object Plans caches, the hash table size can be up to 10007 on 32-bit systems and up to 40009 on 64-bit systems. For the Bound Trees cache, the hash table size can be up to 1009 on 32-bit systems and up to 4001 on 64-bit systems. For the Extended Stored Procedures cache the hash table size can be up to 127 on 32-bit and 64-bit systems. For more information about cache types and hash tables, see sys.dm_os_memory_cache_hash_tables (Transact-SQL).

refcountsint

Number of cache objects that are referencing this cache object. Refcounts must be at least 1 for an entry to be in the cache.

usecountsint

Number of times this cache object has been used since its inception.

size_in_bytesint

Number of bytes consumed by the cache object.

memory_object_addressvarbinary(8)

Memory address of the cached entry. This value can be used with sys.dm_os_memory_objects to get the memory breakdown of the cached plan and with sys.dm_os_memory_cache_entries_entries to obtain the cost of caching the entry.

cacheobjtypenvarchar(34)

Type of object in the cache. The value can be one of the following: Compiled Plan Compiled Plan Stub Parse Tree Extended Proc CLR Compiled Func CLR Compiled Proc

objtypenvarchar(16)

Type of object. The value can be one of the following: Value Description Proc Stored procedure Prepared Prepared statement Adhoc Ad hoc query1 ReplProc Replication-filter-procedure Trigger Trigger View View Default Default UsrTab User table SysTab System table Check CHECK constraint Rule Rule

plan_handlevarbinary(64)

Identifier for the in-memory plan. This identifier is transient and remains constant only while the plan remains in the cache. This value may be used with the following dynamic management functions: sys.dm_exec_sql_text sys.dm_exec_query_plan sys.dm_exec_plan_attributes

pool_idint

The ID of the resource pool against which this plan memory usage is accounted for.



If you need more detail about the plans then you can use the following query, this query will also return the text of the plan itself

  1. SELECT q.TEXT,cp.usecounts,cp.objtype,p.*,
  2. q.*,
  3. cp.plan_handle
  4. FROM
  5. sys.dm_exec_cached_plans cp
  6. cross apply sys.dm_exec_query_plan(cp.plan_handle) p
  7. cross apply sys.dm_exec_sql_text(cp.plan_handle) AS q


If you want to know how to write code that won't bloat the procedure cache read the following blog post: Changing exec to sp_executesql doesn't provide any benefit if you are not using parameters correctly

Contributed by: --SQLDenis 20:01, 11 June 2009 (GMT)

Part of SQL Server Admin Hacks

646 Rating: 3.8/5 (4 votes cast)