SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing
Sponsors
Win a 1day SQL 2016 course Enter your adress at for a chance to win a 1 day course in SQL Server 2016 arranged by SQL Service and LabCenterhttp://bit.ly/sqldag
About me SQL Server nerd since 1995 SQL Server 4.21, 6.0, 6.5, 7.0, 2000, 2005, 2008, 2012, 2014, 2016, Azure Former Member of the Board SQLUG (sqlug.se) Blog: Speaker at: TechDays Microsoft Sommarkollo Cloud Seminarium SQL Saturday
What are Trace Flags? Trace Flags are settings that in some way or another alters the behavior of various SQL Server functions. Is inherited from Sybase SQL Server code Some trace flags are still the same such as 1204, 3604, 3605, 3607, 3608 REMEMBER: Be extremely careful with trace flags, test in your test environment first. And consult professionals first if you are the slightest uncertain about the effects of your changes.
Why should I care? It could save you some day, like it saved me… msdb110_upgrade-sql-encountered-error-547/ msdb110_upgrade-sql-encountered-error-547/
How do I use Trace Flags? How do I turn Trace Flags on and off? *1 – You can use the DBCC TRACEON and DBCC TRACEOFF commands – You can use the -T option in the startup configuration for the SQL Server Service -You can also use the hint QUERYTRACEON in your queries: ::={ QUERYTRACEON trace_flag_number } How do I know what Trace Flags are turned on at the moment? – You can use the DBCC TRACESTATUS command Numbers from -1 to seems to be valid *2 Some Trace Flags only works in combination with others Some Trace Flags only works when used a certain way Startup, QUERYTRACEON, Global Turning on all Trace Flags – Do try this at home! (Not at work..) *3
Ok, ok! Now show me the Trace Flags! Statistics Locks Memory Performance Troubleshooting Other
Statistics 2371 Updates statistics more often on tables with more than rows 4136 *4 Uses density vector instead of histogram for cardinality estimate 2389 *4 Enable auto quick statistics update on known ascending columns 2390 *4 Enable auto quick statistics update on all columns
Locks 1200 *5 Prints the process ID and type of lock requested as every request for a lock is made 1204,1222 Prints deadlock info in the errorlog 1211 Disables lock escalation based on memory pressure 1224 *5 Disables lock escalation based on number of locks 1236 Fixes performance problem in scenarios with high SH-DATABASE lock activity in SQL 2012 and SQL 2014
Memory 834 Large Page Allocations (Might be on by default from SQL 2014) 2335 Generates Query Plans optimized for less memory To be used if you have 512 GB ram or more 2544 Produce a full memory dump in case of an exception 8032 Reverts the cache limit parameters to the SQL Server 2005 RTM setting which in general allows caches such as the Plan Cache to be larger
Performance 610 Minimally logged inserts to indexed tables 661 Disable the ghost record removal process 662 Prints detailed information about the work done by the ghost cleanup task when it runs next. (Use with TF 3605) 1117 Simultaneous Autogrowth in Multiple-file database 1118 Force Uniform Extent Allocation 2312 Forces the query optimizer to use the SQL Server 2014 version of the cardinality estimator 2340 Disable specific SORT optimization in Query Plan 4199 Turn on all optimizations. But from SQL 2016 on, MS has an altered strategy. New major versions will have all ”old” 4199 fixes enabled. 8780 Give the optimizer more time to find a better plan
Troubleshooting 902 Bypass Upgrade Scripts 3607 Skip recovery on startup 3608 Recover only Master db at startup 3609 Do not create tempdb at startup 4022 Bypass Startup procedures
Other 445 – ”Compile Issued” statement to errorlog (with 3605) – New! *6 2388 – Alter DBCC SHOW_STATISTICS output – New! 2529 – Displays DBCC memory allocations – New! *7 Redirect DBCC command output to query window Directs the output of some Trace Flags to the Errorlog Turns off ”Backup Successful” messages in errorlog
What Trace Flags should I always turn on? On new servers: 834 for DW servers, 1117, 1118, 3226, 4199 (not on SQL 2016) On exsisting servers: 834 for DW servers,1117, 1118, 3226
Read more! SQL Service Trace Flag List: trace-flag-list/ trace-flag-list/ SQL Server Central TF List: MSDN TF list: Albert van der Sel TF list: Technet Wiki TF list: server.aspx server.aspx Amit Banerjee TF list: trace-flags/ trace-flags/ Paul Randal discussing TF Pro’s and Con’s: pros-and-cons-of-trace-flags/ pros-and-cons-of-trace-flags/ Some trace flags needs to be specified with ”t” rather than with ”T” in startup options!
Read even more! QUERYTRACEON us/kb/ https://support.microsoft.com/en- us/kb/