SQL Server Extended Events (XEvents): Lightweight Performance Monitoring

Extended Events (XEvents) is a lightweight performance monitoring system in SQL Server that helps DBAs and developers track detailed information about system activities. Introduced in SQL Server 2008, Extended Events provides a scalable, high-performance framework for troubleshooting and monitoring SQL Server.

Key Features of Extended Events

  1. Lightweight: Minimal impact on server performance compared to traditional SQL Trace and Profiler.
  2. Scalable: Suitable for both small-scale and large-scale environments.
  3. Customizable: Allows detailed configuration of events, actions, and predicates.
  4. Integration: Works with SQL Server Management Studio (SSMS) for easy configuration and analysis.

Core Components

  1. Events: Represent actions or occurrences to monitor (e.g., sqlserver.sql_batch_completed).
  2. Actions: Additional information to collect when an event occurs (e.g., sql_text, session_id).
  3. Predicates (Filters): Conditions to limit data collection (e.g., only capture events from a specific database).
  4. Targets: Destinations for the event data (e.g., file, ring buffer, event counter).

Common Use Cases

  1. Performance Monitoring: Track query execution times, resource usage, and wait statistics.
  2. Troubleshooting: Identify deadlocks, long-running queries, and blocking issues.
  3. Auditing: Monitor specific database activities for security and compliance.

Basic Example

Here is an example of how to create and query an Extended Events session to monitor long-running queries:-- Create an Extended Events session

CREATE EVENT SESSION LongRunningQueries ON SERVER ADD EVENT sqlserver.sql_batch_completed( ACTION(sqlserver.sql_text, sqlserver.session_id) WHERE (duration > 10000000) -- 10 seconds in microseconds ) ADD TARGET package0.event_file( SET filename = 'C:\temp\LongRunningQueries.xel' );

GO

-- Start the Extended Events session

ALTER EVENT SESSION LongRunningQueries ON SERVER STATE = START;

GO

Query the Data Collected:-- Query the data collected by the Extended Events session

SELECT event_data.value('(event/@name)[1]', 'nvarchar(50)') AS event_name, event_data.value('(event/data[@name="duration"]/value)[1]', 'bigint') AS duration, event_data.value('(event/action[@name="sql_text"]/value)[1]', 'nvarchar(max)') AS sql_text, event_data.value('(event/action[@name="session_id"]/value)[1]', 'int') AS session_id FROM sys.fn_xe_file_target_read_file('C:\temp\LongRunningQueries*.xel', NULL, NULL, NULL) AS xe_data CROSS APPLY (SELECT CAST(xe_data.event_data AS XML) AS event_data) AS xed; GO

Managing Extended Events in SSMS

  1. Creating a Session:
  • Right-click on “Extended Events” in Object Explorer.
  • Choose “New Session” or “New Session Wizard” for guided configuration.
  • Define events, actions, predicates, and targets.
  1. Starting/Stopping a Session:
  • Right-click on the session and choose “Start Session” or “Stop Session”.
  1. Viewing Data:
  • Right-click on the session and choose “View Target Data”.
  • Use the integrated UI to analyze captured events.

Best Practices

  1. Keep Sessions Lightweight: Only capture necessary events to minimize performance impact.
  2. Use Filters: Apply predicates to filter out irrelevant events.
  3. Regular Maintenance: Periodically review and archive collected data to manage storage.
  4. Combine with Other Tools: Use in conjunction with SQL Server Profiler, Performance Monitor, and other monitoring tools for comprehensive analysis.

Extended Events provide a powerful and flexible way to monitor and troubleshoot SQL Server environments, offering deep insights with minimal overhead. For more detailed information, refer to the Microsoft Extended Events documentation.

This entry was posted in MSSQLServer on by .
Unknown's avatar

About SandeepSingh

Hi, I am working in IT industry with having more than 15 year of experience, worked as an Oracle DBA with a Company and handling different databases like Oracle, SQL Server , DB2 etc Worked as a Development and Database Administrator.

Leave a Reply