{"name":"Ad hoc query stubs in plan cache","description":"<p>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 \u201coptimize for ad hoc workloads\u201d option is turned on. If this option if turned off (the default value) it will only return a zero.<\/p>\r\n\r\n<p>A high value indicates that the \u201coptimize for ad hoc workloads\u201d is working effectively. A low value indicates that having \u201coptimize for ad hoc workloads\u201d 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.<\/p?\r\n\r\n<p>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 \u201coptimize for ad hoc workloads\u201d may not be particularly effective. But of course, this depends on your server.<\/p>\r\n\r\n<p>One way to help you determine if \u201coptimize for ad hoc workloads\u201d should be on or off for a particular instance is to test. For example, if you turn off \u201coptimize for ad hoc workloads\u201d, and then watch the values of the custom metrics <a href=http:\/\/sqlmonitormetrics.red-gate.com\/ad-hoc-queries-running-only-once\/> Ad hoc queries running once<\/a> and <a href=http:\/\/sqlmonitormetrics.red-gate.com\/memory-used-by-ad-hoc-queries-running-only-once\/> Memory used by ad hoc queries running once,<\/a> 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 \u201coptimize for ad hoc workloads\u201d 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 \u201coptimize for ad hoc workloads\u201d turned off.<\/p>","tsql":"SELECT SUM(CASE WHEN usecounts = 1\r\n                THEN 1\r\n                ELSE 0\r\n           END) AS [Adhoc Plans Use Count of 1]\r\n  FROM sys.dm_exec_cached_plans\r\n  WHERE objtype = 'Adhoc'\r\n    AND cacheobjtype = 'Compiled Plan Stub'\r\n  GROUP BY objtype;\r\n","instances":true,"frequency":60,"databases":{"mode":1},"rateofchange":false,"metricenabled":true,"alertname":"Ad hoc query stubs in plan cache","alertdescription":"<p>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 \u201coptimize for ad hoc workloads\u201d option is turned on. If this option if turned off (the default value) it will only return a zero.<\/p>\r\n\r\n<p>A high value indicates that the \u201coptimize for ad hoc workloads\u201d is working effectively. A low value indicates that having \u201coptimize for ad hoc workloads\u201d 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.<\/p?\r\n\r\n<p>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 \u201coptimize for ad hoc workloads\u201d may not be particularly effective. But of course, this depends on your server.<\/p>\r\n\r\n<p>One way to help you determine if \u201coptimize for ad hoc workloads\u201d should be on or off for a particular instance is to test. For example, if you turn off \u201coptimize for ad hoc workloads\u201d, and then watch the values of the custom metrics <a href=http:\/\/sqlmonitormetrics.red-gate.com\/ad-hoc-queries-running-only-once\/> Ad hoc queries running once<\/a> and <a href=http:\/\/sqlmonitormetrics.red-gate.com\/memory-used-by-ad-hoc-queries-running-only-once\/> Memory used by ad hoc queries running once,<\/a> 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 \u201coptimize for ad hoc workloads\u201d 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 \u201coptimize for ad hoc workloads\u201d turned off.<\/p>","aboveorbelow":"below","collections":10,"alertenabled":true,"_thresholds_high":{"selected":true,"value":50},"_thresholds_medium":{"selected":true,"value":150},"_thresholds_low":{"selected":true,"value":250},"thresholds_comment":"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.","targetentitytype":0}