简体   繁体   中英

SQL Server exec plans for ad hoc queries filling plan cache

We have a database which is updated every hour. Changes are made by an application which uses DELETE/INSERT for each row that is updated or inserted. SQL Server is 2008 R2 Standard Edition.

The query execution plans for the ad hoc queries are pushing out other (more important, larger) execution plans which is affecting reporting on that database and eating into the data cache.

After looking at the plan cache we have found that (as expected) SQL is generating a new plan for each query.

My question is, would creating a plan guide stop SQL generating a new plan for each update?

I have seen in the cache that for an update to a float column for example, SQL parameterizes it to a numeric of varying length each time, which means every parameterized query is different. If I create a plan guide with the parameterized version of the query, specifying the correct data types for each column, will this force SQL to use this plan?

I am aware that using stored procedures will solve this, but for other reasons that isn't an option.

Thanks.

Have you tried enabling 'optimize for ad hoc workloads' ?

The optimize for ad hoc workloads option is used to improve the efficiency of the plan cache for workloads that contain many single use ad hoc batches. When this option is set to 1, the Database Engine stores a small compiled plan stub in the plan cache when a batch is compiled for the first time, instead of the full compiled plan. This helps to relieve memory pressure by not allowing the plan cache to become filled with compiled plans that are not reused.

Setting the optimize for ad hoc workloads to 1 affects only new plans; plans that are already in the plan cache are unaffected.

Ref .

Plan cache and optimizing for adhoc workloads

To enable this feature, use these commands (I would test in a test system first):

SP_CONFIGURE 'show advanced options',1
RECONFIGURE
GO

SP_CONFIGURE 'optimize for ad hoc workloads',1
RECONFIGURE
GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM