cached query plans

Is there any way you can work out how long cached query plans will be cached for  (dm_exec_cached_plans)?
david_32Asked:
Who is Participating?
 
Raja Jegan RConnect With a Mentor SQL Server DBA & ArchitectCommented:
SQL server will create execution plans for all queries and stored procedures it executes. Query plans are detailed steps as to how to get the results for the query specified. So this would include which tables to query, which indexes to use, how to join or merge the results, and so on. By default this will be done when you execute or compile a query and hence It would be added in your cached plans.

You have the option to remove specific query plans from the server using any of the following commands.

Following operations will also flush the entire plan cache and new batches will require new plans.

    * Detaching any database
    * Upgrading any database to compatibility level 90 or above on SQL Server 2005 or above
    * Running ALTER DATABASE ... MODIFY FILEGROUP command for any database
    * Running ALTER DATABASE ... COLLATE command for modifying collation of any database

Altering a database with any of the following commands will remove all plans cached for that specific database.

    * ALTER DATABASE ..... MODIFY NAME
    * ALTER DATABASE ..... SET ONLINE
    * ALTER DATABASE ..... SET OFFLINE
    * ALTER DATABASE ..... SET EMERGENCY

Following operations will also remove cached plans for a specific database.

    * Dropping a database
    * Database is auto closed

In syscacheobjects table, you have a column named usecounts. Based on that value, less used plans will be thrown out if some new plans needs to be included in that table.

Hope I have answered you..
0
 
Mark WillsConnect With a Mentor Topic AdvisorCommented:
Short answer, no.

Long answer, well (you saw my last one)....... you probably want to read up on : http://www.microsoft.com/technet/prodtechnol/sql/2005/qrystats.mspx.

There are some good technical guides and manuals on MS Technet for performance analysis.

Have a look at lowest reuse means it gets swapped out quickest (or least used) - need to keep a watch on that usecount - adapted from one of those techo manuals.

SELECT TOP 50
        qs.sql_handle
        ,qs.plan_handle
        ,cp.cacheobjtype
        ,cp.usecounts
        ,cp.size_in_bytes  
        ,qs.statement_start_offset
        ,qs.statement_end_offset
        ,qt.dbid
        ,qt.objectid
        ,qt.text
        ,SUBSTRING(qt.text,qs.statement_start_offset/2,
             (case when qs.statement_end_offset = -1
            then len(convert(nvarchar(max), qt.text)) * 2
            else qs.statement_end_offset end -qs.statement_start_offset)/2)
        as statement
FROM sys.dm_exec_query_stats qs
cross apply sys.dm_exec_sql_text(qs.sql_handle) as qt
inner join sys.dm_exec_cached_plans as cp on qs.plan_handle=cp.plan_handle
where cp.plan_handle=qs.plan_handle
and qt.dbid = db_id('my_database')    ----- put your database name
ORDER BY [Usecounts] asc




0
 
Mark WillsTopic AdvisorCommented:
Might want to combine this question with your other question about a trigger for a more complete answer to this...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.