Every batch (T-SQL, SP etc) when ran creates execution plan which is stored in system for re-use. Due to this reason large number of query plans are stored in system. However, there are plenty of plans which are only used once and have never re-used again. One time ran batch plans wastes memory and resources.
SQL Server 2008 has feature of optimizing ad hoc workloads. Before we move to it, let us understand the behavior of SQL Server without optimizing ad hoc workload.
Please run following script for testing. Make sure to not to run whole batch together. Just run each command separately to really see the effect of subject of article.
Download complete script of this article here.
/* Test 0 */
/* Clean Cache and Buffers */
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
GO
USE AdventureWorks
GO
/* Run Adhoc Query First Time */
SELECT * FROM HumanResources.Shift
GO
/* Check if Adhoc query is cached.
It will return one result */
SELECT usecounts, cacheobjtype, objtype, TEXT
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE usecounts > 0 AND
TEXT LIKE '%SELECT * FROM HumanResources.Shift%'
ORDER BY usecounts DESC;
GO
Now let us check result of this script. It is clear from result that when we ran query once it cached its plan in memory. If we never run this again in future or if we have just ran as part of building longer query the cache plan of this query is just waste of memory.

Let us now enable the option of optimizing ad hoc workload. This feature is available in all the versions of SQL Server 2008.
sp_CONFIGURE 'show advanced options',1
RECONFIGURE
GO
sp_CONFIGURE ‘optimize for ad hoc workloads’,1
RECONFIGURE
GO
We will now run the code for Test 1 which is almost same as Test 0. Make sure to clean the cache and buffer before running the query to create real life scenario of live case.
/* Test 1 */
/* Clean Cache and Buffers */
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
GO
USE AdventureWorks
GO
/* Run Adhoc Query First Time */
SELECT * FROM HumanResources.Shift
GO
/* Check if Adhoc query is cached.
It will not return any result */
SELECT usecounts, cacheobjtype, objtype, TEXT
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE usecounts > 0 AND
TEXT LIKE 'SELECT * FROM HumanResources.Shift%'
ORDER BY usecounts DESC;
GO
We can clear see now as we have advance option enabled we do not have query cache planed stored in database.

We are interested to know now that if we run the batch more than 1 time it will cache its execution plan. With advance option of optimizing ad hoc workload.
/* Test 2 */
/* Clean Cache and Buffers */
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
GO
/* Run Adhoc Query two Time */
SELECT * FROM HumanResources.Shift
GO 5
/* Check if Adhoc query is cached.
It will return result with Adhoc Query ran two times*/
SELECT usecounts, cacheobjtype, objtype, TEXT
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_sql_text(plan_handle)
WHERE usecounts > 0 AND
TEXT LIKE '%SELECT * FROM HumanResources.Shift%'
ORDER BY usecounts DESC;
GO
From our image it is quite clear that when the batch is ran for more than 1 time it caches its execution plan. This is generic behavior with or without turning on advance option.

This may be very simple to see from the top but if you are using SQL Server 2008 and have millions of ad hoc query running every day you wil realize how important this feature is. This feature improves performance by relieving memory pressure by not storing the single time used compiled plans.
Reference : Pinal Dave (http://blog.sqlauthority.com)




Hi Pinal,
Really very good digging. thanks for sharing this useful tip with us.
Ritesh Shah
great article. thank you!
Hi Pinal,
Is it so that the following code runs only in SQL Server 2008, because when I am running this on SQL Express 2005 it giving error “Msg 15123, Level 16, State 1, Procedure sp_configure, Line 51
The configuration option ‘optimize for ad hoc workloads’ does not exist, or it may be an advanced option.”
sp_CONFIGURE ‘optimize for ad hoc workloads’,0
RECONFIGURE
GO
I checked in sys.configurations table under master db and there is no option such as ‘optimize for ad hoc workloads’ available over there.
Thanks
Pradeep Nair
Pradeep,
It is possible to run in SQL Server 2005 and it works fine. You need to turn on advance option first as I have suggested in the blog post.
sp_CONFIGURE ’show advanced options’,1
RECONFIGURE
GO
sp_CONFIGURE ‘optimize for ad hoc workloads’,0
RECONFIGURE
GO
Regards,
Pinal
Hi Pinal,
Thanks for the response. I am executing both the command together and separately. But I am still getting the same error. The first command “sp_CONFIGURE ’show advanced options’, 1″ its running successfully and giving a response as “Configuration option ’show advanced options’ changed from 1 to 1. Run the RECONFIGURE statement to install.”. But the second script is throwing the same error which I mentioned in my previous comment. Thanks.
Regards
Pradeep Nair
Hm.. interesting. May be because it is SQL Server Express. I will have to test with my Express box. I Will do so and will let you know.
Regards,
Pinal
Works in 2008
I just tested it in SQL 2005 Standard 64 bit SP2
There is no “optimize for ad hoc workloads” option, if you just run SP_CONFIGURE to display all options
sp_CONFIGURE ’show advanced options’,1
RECONFIGURE
GO
sp_CONFIGURE ‘optimize for ad hoc workloads’,1
RECONFIGURE
GO
gives me
Msg 15123, Level 16, State 1, Procedure sp_configure, Line 51
The configuration option ‘optimize for ad hoc workloads’ does not exist, or it may be an advanced option.
Thanks Jerry Hung and Pinal…I thought so, because when I ran a select statement on sys.configurations table under master database, the option was not there.
And, Pinal I read your latest blog entry which you wrote today(26 Mar 08 – SQL Server – Fix : Error : Msg 15123….). Thanks once again.
Regards,
Pradeep Nair
Hi ,
Nice article.
I have a stored procedure which is running fine in sql server 2000. But when i upgrade it to sql server 2008 it running for long time and never stopped execution. I tried to stop and rerun but no use.
Can you please let me know what action to be taken on this to get it run again. FYI the execution time for this SP in 2000 is about 60 mins.