Deadlocks can be a common and challenging issue to troubleshoot in SQL Server environments. I previously wrote the blog post: Understanding SQL Server Deadlocks: A Beginner’s Guide, where I explained how deadlock works and my favorite methods of Extended Events to identify it. After reading this blog post, lots of people asked me to talk more about Trace Flags 1204 and 1222. Well, let us discuss Detecting Deadlocks in SQL Server Using Trace Flags 1204 and 1222.  In this blog post, we will explore how to use trace flags 1204 and 1222 for detecting deadlocks, and we’ll include the relevant T-SQL for enabling, disabling, and checking the status of these trace flags. I usually discuss about deadlocks during my popular service Comprehensive Database Performance Health Check.
Understanding Trace Flags 1204 and 1222
When a deadlock occurs, trace flags 1204 and 1222 capture information about the deadlock event and log it in the SQL Server error log. Trace flag 1204 provides deadlock information formatted by each node involved in the deadlock, while trace flag 1222 formats the information first by processes and then by resources. You can enable both trace flags simultaneously to obtain two representations of the same deadlock event.
Differences and Similarities Between Trace Flags 1204 and 1222
Trace flags 1204 and 1222 are useful for capturing deadlock information in SQL Server, but they format and present the information differently. Here’s a comparison of their similarities and differences:
Similarities
Both trace flags are used for detecting and diagnosing deadlocks in SQL Server.
Both trace flags capture deadlock information and log it in the SQL Server error log.
Both trace flags can be enabled and disabled using the same T-SQL commands (DBCC TRACEON and DBCC TRACEOFF), with the respective trace flag number as the parameter.
Differences
Output format: Trace flag 1204 focuses on the nodes involved in the deadlock, with each node having a dedicated section in the output. The final section describes the deadlock victim. On the other hand, trace flag 1222 returns information in an XML-like format (although it doesn’t conform to an XML Schema Definition). The format has three major sections: the first section declares the deadlock victim, the second section describes each process involved in the deadlock, and the third section describes the resources, which are synonymous with the nodes in trace flag 1204.
Detail level: Trace flag 1222 provides more detailed information about the deadlock, such as the input buffer (list of statements in the current batch), which can be useful for further analysis. Trace flag 1204 provides a more concise representation of the deadlock, focusing mainly on the nodes and their relationships.
While both trace flags are helpful for diagnosing deadlocks, trace flag 1222 may be preferred when more detailed information is required for analysis. However, enabling both trace flags simultaneously can provide two different perspectives on the same deadlock event, which may be useful for a comprehensive understanding of the issue.
Enabling Trace Flags 1204 and 1222
To enable trace flags 1204 and 1222, you can use the following T-SQL commands:
DBCC TRACEON (1204, -1); DBCC TRACEON (1222, -1);
The -1 parameter indicates that the trace flags should be enabled globally for all connections to the SQL Server instance.
Checking the Status of Trace Flags 1204 and 1222
You can use the DBCC TRACESTATUS command to check the status of trace flags 1204 and 1222:
DBCC TRACESTATUS (1204); DBCC TRACESTATUS (1222);
This command will return the status of the specified trace flags, indicating whether they are enabled or disabled.
Disabling Trace Flags 1204 and 1222
When you no longer need to capture deadlock information, you can disable trace flags 1204 and 1222 using the following T-SQL:
DBCC TRACEOFF (1204, -1); DBCC TRACEOFF (1222, -1);
Again, the -1 parameter indicates that the trace flags should be disabled globally for all connections to the SQL Server instance.
Error Logs and Deadlocks
After enabling traceflag 1204 and 1222 in SQL Server, you can check the error log using the following steps:
- Open SQL Server Management Studio (SSMS) and connect to the SQL Server instance.
- In the Object Explorer, expand the Management node and right-click on SQL Server Logs, then click on View SQL Server Log.
- In the SQL Server Log Viewer, select the current log file from the drop-down list of log files.
- Scroll through the log file to find errors related to the deadlock trace flags 1204 and 1222.
- Look for entries with a message similar to “Process [spid] was deadlocked on [lock] resources with another process and has been chosen as the deadlock victim.”
- If you want to filter the log to show only entries related to the deadlock trace flags, you can use the filter option in the SQL Server Log Viewer.
- To do this, click on the Filter button, then select the following options:
- Choose the “By Source” tab and select “SQL Server” as the source.
- Choose the “By Level” tab and select “Error” as the level.
- Choose the “By Keyword” tab and enter “deadlock” as the keyword.
- Click OK to apply the filter and view only the entries related to deadlock errors.
By following these steps, you should be able to check the error log in SQL Server after enabling traceflag 1204 and 1222.
Performance Impact of Trace Flags
Enabling trace flags 1204 and 1222 can have a performance impact on your SQL Server instance, particularly in environments with heavy workloads and frequent deadlocks. This is because these trace flags capture detailed information about deadlock events, which requires additional processing and resources.
Factors Contributing to Performance Impact
Increased I/O: When trace flags 1204 and 1222 are enabled, SQL Server writes deadlock information to the error log. This can increase I/O operations, as the system needs to write more information to disk. In high-workload environments with frequent deadlocks, this can lead to significant I/O overhead.
Additional CPU and memory usage: Capturing and formatting deadlock information requires additional CPU and memory resources. In environments where deadlocks occur often, this can result in increased CPU and memory usage, which can impact overall system performance.
Impact on concurrency: Diagnosing deadlocks involves analyzing the relationships between locks and processes. This requires SQL Server to acquire additional internal locks and synchronize access to shared data structures, which can impact concurrency and increase the likelihood of contention.
Mitigating Performance Impact
To mitigate the performance impact of trace flags 1204 and 1222, consider the following recommendations:
Enable trace flags only when necessary: Turn on trace flags 1204 and 1222 only when you need to diagnose deadlocks, and disable them once you have collected enough information. This helps minimize the performance impact on your SQL Server instance.
Use Extended Events: Instead of using trace flags 1204 and 1222, consider using the Deadlock Extended Event to capture deadlock information. Extended Events are a lightweight and customizable event-handling system in SQL Server, which can have a lower performance impact compared to trace flags.
Optimize SQL Server configuration: Ensure your SQL Server instance is properly configured and optimized to minimize deadlocks and reduce the need for enabling trace flags. This includes optimizing query performance, implementing appropriate indexing strategies, and managing transaction isolation levels.
By using trace flags 1204 and 1222 judiciously and following best practices for SQL Server configuration and optimization, you can minimize the performance impact of these trace flags while effectively diagnosing and resolving deadlocks.
Action Items – Detecting Deadlocks
- Identify if your SQL Server environment is experiencing deadlocks.
- Enable trace flags 1204 and 1222 using the provided T-SQL commands.
- Monitor the SQL Server error log to review deadlock information captured by the trace flags.
- Analyze the deadlock information to identify and troubleshoot the root cause.
- Once you have resolved the deadlock issue, disable trace flags 1204 and 1222 using the provided T-SQL commands.
By following these steps, you can effectively detecting deadlocks and diagnose them in your SQL Server environment using trace flags 1204 and 1222. If you need any further help with deadlocks or SQL Server Performance Tuning help, you can always reach out to me via Comprehensive Database Performance Health Check. You can also subscribe to my YouTube videos.
Reference:Â Pinal Dave (http://blog.SQLAuthority.com)