You are on page 1of 2

Kimberly & Paul: SQL Server Questions Answered

Should I optimize for ad hoc workloads?

Question: I think I heard you recommend broadly the new SQL Server 2008 configuration “optimize for ad
hoc workloads” and I want to know if this is true? Should I turn this option on? Why?

Answer: Yes! In general, I recommend that this option be turned on for all of my servers/customers.
The original idea behind the option was to reduce plan cache bloat nadimanje caused by ad hoc workloads
but even if you don’t have a lot of intentionally ad hoc workloads you might still save cache and reduce
bloat of dynamically executed strings and other internal code.

What this option does is prevent SQL Server from placing a plan in cache on the first execution of a
statement. Instead, SQL Server will only create a query hash (and place this in cache). Once a statement
executes a second time, then SQL Server will place the plan in cache. The reason this is a problem is that
query plans can be quite large – especially when compared to the query hash. A simple statement
executed against a table with only a few indexes might still be 24K in size whereas more complicated
statements against more complicated schemas can actually be megabytes in size. A query hash is usually
measured in bytes or maybe 1K if it’s really complicated. If a statement ends up only executing once then
you save by not placing a large plan in cache that won’t ever get reused. The downside is that the second
execution also has to generate the plan to place it in cache but if the statement is executed often then
there’s only 1 execution that suffers. If the statements are not executed often then your plan cache will
not be wasted and can instead be used by procedures and plans that do get reused.

I’ve written a few detailed blog posts on this subject and it’s something that you might want to investigate
further. However, I have not yet found an environment that didn’t benefit from having this option set. The
bad news is that it was a new option in SQL Server 2008 and therefore only exists in 2008 and higher.

Što je ova opcija čini se spriječiti SQL Server stavljaju plan u predmemoriju na prvi izvršenje izjave. Umjesto toga, SQL
Server će se samo stvoriti upit hash (i mjesto to cache). Nakon izjava izvršava drugi put, onda SQL Server će se plan u
cache. Razlog je to problem je taj upit planovi mogu biti vrlo velik - osobito u odnosu na upit mljeveno
meso.Jednostavna izjava izvršiti prema tablici sa samo nekoliko indeksi još uvijek može biti 24K u veličini, dok
složenije izjave protiv više komplicirana shema zapravo može biti megabajta u veličini.Upit hash se obično mjeri u
bajtovima ili možda 1K ako je stvarno komplicirano. Ako izjava završava tek izvršenja jednom tada spasiti ne
stavljanje velikog plana u priručnu memoriju koji se neće nikada dobiti ponovno. The downside je da je drugi
izvršenje također generirati plan stavite ga u predmemoriju, ali ako je izjava izvršava često onda postoji samo 1
izvršenja koja pati. Ako izjave nisu izvršena često onda vaš plan predmemorije neće biti uzaludan i može se koristiti
umjesto za postupke i planove koji ne bi ponovno koristiti.
Ja sam napisao par detaljni blogu na tu temu i to je nešto što možda želite dodatno istražiti. Međutim, ja još nisu
pronašli okoliš koji nisu imali koristi od toga ovu opciju set.Loša vijest je da je nova opcija u SQL Server 2008 i stoga
postoji samo u 2008 i više.

NOTE: This is an advanced configuration option and so you need configure your server to “show advanced
options” first. Here are the commands necessary to turn this on at the server-level:

sp_configure 'show advanced options', 1


go
reconfigure
go
sp_configure 'optimize for ad hoc workloads', 1
go
reconfigure
go

If you want to learn more about how much of your cache is being used by “single-use plans” then check
out this post: Plan cache and optimizing for adhoc workloads as well as Plan cache, adhoc workloads and
clearing the single-use plan cache bloat.

If you want to see what’s in cache and get more insight into plan size and query hash then check out this
post: Clearing the cache - are there other options?.

Thanks for reading and happy holidays!


kt

You might also like