| Lesson 6 | Extended Events and Sessions |
| Objective | Run extended events and sessions |
In modern SQL Server, performance and diagnostic monitoring is built around Extended Events (XE / XEvents).
You do not “run traces” anymore—your operational workflow is to create an Extended Events session, start it, view data live or
write results to a target (usually an .xel file), then stop and analyze the capture.
event_file target, choose a path with sufficient space and a rollover policy.Think of an XE session as a repeatable diagnostic instrument you define once and run when needed:
.xel file in SSMS or via T-SQL).XE_SlowStatements or XE_Deadlocks).Select events that match your troubleshooting goal. Common starting points:
Actions are the “who/where/what context” you attach to the event stream: session id, database name, application name, user name, SQL text, and so on. Start minimal—only add what you will actually use to filter and analyze.
Filters reduce overhead and noise. For example, only capture duration ≥ 1 second, or only capture activity in one database. Aggressive filtering is a best practice for production troubleshooting.
Scripting is ideal when you want repeatability (runbooks, incident response, jobs, and consistent production workflows).
The example below captures slow statements to an event_file target with rollover, causality tracking, and a time limit.
-- Create a session to capture slow statements with useful context
CREATE EVENT SESSION [XE_SlowStatements] ON SERVER
ADD EVENT sqlserver.sql_statement_completed
(
ACTION
(
sqlserver.session_id,
sqlserver.database_name,
sqlserver.client_app_name,
sqlserver.username,
sqlserver.sql_text,
package0.event_sequence
)
WHERE
(
[duration] >= (1000000) -- 1 second (microseconds)
)
)
ADD TARGET package0.event_file
(
SET filename = N'C:\XE\XE_SlowStatements.xel',
max_file_size = (50),
max_rollover_files = (4)
)
WITH
(
MAX_DISPATCH_LATENCY = 5 SECONDS,
TRACK_CAUSALITY = ON,
MAX_DURATION = 2 HOURS
);
GO
-- Start the session
ALTER EVENT SESSION [XE_SlowStatements] ON SERVER STATE = START;
GO
-- Stop the session when finished
ALTER EVENT SESSION [XE_SlowStatements] ON SERVER STATE = STOP;
GO
If you used event_file, you can open the .xel file in SSMS, or query it with T-SQL for automation and reporting.
-- Read events from the XEL file (one row per event)
SELECT
CAST(event_data AS xml) AS event_data_xml
FROM sys.fn_xe_file_target_read_file
(
N'C:\XE\XE_SlowStatements*.xel',
NULL, NULL, NULL
);
The images below come from the legacy SQL Server Profiler era. They are preserved on this site to help you recognize older tooling in legacy documentation.
In modern SQL Server, you will start/stop XE sessions (SSMS “Start Session / Stop Session” or ALTER EVENT SESSION) rather than
“run traces.”
package0.event_sequence to reconstruct deterministic ordering during analysis.
.xel output from an event_file target.
ALTER EVENT SESSION [name] ON SERVER STATE = START;