I’m trying to understand the plan cache content of a sql server.
so my questions are:
1. What is the difference between ad hoc and prepared plans?
2.What should I know about it when trying to optimize the sql server plan cache?
I’m trying to understand the plan cache content of a sql server.
so my questions are:
1. What is the difference between ad hoc and prepared plans?
2.What should I know about it when trying to optimize the sql server plan cache?
Copyright © 2021 Jogjafile Inc.
Adhoc query:
Prepared query:
Queries which substitute place holders in place of actual values are called Prepared statements.
Some Examples:
One more Example taken from wikipedia:
There are whitepapers written about how to optimize plan cache.so i will try to keep it little..
Normally when a query is executed against SQL ,SQL compiles the plan and stores it in the plan cache .This plan cache is memory taken from buffer pool and different versions have different restrictions on how much amount of memory will be used
You know that memory is a precious resource and no amount of Hardware will be enough if you have leaks..
Assume you submit queries only once or twice and you tend to submit like this queries a lot.SQL will store the plan of this queries in plan cache which generally bloats PlanCache which is bad
There are different DMVS which will help you in digging through plan cache..
Query to find different type of objects are in plan cache :
Adhoc,prepared queries which are bloating plancache and are used only once:
To Delete statements which are bloating plan cache:
The above should give you an idea on where to start ,below are the must read topics and references :
1.http://www.sqlskills.com/blogs/kimberly/category/plan-cache/
2.http://sqlblog.com/blogs/kalen_delaney/archive/2007/11/04/did-you-know-sp2-does-not-limit-the-amount-of-plan-cache-you-can-have.aspx
3.https://technet.microsoft.com/en-us/library/dd672789(v=sql.100).aspx
4.Must read article By SQLCAT on issues customer faced while using Prepare Statements
In referenced articles above,kimberely suggests to Enable Optimize for Adhoc workloads option,but i suggest test it first.here is an interesting thread on DBA.SE