We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

cached query plans

Medium Priority
336 Views
Last Modified: 2012-08-13
Is there any way you can work out how long cached query plans will be cached for  (dm_exec_cached_plans)?
Comment
Watch Question

SQL Server DBA & Architect, EE Solution Guide
CERTIFIED EXPERT
Awarded 2009
Distinguished Expert 2019
Commented:
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..

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mark WillsTopic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
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




Mark WillsTopic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Might want to combine this question with your other question about a trigger for a more complete answer to this...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.