Select the product you need help with
Enable plan-affecting SQL Server query optimizer behavior that can be controlled by different trace flags on a specific-query levelArticle ID: 2801413 - View products that this article applies to. IntroductionStarting with Microsoft SQL Server 2000 Service Pack 3 (SP3), the SQL Server query processor team adopted a policy that any hotfix that could potentially affect the execution plan of a query must be controlled by a trace flag. Except for fixes to bugs that can cause incorrect results or corruption, these hotfixes are turned off by default, and a trace flag is required to enable the fix. This policy helps avoid unexpected changes to the execution plans of existing workloads that may occur when a hotfix or a security update is installed. Usually trace flags are enabled at startup or in a user session. However, this may have an unexpected effect on some queries in an existing database application. For example, consider an application or workload that includes multiple queries, and some of these queries use an inefficient query execution plan that is improved by enabling a trace flag that controls a corresponding hotfix. However, other queries may experience a less optimal execution plan when the same trace flag is applied. This is because the execution plan choice affects all queries that are compiled in the instance or the session when the corresponding trace flag is enabled. Depending on the query and the data, changing the models that are used by the query optimizer may both improve and decrease execution plan efficiency and compilation time for particular queries. If a trace flag affects any query execution plan in an unwanted way, but improves some other query execution plan, you may want to enable a corresponding trace flag for only a particular query. You can do this by enabling the trace flag in a batch (by using DBCC TRACEON command) right before the target query, and then disabling the trace flag (by using DBCC TRACEOFF command) right after the query. However, this may not always be possible to control the Transact-SQL batch text for existing applications. You may experience poor query performance in an existing workload, and want to apply an available plan-affecting change to a query without changing the batch text itself. You can do this by using a query-level option to enable a trace flag for only a particular query. Starting with Microsoft SQL Server 2005 Service Pack 2 (SP2) and Microsoft SQL Server 2008, the query-level option "QUERYTRACEON" is available. This option lets you to enable a plan-affecting trace flag only during single-query compilation. Like other query-level options, you can use it together with plan guides to match the text of a query being executed from any session, and automatically apply a plan-affecting trace flag when this query is being compiled. More informationThe QUERYTRACEON hint is available as a query hint that enables a plan-affecting change in the query optimizer that is controlled by a trace flag. The QUERYTRACEON hint is specified as part of the OPTION clause similar to other query hints
(http://msdn.microsoft.com/en-us/library/ms181714.aspx)
.Syntax
<querytraceon_hint> ::=
{ QUERYTRACEON trace_flag_number }
ArgumentsQUERYTRACEON trace_flag_numberThis specifies a plan-affecting trace flag number that is enabled during compiling of the query. The following trace flag numbers are supported: Collapse this table
RemarksThe QUERYTRACEON option is not supported for trace flags other than the trace flags that are listed in the table. However, this option will not return any error or warning if an unsupported trace flag number is used. If the specified trace flag is not one that affects a query execution plan, the option will be silently ignored.More than one trace flag can be specified in the OPTION clause if QUERYTRACEON trace_flag_number is duplicated with different trace flag numbers. The QUERYTRACEON option can be used in Plan Guides
(http://msdn.microsoft.com/en-us/library/ms190417.aspx)
.Examples
PropertiesArticle ID: 2801413 - Last Review: April 5, 2013 - Revision: 2.0 Applies to
|


Back to the top








