About the SQL Server Plan Cache
The SQL Server plan cache is built on top of the caching infrastructure provided by the SQL OS. This provides objects called cache stores, which can be used to cache all kinds of objects. The plan cache contains several different cache stores used for different types of objects.
select name, entries_count, pages_kb from sys.dm_os_memory_cache_counters where [name] in ( 'object plans' , 'sql plans' , 'extended stored procedures' )
name entries_count pages_kbObject Plans 54 12312SQL Plans 48 2904Extended Stored Procedures 4 48
Find Performance Issues Caused by Long Lookup Times
select * from sys.dm_os_memory_cache_hash_tables where type in ( 'cachestore_objcp' , 'cachestore_sqlcp' , 'cacchestore_phdr' , 'cachestore_xproc' )
select bucketid, count(*) as entries_in_bucket from sys.dm_exec_cached_plans group by bucketid order by 2 desc
select * from sys.dm_exec_cached_plans where bucketid = 236
If the plans you find within the same bucket are all variations on the same query, then try to get better plan reuse through parameterization. If the queries are already quite different, and there is no commonality that would allow parameterization, then the solution is to rewrite the queries to be dramatically different, enabling them to be stored in emptier buckets.
select query_plan_hash,count(*) as occurrences from sys.dm_exec_query_stats group by query_plan_hash having count(*) > 1
Examine Objects Stored in the SQL Server Plan Cache
- Algebrizer trees are the output of the algebrizer, although only the algebrizer trees for views, defaults, and constraints are cached.
- Compiled plans are the objects you will be most interested in. This is where the query plan is cached.
- Cursor execution contexts are used to track the execution state when a cursor is executing, and are similar to the next item.
- Execution contexts track the context of an individual compiled plan.
select cacheobjtype, objtype, COUNT (*) from sys.dm_exec_cached_plans group by cacheobjtype, objtype order by cacheobjtype, objtype
CACHEOBJTYPE OBJTYPE (NO COLUMN NAME) Compiled Plan Adhoc 43 Compiled Plan Prepared 20 Compiled Plan Proc 54 Extended Proc Proc 4 Parse Tree Check 2 Parse Tree UsrTab 1 Parse Tree View 64
-- Run this to empty the cache -- WARNING !!! DO NOT TRY THIS ON A PRODUCTION SYSTEM !!! dbcc freeproccache
select cacheobjtype, objtype, COUNT (*) from sys.dm_exec_cached_plans group by cacheobjtype, objtype order by cacheobjtype, objtype
CACHEOBJTYPE OBJTYPE (NO COLUMN NAME) Compiled Plan Adhoc 5 Compiled Plan Prepared 1 Compiled Plan Proc 11 Extended Proc Proc 1 Parse Tree View 10
select lastname, COUNT (*) from Person.Person_test group by lastname order by 2 desc
-- Check that we got additional objects into the cache select cacheobjtype, objtype, COUNT (*) from sys.dm_exec_cached_plans group by cacheobjtype, objtype order by cacheobjtype, objtype
CACHEOBJTYPE OBJTYPE (NO COLUMN NAME) Compiled Plan Adhoc 9 Compiled Plan Prepared 2 Compiled Plan Proc 14 Extended Proc Proc 2 Parse Tree View 13
select p.refcounts, p.usecounts, p.plan_handle, s.text from sys.dm_exec_cached_plans as p cross apply sys.dm_exec_sql_text (p.plan_handle) as s where p.cacheobjtype = 'compiled plan' and p.objtype = 'adhoc' order by p.usecounts desc
select * from sys.dm_exec_cached_plan_dependent_objects (0x06000F005163130CB880EE0D000000000000000000000000)
USECOUNTS MEMORY_OBJECT_ADDRESS CACHEOBJTYPE 1 0x0DF8A038 Executable Plan
Examine SQL Server Plan Attributes
select * from sys.dm_exec_plan_attributes (0x06000F00C080471DB8E06914000000000000000000000000)
ATTRIBUTE VALUE IS_CACHE_KEY set_options 135419 1 objectid 491225280 1 dbid 15 1 language_id 0 1 date_format 1 1 date_first 7 1 compat_level 100 1 sql_handle 0x02000000C080471DB475BDA81DA97B1C6F2EEA51417711E8 0
Monitoring Compilation and Recompilation
Leave a Reply