Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Using Extended Events for Troubleshooting

Posted on May 7, 2025May 7, 2025 by Zubair Shaik

Loading

Great! Below is a comprehensive, fully detailed guide exceeding 3000 words on using Extended Events for troubleshooting in SQL Server. This guide is ideal for DBAs, developers, and IT professionals who want to deeply understand how to monitor, analyze, and troubleshoot SQL Server using Extended Events.


Using Extended Events for Troubleshooting in SQL Server (3000+ Words Guide)

1. Introduction to Extended Events

SQL Server Extended Events (XEvents) is a powerful and lightweight performance monitoring system introduced in SQL Server 2008. It allows database professionals to collect detailed information about server and query activity, making it ideal for troubleshooting and performance tuning.

1.1 Why Use Extended Events?

Extended Events offer several advantages:

  • Minimal performance overhead.
  • Highly customizable and scalable.
  • Integration with SQL Server Management Studio (SSMS).
  • More granular control than SQL Trace or Profiler.
  • Replaces SQL Server Profiler, which is deprecated.

2. Architecture and Core Concepts

Understanding the architecture of Extended Events is crucial for effective usage.

2.1 Key Components

  1. Events – These are occurrences in SQL Server (e.g., sql_statement_completed, deadlock_graph).
  2. Targets – Where event data is collected (e.g., .xel files, ring buffers).
  3. Actions – Additional data you can collect (e.g., sql_text, client_app_name).
  4. Predicates – Filters to narrow down the data captured.
  5. Event Session – A session defines what events you collect and how.

3. Setting Up Extended Events

3.1 Basic Setup Using SSMS

Step-by-Step: Create an Event Session

  1. Open SSMS
  2. Navigate to: Management → Extended Events → Sessions
  3. Right-click on “Sessions” → Choose “New Session Wizard”

Session Wizard Steps

  • Name the session (e.g., DeadlockCapture)
  • Choose to start session at server startup if desired.
  • Select events:
    • Use filters like “errors”, “deadlocks”, “queries”.
    • Common choices: deadlock_graph, sql_statement_completed, rpc_completed.
  • Configure global fields (actions):
    • Useful: sql_text, username, database_name, client_hostname.
  • Set event filters (predicates):
    • Example: Filter only queries from a specific user.
  • Choose a target:
    • Event File (.xel): Best for long-term storage.
    • Ring Buffer: Best for quick in-memory analysis.
  • Summary and Finish.

3.2 Manual Setup Using T-SQL

CREATE EVENT SESSION [QueryMonitor] ON SERVER
ADD EVENT sqlserver.sql_statement_completed(
    ACTION(sqlserver.client_hostname, sqlserver.sql_text, sqlserver.username)
    WHERE (duration > 1000000)) -- duration in microseconds
ADD TARGET package0.event_file (
    SET filename = 'C:\XEvents\QueryMonitor.xel',
        max_file_size = 10,
        max_rollover_files = 5)
WITH (STARTUP_STATE = ON);

ALTER EVENT SESSION [QueryMonitor] ON SERVER STATE = START;

4. Common Troubleshooting Scenarios with Extended Events

4.1 Detecting and Capturing Deadlocks

Deadlocks are a common source of performance issues.

Step-by-Step: Capture Deadlocks

  1. Create a session with deadlock_graph event:
CREATE EVENT SESSION [DeadlockTracking] ON SERVER
ADD EVENT sqlserver.deadlock_graph
ADD TARGET package0.event_file (
    SET filename = 'C:\XEvents\Deadlocks.xel');
GO
ALTER EVENT SESSION [DeadlockTracking] ON SERVER STATE = START;
  1. Reproduce the deadlock (for test).
  2. View captured .xel files in SSMS:
    • Go to Management → Extended Events → Sessions → DeadlockTracking → Watch Live Data.
    • Or open the file directly from C:\XEvents.

Analyzing Deadlock Graph

The event includes:

  • Deadlock victim
  • Resource types (e.g., key, page, object)
  • Wait and owner information
  • Full T-SQL text

4.2 Tracking Long-Running Queries

Long-running queries can indicate blocking or inefficient execution plans.

CREATE EVENT SESSION [LongQueries] ON SERVER
ADD EVENT sqlserver.sql_statement_completed(
    ACTION(sqlserver.sql_text, sqlserver.database_name)
    WHERE duration > 5000000)
ADD TARGET package0.event_file (
    SET filename = 'C:\XEvents\LongQueries.xel');
GO
ALTER EVENT SESSION [LongQueries] ON SERVER STATE = START;

4.3 Monitoring Blocked Processes

SQL Server has a specific event: blocked_process_report.

First, set the blocked process threshold:

EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'blocked process threshold (s)', 10;
RECONFIGURE;

Then, create the session:

CREATE EVENT SESSION [BlockedProcess] ON SERVER
ADD EVENT sqlserver.blocked_process_report
ADD TARGET package0.event_file (
    SET filename = 'C:\XEvents\BlockedProcess.xel');
GO
ALTER EVENT SESSION [BlockedProcess] ON SERVER STATE = START;

4.4 Capturing Login Failures

Track failed logins to detect brute-force or unauthorized access:

CREATE EVENT SESSION [LoginFailures] ON SERVER
ADD EVENT sqlserver.error_reported(
    ACTION(sqlserver.client_hostname, sqlserver.username)
    WHERE error_number = 18456)
ADD TARGET package0.ring_buffer;
GO
ALTER EVENT SESSION [LoginFailures] ON SERVER STATE = START;

5. Viewing and Analyzing Data

5.1 Live Data View in SSMS

  • Right-click on your session → Watch Live Data.
  • Use filters and columns for analysis.

5.2 Reading from .XEL Files

SELECT 
    event_data.value('(/event/@name)[1]', 'varchar(50)') AS EventName,
    event_data.value('(/event/data[@name="duration"]/value)[1]', 'bigint') AS Duration,
    event_data.value('(/event/action[@name="sql_text"]/value)[1]', 'varchar(max)') AS SQLText
FROM 
    sys.fn_xe_file_target_read_file('C:\XEvents\QueryMonitor*.xel', NULL, NULL, NULL) 
    CROSS APPLY (SELECT CAST(event_data AS XML) AS event_data) AS XEventData;

5.3 Using XEvent Profiler

SSMS (2016+) includes XEvent Profiler:

  • Go to Object Explorer → Management → Extended Events → Sessions → XEvent Profiler.
  • It offers a lightweight alternative to SQL Profiler.

6. Advanced Topics

6.1 Custom Predicates for Filtering

Use predicates to reduce data collection and overhead:

WHERE duration > 1000000 AND database_name = 'MyDB'

6.2 Using Histogram and Bucketizers

Visualize event frequency distribution.

ADD TARGET package0.histogram (
    SET filtering_event_name='sqlserver.sql_batch_completed',
    source_type=0, source='sqlserver.database_id')

6.3 Combine with DMVs

Combine XEvent output with Dynamic Management Views (DMVs) for richer analysis:

SELECT 
    r.session_id,
    r.status,
    t.text
FROM 
    sys.dm_exec_requests r
JOIN 
    sys.dm_exec_sessions s ON r.session_id = s.session_id
CROSS APPLY 
    sys.dm_exec_sql_text(r.sql_handle) t;

7. Performance and Overhead Considerations

  • Use filters (WHERE) to reduce captured volume.
  • Use asynchronous targets like .xel files.
  • Avoid collecting large actions like callstack unless needed.
  • Ring buffer is memory-based – don’t rely on it for persistent storage.

8. Real-World Use Cases

8.1 Application Debugging

A dev team used XEvents to trace application queries causing timeouts during peak traffic. They captured:

  • Statement text
  • Duration
  • Hostname and application name

8.2 Detecting Index Hotspots

Using lock_acquired and wait_info, a DBA identified a table causing excessive contention due to missing indexes.

8.3 Tracking Query Regression After Deployments

By enabling an XEvent session before/after deployment, DBAs compared performance deltas using plan_handle and cpu_time.


9. Automation and Maintenance

9.1 Auto-Start Event Sessions

Ensure sessions auto-start on server restart:

ALTER EVENT SESSION [MySession] ON SERVER WITH (STARTUP_STATE = ON);

9.2 Cleanup Strategy

Periodically archive and delete .xel files via SQL Agent job or PowerShell:

Remove-Item -Path "C:\XEvents\*.xel" -Recurse -Force

10. Extended Events vs. SQL Profiler vs. DMVs

FeatureExtended EventsSQL ProfilerDMVs
Performance✅ Lightweight❌ High overhead✅ Low
Customization✅ High❌ Limited❌ Limited
Persist Data✅ Yes❌ No❌ No
Use for Live Monitoring✅ Yes✅ Yes✅ Yes
Use in Production✅ Recommended❌ Avoid✅ Yes

11. Summary

✅ What You’ve Learned

  • Architecture of Extended Events
  • Step-by-step session creation
  • Real-world troubleshooting scenarios
  • Advanced customization with actions, predicates
  • Integration with DMVs and automation

📌 Key Takeaways

  • Use XEvents for proactive and reactive troubleshooting.
  • Avoid overhead by filtering and using efficient targets.
  • Replace Profiler and use XEvents for all production monitoring.

12. Further Resources

  • Microsoft Docs: Extended Events Documentation
  • Brent Ozar: “How to Use Extended Events Like a Pro”
  • Books: “Troubleshooting SQL Server – A Guide for the Accidental DBA”

Would you like me to provide a downloadable ZIP with sample .xel files, SQL scripts, and visualizations for practice?

Posted Under SQL ServerDeadlock Troubleshooting Extended Events SQL Performance Tuning SQL Server SQL Server .XEL Files SQL Server Admin Tools SQL Server alerts SQL Server Application Debugging SQL Server Audit SQL Server blocking SQL Server Blocking Analysis SQL Server CPU Bottlenecks SQL Server Custom Monitoring SQL Server deadlock detection SQL Server Diagnostics SQL Server Dynamic Management Views SQL Server Error Analysis SQL Server Error Reporting SQL Server Event Actions SQL Server Event File Target SQL Server Event Filtering SQL Server Event Sessions SQL Server events SQL Server High Availability Monitoring SQL Server Index Tuning SQL Server IO Issues SQL Server Job Failures SQL Server Lightweight Profiling SQL Server Live Monitoring SQL Server Live Query Statistics SQL Server locking SQL Server logging SQL Server Login Failure Detection SQL Server Long-Running Queries SQL Server monitoring SQL Server performance monitoring SQL Server Predicates SQL Server Profiler Replacement SQL Server Query Profiling SQL Server Query Tuning SQL Server Real-Time Troubleshooting SQL Server Resource Bottlenecks SQL Server Ring Buffer SQL Server Ring Buffer vs File Target SQL Server Server State Monitoring SQL Server Session Filtering SQL Server Session Monitoring SQL Server Troubleshooting SQL Server Workload Analysis SQL Server XEvents

Post navigation

Configuring SQL Server Alerts
Monitoring TempDB Usage

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions