36
SQLintersection SQL Server Trace Flags : A Practical Guide Aaron Bertrand [email protected] SQL123

SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

  • Upload
    dangnga

  • View
    226

  • Download
    3

Embed Size (px)

Citation preview

Page 1: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

SQLintersection

SQL Server Trace Flags : A Practical Guide

Aaron [email protected]

SQL123

Page 2: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

About Me

Senior Consultant at SQL Sentry Editor for http://blogs.sqlsentry.com and http://sqlperformance.com Microsoft MVP since 1997 Community moderator at http://dba.stackexchange.com Twitter: @AaronBertrand

Page 3: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Overview

Overview Disclaimers What are trace flags? When should trace flags be used? The Meat – 116 trace flags! References

Page 4: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Disclaimers

I am sharing this information for educational purposes only

Unless explicitly stated, I do not advocate using any of these flags in production

Use at your own risk!

Page 5: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

What are trace flags?

Settings to change SQL Server’s behavior when no explicit setting exists, set: Globally (at startup or interactively) At session scope At query scope

Not all flags are valid at all scopes; some require startup / restart or 3604/3605

Many are not officially documented – most of the cool ones, actually Officially documented trace flags (/21) will be in green text (underlined) “Loosely” documented trace flags will be in in orange text (no underline) If I couldn’t even find it in the wiki (/264), the number will be in blue text

Page 6: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

When should trace flags be used?

Sparingly – when proven they help a scenario, without negative effects

Any trace flag you rely on MAY need to be turned off before support will helpAlso: “Trace flag behavior may not be supported in future releases of SQL Server.”

Functionality may change or stop working with a service pack, upgrade, etc.

I’ll discuss flags I always use, flags I never use, flags that deserve caution⚠There are hundreds I will *not* be talking about today

Page 7: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

The first two flags you need to know

3604 – Prints DBCC/TF output to the client (SSMS, debugger, …) 3605 – Logs DBCC/TF output to the error log

Some output can only go to the error log

Page 8: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Turning trace flags on & off

Globally (Configuration Manager / startup): -T3226 Globally (interactively / after startup): DBCC TRACEON(1118, -1); Session: DBCC TRACEON(1200); Query: OPTION (QUERYTRACEON 8649);

To turn off: DBCC TRACEOFF(1200 [, -1]);

To see status: DBCC TRACESTATUS;sys.dm_server_registry (2008 R2 SP1+)

Page 9: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

DemoTurning Trace Flags On & Off

AB_TF_1.sql

Page 10: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags I always use

3226 – prevents useless “backup succeeded” noise from filling error log 2371 – uses a better algorithm to trigger statistics updates at scale

Changes default 20% + (500 rows) => SQRT(1000 x (row count)) 2453 – uses #temp table recompilation threshold for table variables 1118 – prevents mixed extents, which can reduce SGAM/PFS contention

Microsoft says this has been fixed, but it can still help some workloads 7806 – enables DAC (only Express Edition)

Page 11: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags I almost always use

1117 – forces all data files to auto-grow at the same time 1808 – prevents auto-close databases from auto-closing

Page 12: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to use with caution

845 – locks pages in memory (reduces buffer pool paging) Also needs the “Lock Pages in Memory” user right Only necessary on Standard prior to SQL Server 2012

835 – turns off lock pages in memory

Page 13: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags I never use

253 – prevents ad hoc queries from being cached 272 – “fixes” IDENTITY gaps issue introduced in 2012⚠ 1806 – disables instant file initialization 3505 – disables automatic CHECKPOINT across all databases 3688 – suppresses trace start/stop from the log 8602 – ignores all index hints 8755 – ignores all locking hints 8722 – ignores all other hints (including OPTION)

Page 14: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

Demo8602 (Ignore Index Hints)

AB_TF_2.sql

Page 15: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to help with diagnostics 205 – logs procedure recompilations (with reasons) 646 – logs segment elimination details for columnstore queries 842 – foreign memory info in dm_os_memory_node_access_stats⚠ 2309 – changes SHOW_STATISTICS to allow 3rd param (partition number) 2388 – changes SHOW_STATISTICS output to one row with more details 3602 – traces all errors and warnings⚠ 3654 – provides more info in dm_os_memory_allocations⚠ 4032 – traces all SQL commands⚠ 8001 – exposes more waits in sys.dm_os_wait_stats (2005) 8050 – exposes *fewer* wait types in sys.dm_os_wait_stats (2008+) 8721 – log to error log whenever automatic stats are triggered

Page 16: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

Demo2309, 2388, 8050

AB_TF_3.sql, AB_TF_4.sql

Page 17: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to emulate Paul White – diagnose queries

2363 – shows stats used and selectivity in 2014+ – replaces 9204/9292 Caution: Can cause AVs⚠

2372 – shows memory usage during stages of optimization 2373 – shows rules used during optimization & memory used (2012+) 8666 – adds info such as stat object thresholds to showplan XML

See his great series here:http://sqlblog.com/blogs/paul_white/archive/2012/04/28/query-optimizer-deep-dive-part-1.aspxhttp://sqlblog.com/blogs/paul_white/archive/2012/04/28/query-optimizer-deep-dive-part-2.aspxhttp://sqlblog.com/blogs/paul_white/archive/2012/04/29/query-optimizer-deep-dive-part-3.aspxhttp://sqlblog.com/blogs/paul_white/archive/2012/05/01/query-optimizer-deep-dive-part-4.aspx

Page 18: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to emulate Paul White – diagnose queries 7352 – shows final query tree 7357 – shows unique hash optimization used (2012+) 8605 – shows converted tree (8612 adds cardinality to 8605-07) 8606 – shows input, simplified, join-collapsed, and normalized trees 8607 – shows output tree and whether query is cachable 8608 – shows initial memo structure 8609 – shows task and operation type counts (2012+) 8615 – shows final memo structure 8619 – shows transformation rules (8620 adds memo args) (2012+) 8621 – shows rule with resulting tree (2012+) 8675 – shows optimization stages and timing

Page 19: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

DemoPretend to Emulate Paul White

AB_TF_5.sql

Page 20: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to emulate Paul White – control queries

2340 – disables batch sort for nested loops (excessive memory grant)

4136 – OPTION (OPTIMIZE FOR UNKNOWN) to all queries⚠ 4137 – uses lowest selectivity for multiple AND predicates

If on new estimator in 2014, use 9471 4138 – turns off row goals with TOP / FAST <N> / IN / EXISTS 4199 – enables dozens of optimizer fixes⚠

This will be enabled by default in a future version Many individual 41xx flags were used to fix 2005 issues that no longer exist

Page 21: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to emulate Paul White – control queries

8649 – encourages parallel plans (sets cost threshold to 0) 8687 – discourages parallelism 8671 – prevents “good enough plan found”⚠ 8780 – spend more time finding optimal plan - also 8788/2301⚠ 8744 – disable pre-fetching for nested loops 8757 – skip trivial optimization phase 8790 – force a wide update plan 9130 – disable rewrites to residual predicates

Page 22: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to control engine behavior

174 – increases number of buckets in plan cache⚠ 634 – disables background columnstore compression task 652 – disables read-ahead reads (653 for just session) 661 – disables ghost cleanup record task 834 – large page allocation for buffer pool (often used with 840) 836 – use max server memory (instead of total) to size buffer pool 1851 – disables auto-merge for checkpoint files (In-Memory OLTP) 2335 – more conservative memory grants for plan generation 2861 – caches zero-cost / trivial plans⚠

Page 23: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags to control engine behavior

6498 – fixes RESOURCE_SEMAPHORE_QUERY_COMPILE waits issue 8008 – always puts new work on the least busy scheduler 8032 – allows for larger caches (e.g. plan cache)⚠

Page 24: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags for cardinality estimates

2389 – helps with better estimates in ascending key scenarios (also 2390) If on 2005, make sure @@VERSION >= 9.0.2209 Do not use 2390 by itself – it means once ascending is known, shuts off 4139 can address an estimation issue with 2389/2390 due to distribution

2312 – enables the 2014 cardinality estimator (9481 disables it) At database level: compatibility level – 120+ = new estimator, <= 110 = old

Page 25: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags for transaction log behavior

9024 – fixes issue with high log write waits for AGs in 2012+ Enabled by default in SQL Server 2014 SP1 For NUMA systems, may also need 8048 (please consult CSS)⚠

2537 – lets ::fn_dblog() scan full log rather than just active portion 1462 – turns off log stream compression⚠

Likely only useful if you have a fat pipe, SSDs, and CPU-bound system 610 – encourages minimal logging when possible

Page 26: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags for backup/restore behavior

3001 – like 3226, except prevents logging to msdb.dbo.backuphistory⚠ 3004 – adds output; instant file initialization info (to log only) 3014 – shows file stream and data size metrics 3023 – enables CHECKSUM for all backups 3042 – changes backup compression algorithm to not pre-size 3210 – logs information about wait times and page allocations 3212 – prints and logs configuration parameters (3213 does the same) 3216 – logs very verbose diagnostics about backup/restore internals 3400 – prints VLF information, logs undo/redo for restore operations

Page 27: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

DemoBabysit Long-Running Backup/Restore

AB_TF_6.sql

Page 28: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags showing locking behavior

1200 – show locking as query executes⚠ 1211 – disables escalation due to memory pressure / number of locks 1224 – disables escalation due to number of locks – preferred over 1211 1236 – encourages lock partitioning to reduce contention

Note: 2014 SP1 includes this behavior without the trace flag

Page 29: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags for deadlocks

1204 – shows resources and types of locks held in text format 1206 – adds additional locks held on non-affected objects 1208 – adds host and application name 1222 – shows resources and types of locks held in XML format

Let monitoring tools handle this

Page 30: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags for DBCC CHECKDB

2528 – disables parallelism for CHECKDB, CHECKFILEGROUP, CHECKTABLE Only matters in Enterprise Edition; these operations are serial in Standard

2509 – shows forwarded record count in DBCC CHECKTABLE 2514 – shows ghost record count in DBCC CHECKTABLE 2549 – treats each data file as if it were on a unique underlying disk⚠

Not many use cases for this, plus regression in 2014 2562 – treats DBCC as a single batch (higher tempdb strain)

Also uses better algorithm for reads (improved in 2012 without trace flag) 2566 – disables DATA_PURITY checks - 2005 only (SP2 CU9+)

Page 31: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Flags for undocumented DBCC commands

2588 – exposes help for undocumented DBCC (2520 pre-2005)http://www.sqlskills.com/blogs/paul/dbcc-writepage/

Similar disclaimers here – use this for education, not for production

Page 32: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

DemoShow Undocumented DBCC Help

AB_TF_7.sql

Page 33: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Resources

The most thorough list I know of is from Aaron Morelli:http://sqlcrossjoin.wordpress.com/2013/10/28/a-topical-collection-of-sql-server-flags/

Other 3rd party lists / articles:http://www.sqlservice.se/updated-microsoft-sql-server-trace-flag-list/http://www.sql-server-performance.com/2002/traceflags/http://www.sqlservercentral.com/articles/trace+flags/70131/http://troubleshootingsql.com/2014/01/20/sql-server-2012-trace-flags/http://database-wiki.com/2012/10/20/documented-sql-server-trace-flags-use-them-cautiously/http://www.benjaminnevarez.com/2012/04/more-undocumented-query-optimizer-trace-flags/

Page 34: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

© SQLintersection. All rights reserved.http://www.SQLintersection.com

Official Trace Flag DocumentationSQL Server 2005 http://technet.microsoft.com/en-us/library/ms188396(SQL.90).aspx

SQL Server 2008 http://technet.microsoft.com/en-us/library/ms188396(SQL.100).aspx

SQL Server 2008 R2 http://technet.microsoft.com/en-us/library/ms188396(SQL.105).aspx

SQL Server 2012 http://technet.microsoft.com/en-us/library/ms188396(SQL.110).aspx

SQL Server 2014 http://technet.microsoft.com/en-us/library/ms188396(SQL.120).aspx

More inclusive wikihttp://social.technet.microsoft.com/wiki/contents/articles/13105.trace-flags-in-sql-server.aspx

QUERYTRACEON http://support.microsoft.com/en-us/kb/2801413/en-us

Page 35: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

ResourcesSlide deck and demos available at:

http://sqlsentry.com/AaronBertrand

Password: $entry

Page 36: SQLintersection - Microsoft SQL Server Trace Flags : ... DBCC TRACEON(1118 , -1); Session: DBCC TRACEON(1200 ); ... 610 – encourages

Don’t forget to complete an online evaluation on EventBoard!

Your evaluation helps organizers build better conferencesand helps speakers improve their sessions.

Questions?

Thank you!

SQL Server Trace Flags : A Practical Guide