.NET Tips and Tricks

Blog archive

Speeding Up SQL Server: Planning for One-Time Queries

As I discussed in an earlier column, SQL Server keeps a plan cached for each query it sees (assuming the query requires planning in the first place, of course). That's great for speeding up processing the next time that query shows up because the plan for the query can simply be pulled from the cache.

However, there are any number of queries in any application that SQL Server may never see again (at least in, for example, the next 24 hours). The plans for these "one-time" queries are taking up space in the cache even though they might never be used again. The cache manager will recover space if necessary by keeping track of plans with low planning costs and discarding those plans as the cache runs out of space. However, that doesn't address the space used by those "one-time" plans.

You can help the cache manager out by turning on (or having your DBA turn on) the option to optimize SQL Server for ad-hoc workloads. With this turned on, the first time a plan is created, the identifiers for storing the plan in the cache are created, along with a stub for the plan. The plan itself, however, isn't added to the cache. It's only upon the second occurrence of the query (or one like it) that the plan is added to the cache. The basic assumption here is that if a query appears twice, it will appear many times. The only cost is that the plan has to be generated twice.

In addition to reducing the amount of memory held by the cache, this option will give you some idea of how many "one-off" queries you have. After this option is turned on, you can check to see how many stubs you have in the cache. If you don't have many, then it indicates that most of your queries are run multiple times and optimizing for ad hoc queries probably isn't doing you much good.

If you have lots of stubs it indicates that you've saved yourself space in your cache -- this was a change worth making.

But it might be telling you something else. It seems to me that an application with many unique queries is an unusual kind of application. I'd wonder if the application was generating queries in such a way that SQL Server didn't recognize that it could reuse its plans. I'd be interested in finding a way to "standardize" those queries to allow SQL Server to reuse their plans.

Posted by Peter Vogel on 10/15/2018 at 9:45 AM


  • Bright Tunnel

    Visual Studio 16.4 Preview 2 Boosted by Extension Tech

    Microsoft today shipped Visual Studio 2019 v16.4 Preview 2, boosted with new features that come from formerly separate extensions.

  • Nebula

    .NET Core 3.1 Preview 1 Focuses on Blazor, Desktop

    The first preview of .NET Core 3.1 focuses on two of the big features highlighting the Sept. 23 release of .NET Core 3.0: Blazor (for C# Web development instead of JavaScript) and desktop development (Windows Forms and Windows Presentation Foundation).

  • Stone Steps Graphic

    Microsoft Research's SandDance Data Visualization Tool Goes Open Source

    A data visualization tool some four years in the making from Microsoft Research has been open sourced, available for use as an extension for Visual Studio Code or Azure Data Studio.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events