SQL SERVER – AdHoc Queries and Optimize for Adhoc Workloads

Optimize for AdHoc Queries reduces the first-use plan-cache footprint of ad hoc batches. A customer variable-query workload prompted testing.

Small retained shaping stubs remain distinct from a full reusable mold and working press.

Historical SSMS setting for optimize for ad hoc workloads.
Historical SSMS setting for optimize for ad hoc workloads.
SELECT name,value,value_in_use FROM sys.configurations
WHERE name=N'optimize for ad hoc workloads';
SELECT objtype,cacheobjtype,COUNT_BIG(*) AS EntryCount,
       SUM(CONVERT(bigint,size_in_bytes))/1048576.0 AS CacheMB
FROM sys.dm_exec_cached_plans WHERE usecounts=1
GROUP BY objtype,cacheobjtype ORDER BY CacheMB DESC;

When enabled, SQL Server can retain a compiled-plan stub after the first use. The batch still compiles and executes. A later qualifying use can create a full cached plan. The option does not eliminate compilation.

The customer CPU observation fell from about 100 percent to 60 percent after the change. That was a specific before/after result. It is not a guaranteed reduction. Measure memory, compilation and reuse separately.

Inspect single-use entries and application parameterization. Changing literals differs from changing query structure. Assess parameter-sensitive behavior and retain a baseline before configuration changes. This read-only inventory does not alter the setting or clear plans.

Reference: Ad hoc plan stubs and limitations.

Related reading

A plan stub is not elimination of compilation, it is a smaller first-use cache entry.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Ad Hoc Query, SQL CPU, SQL Scripts, SQL Server, SQL Server Configuration
Previous Post
Handling Duplicate Key Errors 2627 and 2601 in a CATCH Block
Next Post
SQL SERVER – Change Password at the First Login

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.