Ad hoc query stubs in plan cache

This metric measures the total number of one-time use ad hoc queries in the plan cache that have been stored in the form of a stub, not as a full execution plan.

Install metric...

Metrics install automatically if you have Redgate Monitor installed.

If you are using Redgate’s SQL Server monitoring tool, Redgate Monitor, you can instantly install and run this metric on your servers.

This metric measures the total number of one-time use ad hoc queries in the plan cache that have been stored in the form of a stub, not as a full execution plan. It will only report values if the instance-level “optimize for ad hoc workloads” option is turned on. If this option if turned off (the default value) it will only return a zero.

A high value indicates that the “optimize for ad hoc workloads” is working effectively. A low value indicates that having “optimize for ad hoc workloads” turned on for the instance may not be a good idea, as a low number indicates that your instance is not suffering from plan cache bloat, and in addition, it may be causing ad hoc queries to be recompiling more often than they need to be, which can hinder SQL Server performance.Like many SQL Server metrics, there is no perfect answer, and it is best to gather baseline data over time to see what is happening in your server. Generally speaking, if this value is less than 250, then the “optimize for ad hoc workloads” may not be particularly effective. But of course, this depends on your server.

One way to help you determine if “optimize for ad hoc workloads” should be on or off for a particular instance is to test. For example, if you turn off “optimize for ad hoc workloads”, and then watch the values of the custom metrics Ad hoc queries running once and Memory used by ad hoc queries running once, and these values grow substantially (for example, over 10,000 for the first metric and over 350 MB for the second metric), then you probably will want to turn “optimize for ad hoc workloads” back on. On the other hand, if the values of the above two metrics are below the recommended values, then you may want to leave “optimize for ad hoc workloads” turned off.

Metric definition

Name

Ad hoc query stubs in plan cache

Description

This metric measures the total number of one-time use ad hoc queries in the plan cache that have been stored in the form of a stub, not as a full execution plan. It will only report values if the instance-level “optimize for ad hoc workloads” option is turned on. If this option if turned off (the default value) it will only return a zero.

A high value indicates that the “optimize for ad hoc workloads” is working effectively. A low value indicates that having “optimize for ad hoc workloads” turned on for the instance may not be a good idea, as a low number indicates that your instance is not suffering from plan cache bloat, and in addition, it may be causing ad hoc queries to be recompiling more often than they need to be, which can hinder SQL Server performance.Like many SQL Server metrics, there is no perfect answer, and it is best to gather baseline data over time to see what is happening in your server. Generally speaking, if this value is less than 250, then the “optimize for ad hoc workloads” may not be particularly effective. But of course, this depends on your server.

One way to help you determine if “optimize for ad hoc workloads” should be on or off for a particular instance is to test. For example, if you turn off “optimize for ad hoc workloads”, and then watch the values of the custom metrics Ad hoc queries running once and Memory used by ad hoc queries running once, and these values grow substantially (for example, over 10,000 for the first metric and over 350 MB for the second metric), then you probably will want to turn “optimize for ad hoc workloads” back on. On the other hand, if the values of the above two metrics are below the recommended values, then you may want to leave “optimize for ad hoc workloads” turned off.

The T-SQL query that will collect data

Instances to collect from

Select all

Databases to collect from

master

Collection frequency

60

Use collected or calculated values

Leave the Use a calculated rate of change between collections check box unchecked

Metric collection

Enabled

Alert definition

Alert name

Ad hoc query stubs in plan cache

Description

This alert is raised when the total number of one-time use ad hoc queries in the plan cache that have been stored in the form of a stub (not as a full execution plan) exceed the specified threshold. It will only report values if the instance-level “optimize for ad hoc workloads” option is turned on. If this option if turned off (the default value) it will only return a zero.

A high value indicates that the “optimize for ad hoc workloads” is working effectively. A low value indicates that having “optimize for ad hoc workloads” turned on for the instance may not be a good idea, as a low number indicates that your instance is not suffering from plan cache bloat, and in addition, it may be causing ad hoc queries to be recompiling more often than they need to be, which can hinder SQL Server performance.Like many SQL Server metrics, there is no perfect answer, and it is best to gather baseline data over time to see what is happening in your server. Generally speaking, if this value is less than 250, then the “optimize for ad hoc workloads” may not be particularly effective. But of course, this depends on your server.

One way to help you determine if “optimize for ad hoc workloads” should be on or off for a particular instance is to test. For example, if you turn off “optimize for ad hoc workloads”, and then watch the values of the custom metrics Ad hoc queries running once and Memory used by ad hoc queries running once, and these values grow substantially (for example, over 10,000 for the first metric and over 350 MB for the second metric), then you probably will want to turn “optimize for ad hoc workloads” back on. On the other hand, if the values of the above two metrics are below the recommended values, then you may want to leave “optimize for ad hoc workloads” turned off.

Raise an alert when the metric value goes

Below the defined threshholds

Default threshold values

High:50
Medium:150
Low:250

Note: These thresholds are intended as guideline values. If they seem too high or too low for your environment, replace them with values more suited to your server performance.

Raise an alert when the threshold is passed for

10 collections

Alert is

Enabled