SQL Server Health Check

“Optimize For Ad Hoc Workloads” Option And SQL Performance

Updated
4 min read
Written by
Mark Varnas

What is the “Optimize for Ad Hoc Workloads” option?

When you write a query in SQL Server, the query optimizer needs to create a “Plan” of how it is going to execute that query.

Usually, SQL Servers save that plan in the plan cache to be reused again at a later date.

Optimize for Ad Hoc Workloads is a server-level option that changes the behavior of the plan cache for single-use queries.

Turning this on, SQL Server will not store the entire plan when the query is executed the first time, and it will store a plan stub instead, reducing what we call plan bloat.

This reduces the amount of size one-time queries take up in the cache allowing more reusable plans to remain in the cache for more extended periods.

That’s great because some of those plans won’t ever get reused.

Enable or not – “Optimize for Ad hoc Workloads”?

If your workload indeed involves lots of ad hoc queries (different statements on every execution), enabling Optimize for Ad hoc Workloads may be your best option.

You can check the size of your ad hoc plan and total plan cache, using the query below:

SELECT AdHoc_Plan_MB
	,Total_Cache_MB
	,AdHoc_Plan_MB * 100.0 / Total_Cache_MB AS 'AdHoc plan%'
FROM (
	SELECT SUM(CASE 
				WHEN objtype = 'adhoc'
					THEN size_in_bytes
				ELSE 0
				END) / 1048576.0 AdHoc_Plan_MB
		,SUM(size_in_bytes) / 1048576.0 Total_Cache_MB
	FROM sys.dm_exec_cached_plans
	) P

If your ad hoc plan cache is 15-30% of the total Plan Cache, enabling the Optimize for Ad Hoc Workloads may be beneficial to your SQL Server.

However, when there are unparameterized queries, enabling Optimize for Ad hoc Workloads certainly will not help, those original queries will still be getting generated.

The primary issue with forced parameterization is what we call parameter sniffing.

Parameterization is not recommended as a default setting. However, that doesn’t mean it isn’t useful. It is always better for you to find the root cause than just treating the symptoms.

How to turn “Optimize for Ad Hoc Workloads option” ON?

You can enable the Optimize for Ad Hoc Workloads using SSMS, by following these steps:

  1. In the Object Explorer, right-click on the Server.
  2. Go to the Advanced page and change the property Optimize for ad hoc Workloads to true.

Also, you may implement the change, using T-SQL, by running the following query:

SP_CONFIGURE 'Show Advanced Options'
	,1
GO

RECONFIGURE
GO

SP_CONFIGURE 'optimize for ad hoc workloads'
	,1
GO

RECONFIGURE
GO

Note: Enabling “optimize for ad hoc workloads” affects only new plans. The plans that are already in the plan cache are unaffected.

More information

Speak with a SQL Expert

In just 30 minutes, we will show you how we can eliminate your SQL Server headaches and provide 
operational peace of mind

Article by
Mark Varnas
Founder | CEO | SQL Veteran
Hey, I'm Mark, one of the guys behind Red9. I make a living performance tuning SQL Servers and making them more stable.

Leave a Comment

Discover More

SQL Server Health Check SQL Server Migrations & Upgrades SQL Server Performance Tuning SQL Server Security SQL Server Tips

Discover what clients are saying about Red9

Red9 has incredible expertise both in SQL migration and performance tuning.

The biggest benefit has been performance gains and tuning associated with migrating to AWS and a newer version of SQL Server with Always On clustering. Red9 was integral to this process. The deep knowledge of MSSQL and combined experience of Red9 have been a huge asset during a difficult migration. Red9 found inefficient indexes and performance bottlenecks that improved latency by over 400%.

Rich Staats 5 stars
Rich Staats
Cloud Engineer
MetalToad

Always willing to go an extra mile

Working with Red9 DBAs has been a pleasure. They are great team players and have an expert knowledge of SQL Server database administration. And are always willing to go the extra mile to get the project done.
5 stars
Evelyn A.
Sr. Database Administrator

Boosts server health and efficiency for enhanced customer satisfaction

Since adding Red9 to the reporting and DataWarehousing team, Red9 has done a good job coming up to speed on our environments and helping ensure we continue to meet our customer's needs. Red9 has taken ownership of our servers ensuring they remain healthy by monitoring and tuning inefficient queries.
5 stars
Andrew F.
Datawarehousing Manager
See more testimonials