![]() |
|
|
By: Tim Ford | Read Comments (2) | Print Tim is a Senior Database Administrator and SQL Server MVP. He has been working with SQL Server since 1999. Related Tips: More |
|
Problem
A cached query plan will not be used if the query is different than the original query used to generate the cached plan - makes sense - but what if the query is identical? Well even then the query optimizer may not used the cached query plan. Why? Because all the session settings for the current query request must match those from the first time the query plan was generated and cached. So the question is how can you determine what settings are associated with the cached plan? The answer is in the SQL Server Dynamic Management Objects.
Solution
I've spoken quite a bit as of late on the DMOs. These views and functions constructs exist as an abstraction layer for the system tables that are now protected inside of the resource database. The DMOs that accomplish the goal we're looking at examining today are the following:
Let's look at the query that puts all of this together:
SELECT DEQP.query_plan, DEPA.attribute, DEPA.value |

You may wonder how to determine the plan handle you're interested in querying against. There are many options and in my case I hard-coded the value based upon a specific value I returned from a targeted query against sys.dm_exec_requests for a query that was behaving poorly. The nice thing is that the DMV structure is so extensible. I could easily return results like above for a specific user (for example) by using the following query:
SELECT SDEQP.query_plan, SDEPA.attribute, SDEPA.value |
The join structures are quite straight-forward and with the advent of IntelliSense technology in SSMS 2008 (or by use of SQL Prompt for example, which provides the same functionality) the joins usually present themselves. The results in this case are the same, but there is no need for me to do the initial leg work of identifying the exact plan_handle first. You can tailor the query to fit your needs going forth on your own.
Now that you know what session settings are associated with the cached plan, you structure the session state to match the cached plan and take advantage of performance gains that come with plan re-use.
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |