|
1 | | ---- |
2 | | -title: optimize for ad hoc workloads (server configuration option) |
3 | | -description: "Learn about the optimize for ad hoc workloads option. Use it to improve SQL Server plan cache efficiency when workloads contain many single-use ad hoc batches." |
4 | | -author: rwestMSFT |
5 | | -ms.author: randolphwest |
6 | | -ms.date: 08/12/2022 |
7 | | -ms.service: sql |
8 | | -ms.subservice: configuration |
9 | | -ms.topic: conceptual |
10 | | -helpviewer_keywords: |
11 | | - - "optimize for ad hoc workloads option" |
12 | | ---- |
13 | | - |
14 | | -# optimize for ad hoc workloads (server configuration option) |
15 | | - |
16 | | -[!INCLUDE [SQL Server Azure SQL Database](../../includes/applies-to-version/sql-asdb-asdbmi-pdw.md)] |
17 | | - |
18 | | -The **optimize for ad hoc workloads** option is used to improve the efficiency of the plan cache for workloads that contain many single use ad hoc batches. When this option is set to `1`, the Database Engine stores a small compiled plan stub in the plan cache when a batch is compiled for the first time, instead of the full compiled plan. This may help to relieve memory pressure by not allowing the plan cache to become filled with compiled plans that aren't reused. However, enabling this option may affect your ability to troubleshoot single-use plans. |
19 | | - |
20 | | -The compiled plan stub allows the [!INCLUDE[ssDE](../../includes/ssde-md.md)] to recognize that this ad hoc batch has been compiled before but has only stored a compiled plan stub, so when this batch is invoked (compiled or executed) again, the [!INCLUDE[ssDE](../../includes/ssde-md.md)] compiles the batch, removes the compiled plan stub from the plan cache, and adds the full compiled plan to the plan cache. |
21 | | - |
22 | | -You can find compiled plan stubs by querying the `sys.dm_exec_cached_plans` catalog view and looking for "Compiled Plan" in the `cacheobjtype` column. The stub has a unique `plan_handle`. The compiled plan stub doesn't have an execution plan associated with it, and querying for the plan handle doesn't return an XML Showplan. |
23 | | - |
24 | | -[Trace flag 8032](../../t-sql/database-console-commands/dbcc-traceon-trace-flags-transact-sql.md) reverts the cache limit parameters to the [!INCLUDE[ssVersion2005](../../includes/ssversion2005-md.md)] RTM setting, which in general allows caches to be larger. Use this setting when frequently reused cache entries don't fit into the cache and when the **optimize for ad hoc workloads** option has failed to resolve the problem with plan cache. |
25 | | - |
26 | | -> [!WARNING] |
27 | | -> Trace flag 8032 can cause poor performance if large caches make less memory available for other memory consumers, such as the buffer pool. |
28 | | -
|
29 | | -## Remarks |
30 | | - |
31 | | -Setting the **optimize for ad hoc workloads** option to `1` affects only new plans; plans that are already in the plan cache are unaffected. |
32 | | - |
33 | | -To affect already cached query plans immediately, the plan cache needs to be cleared using [ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE](../../t-sql/statements/alter-database-scoped-configuration-transact-sql.md), or [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] has to restart. |
34 | | - |
35 | | -## Recommendations |
36 | | - |
37 | | -Avoid having a large number of single-use plans in the plan cache. A common cause of this problem is when the data types of query parameters aren't consistently defined. This particularly applies to the length of strings but can apply to any data type that has a maxlength, a precision, or a scale. For example, if a parameter named `@Greeting` is passed as **nvarchar(10)** on one call and **nvarchar(20)** on the next call, separate plans are created for each parameter size. |
38 | | - |
39 | | -If a query has several parameters and they aren't consistently defined when called, a large number of query plans could exist for each query. Plans could exist for each combination of query parameter data types and lengths that have been used. |
40 | | - |
41 | | -If the number of single-use plans take a significant portion of [!INCLUDE[ssDEnoversion](../../includes/ssdenoversion-md.md)] memory in an OLTP server, and these plans are ad hoc plans, use this server option to decrease memory usage with these objects. |
42 | | - |
43 | | -## Example |
44 | | - |
45 | | -To find the number of single-use cached plans, run the following query: |
46 | | - |
47 | | -```sql |
48 | | -SELECT objtype, cacheobjtype, |
49 | | - AVG(usecounts) AS Avg_UseCount, |
50 | | - SUM(refcounts) AS AllRefObjects, |
51 | | - SUM(CAST(size_in_bytes AS bigint))/1024/1024 AS SizeInMB |
52 | | -FROM sys.dm_exec_cached_plans |
53 | | -WHERE objtype = 'Adhoc' AND usecounts = 1 |
54 | | -GROUP BY objtype, cacheobjtype; |
55 | | -``` |
56 | | - |
57 | | -## See also |
58 | | - |
59 | | -- [sys.dm_exec_cached_plans (Transact-SQL)](../../relational-databases/system-dynamic-management-views/sys-dm-exec-cached-plans-transact-sql.md) |
60 | | -- [Server Configuration Options (SQL Server)](../../database-engine/configure-windows/server-configuration-options-sql-server.md) |
| 1 | +--- |
| 2 | +title: Optimize for ad hoc workloads (server configuration option) |
| 3 | +description: "Learn about the 'optimize for ad hoc workloads' option. Use it to improve SQL Server plan cache efficiency when workloads contain many single-use ad hoc batches." |
| 4 | +author: rwestMSFT |
| 5 | +ms.author: randolphwest |
| 6 | +ms.date: 12/18/2023 |
| 7 | +ms.service: sql |
| 8 | +ms.subservice: configuration |
| 9 | +ms.topic: conceptual |
| 10 | +helpviewer_keywords: |
| 11 | + - "optimize for ad hoc workloads option" |
| 12 | +--- |
| 13 | + |
| 14 | +# Optimize for ad hoc workloads (server configuration option) |
| 15 | + |
| 16 | +[!INCLUDE [SQL Server Azure SQL Database](../../includes/applies-to-version/sql-asdb-asdbmi-pdw.md)] |
| 17 | + |
| 18 | +The **optimize for ad hoc workloads** option is used to improve the efficiency of the plan cache for workloads that contain many single use ad hoc batches. When this option is set to `1`, the Database Engine stores a small compiled plan stub in the plan cache when a batch is compiled for the first time, instead of the full compiled plan. This option might help to relieve memory pressure by not allowing the plan cache to become filled with compiled plans that aren't reused. However, enabling this option might affect your ability to troubleshoot single-use plans. |
| 19 | + |
| 20 | +The compiled plan stub allows the [!INCLUDE [ssDE](../../includes/ssde-md.md)] to recognize that this ad hoc batch was compiled before, only stores a compiled plan stub. When this batch is invoked (compiled or executed) again, the [!INCLUDE [ssDE](../../includes/ssde-md.md)] compiles the batch, removes the compiled plan stub from the plan cache, and adds the full compiled plan to the plan cache. |
| 21 | + |
| 22 | +You can find compiled plan stubs by querying the `sys.dm_exec_cached_plans` catalog view and looking for "Compiled Plan" in the `cacheobjtype` column. The stub has a unique `plan_handle`. The compiled plan stub doesn't have an execution plan associated with it, and querying for the plan handle doesn't return a graphical or XML showplan. |
| 23 | + |
| 24 | +[Trace Flag 8032](../../t-sql/database-console-commands/dbcc-traceon-trace-flags-transact-sql.md) reverts the cache limit parameters to the [!INCLUDE [ssVersion2005](../../includes/ssversion2005-md.md)] RTM setting, which in general allows caches to be larger. Use this setting when frequently reused cache entries don't fit into the cache and when the **optimize for ad hoc workloads** option failed to resolve the problem with plan cache. |
| 25 | + |
| 26 | +> [!WARNING] |
| 27 | +> Trace Flag 8032 can cause poor performance if large caches make less memory available for other memory consumers, such as the buffer pool. |
| 28 | +
|
| 29 | +## Remarks |
| 30 | + |
| 31 | +Setting the **optimize for ad hoc workloads** option to `1` affects only new plans; plans that are already in the plan cache are unaffected. |
| 32 | + |
| 33 | +To affect already cached query plans immediately, the plan cache needs to be cleared using [ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE](../../t-sql/statements/alter-database-scoped-configuration-transact-sql.md), or [!INCLUDE [ssNoVersion](../../includes/ssnoversion-md.md)] has to restart. |
| 34 | + |
| 35 | +## Recommendations |
| 36 | + |
| 37 | +Avoid having a large number of single-use plans in the plan cache. Common causes include: |
| 38 | + |
| 39 | +- Data types of query parameters that aren't consistently defined. This particularly applies to the length of strings but can apply to any data type that has a maxlength, a precision, or a scale. For example, if a parameter named `@Greeting` is passed as **nvarchar(10)** on one call and **nvarchar(20)** on the next call, separate plans are created for each parameter size. |
| 40 | + |
| 41 | +- Queries that aren't parameterized. If a query has one or more parameters for which hard-coded values get submitted to the [!INCLUDE [ssde-md](../../includes/ssde-md.md)], a large number of query plans could exist for each query. Plans could exist for each combination of query parameter data types and lengths that were used. |
| 42 | + |
| 43 | +If the number of single-use plans take a significant portion of [!INCLUDE [ssDEnoversion](../../includes/ssdenoversion-md.md)] memory in an OLTP server, and these plans are ad hoc plans, use this server option to decrease memory usage with these objects. |
| 44 | + |
| 45 | +If the **optimize for ad hoc workloads** option is enabled, you can't view execution plans for single-use queries, because only the plan stub is cached. Depending on your environment and workload, you might benefit from the following two features: |
| 46 | + |
| 47 | +- The **[Query Store](../../relational-databases/performance/monitoring-performance-by-using-the-query-store.md)** feature, introduced in [!INCLUDE [sssql16-md](../../includes/sssql16-md.md)], helps you quickly find performance differences caused by query plan changes. Query Store is enabled by default on new databases in [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and later versions. |
| 48 | + |
| 49 | +- **[Forced parameterization](../../relational-databases/query-processing-architecture-guide.md#forced-parameterization)** can improve the performance of certain databases by reducing the frequency of query compilations and recompilations. Databases that benefit from forced parameterization generally experience high volumes of concurrent queries from sources such as point-of-sale applications. |
| 50 | + |
| 51 | + Forced parameterization can cause performance issues due to parameter sensitivity. For more information, see [Investigate and resolve parameter-sensitive issues](/troubleshoot/sql/database-engine/performance/troubleshoot-high-cpu-usage-issues#step-5-investigate-and-resolve-parameter-sensitive-issues). For [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and later versions, you can also enable [Parameter Sensitive Plan optimization](../../relational-databases/performance/parameter-sensitive-plan-optimization.md). |
| 52 | + |
| 53 | +## Examples |
| 54 | + |
| 55 | +To find the number of single-use cached plans, run the following query: |
| 56 | + |
| 57 | +```sql |
| 58 | +SELECT objtype, |
| 59 | + cacheobjtype, |
| 60 | + AVG(usecounts) AS Avg_UseCount, |
| 61 | + SUM(refcounts) AS AllRefObjects, |
| 62 | + SUM(CAST(size_in_bytes AS BIGINT)) / 1024 / 1024 AS SizeInMB |
| 63 | +FROM sys.dm_exec_cached_plans |
| 64 | +WHERE objtype = 'Adhoc' AND usecounts = 1 |
| 65 | +GROUP BY objtype, cacheobjtype; |
| 66 | +``` |
| 67 | + |
| 68 | +## Related content |
| 69 | + |
| 70 | +- [sys.dm_exec_cached_plans (Transact-SQL)](../../relational-databases/system-dynamic-management-views/sys-dm-exec-cached-plans-transact-sql.md) |
| 71 | +- [Server configuration options (SQL Server)](server-configuration-options-sql-server.md) |
| 72 | +- [Monitor performance by using the Query Store](../../relational-databases/performance/monitoring-performance-by-using-the-query-store.md) |
| 73 | +- [Query processing architecture guide](../../relational-databases/query-processing-architecture-guide.md) |
| 74 | +- [Parameter Sensitive Plan optimization](../../relational-databases/performance/parameter-sensitive-plan-optimization.md) |
0 commit comments