// sql server · memory

optimize for ad hoc workloads

Cache only a small plan stub the first time an ad hoc batch runs.

Default
0 (off)
Range
0 or 1
Restart needed
No
Default checked on
SQL Server 2025

What it does

The full plan is cached on the second execution. This keeps single-use plans from filling the plan cache on applications that send unparameterized SQL.

Default value

0 (off). 0 (SQL Server 2025). From SQL Server 2019 a database scoped OPTIMIZE_FOR_AD_HOC_WORKLOADS option does the same for one database.

Check the current value

SELECT name, value, value_in_use, minimum, maximum, is_dynamic, is_advanced
FROM sys.configurations
WHERE name = 'optimize for ad hoc workloads';

-- EXEC sp_configure 'optimize for ad hoc workloads'; also works, but only while show advanced options is 1;
-- otherwise it fails with Msg 15123 because this is an advanced option.

Change it

EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'optimize for ad hoc workloads', 1;
RECONFIGURE;

No restart: it takes effect when RECONFIGURE runs (sys.configurations.is_dynamic = 1). It is an advanced option, so show advanced options must be on to change it.

When to change it

When sys.dm_exec_cached_plans shows a large share of single-use ad hoc plans. Turning it on is low risk for most workloads.

What can go wrong

The first execution of each batch always compiles without a cached plan, and you lose that first plan when diagnosing.

Questions about optimize for ad hoc workloads

What is the default value of optimize for ad hoc workloads in SQL Server?

0 (off). 0 (SQL Server 2025). From SQL Server 2019 a database scoped OPTIMIZE_FOR_AD_HOC_WORKLOADS option does the same for one database.

Does changing optimize for ad hoc workloads require a restart?

No. No restart: it takes effect when RECONFIGURE runs (sys.configurations.is_dynamic = 1). It is an advanced option, so show advanced options must be on to change it.

When should I change optimize for ad hoc workloads?

When sys.dm_exec_cached_plans shows a large share of single-use ad hoc plans. Turning it on is low risk for most workloads.

Back to all SQL Server settings, or check the SQL Server manual for every option.

// one client for every engine

Check optimize for ad hoc workloads on your own server

SQLly runs the query above against SQL Server and the other engines in this reference, from one desktop app on macOS, Windows and Linux.