Extended EventsPractical Examples
Dean Richards
Who Am I
• 20+ years of experience in SQL Server and Oracle
• Speak at many user groups throughout US
• Owner of DRConsulting• [email protected]
• @ConfioDean
• Focus on application and database performance
• Review performance for hundreds of customers per year
• Common question – how do I do performance tuning?
Agenda
• Extended Events Introduction
• Terms & Useful DMVs
• How to Create Sessions
• Viewing & Reporting Event Info
• Examples• Deadlock Monitoring
• Query Performance and Waits
• Actual Execution Plans
Extended Events Introduction
• Lightweight event-handling mechanism • Captures event information like SQL Profiler / SQL Trace
• More information plus you can now configure easier
• When events are triggered• They can be sent to a target for further analysis
• Introduced in SQL Server 2008• Very complex to code and read (parse xml)
• Much Improved in 2012 with many more Events
GUI for XE
• SQL 2012+ has a GUI included in SSMS
• SQL 2008 does not• Get one from http://extendedeventmanager.codeplex.com/
• Much easier, makes XE usable in SQL 2008
5
Key Terms
• Session – collection of events with actions and targets
• Package – can be sqlserver objects or sqlos objects
• Event – an instrumented piece of code within SQL Server or Windows O/S, e.g. sql_statement_completed
• Action – what should be done when an event fires, e.g. collect T-SQL code, wait information, plans
• Target – a place to store the event data, e.g. ring buffer in memory and file in O/S
• Predicates – allows selective collection of events, e.g. do not capture system process information
DDL for XE
• 2008 - DDL statements that create / modify Extended Events sessions• CREATE EVENT SESSION
• Creates an extended event session object• Identifies Source of the events, Targets, and Parameters
• ALTER EVENT SESSION • Starts/stops an event session or changes an event session
configuration
• DROP EVENT SESSION • Drops an event session
• DMVs / Catalog views show session data & metadata• Use TSQL statements to get information on every extended events
session that is created
Creation Session DDL Example
CREATE EVENT SESSION [Deadlocks] ON SERVER
ADD EVENT sqlserver.xml_deadlock_report(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_id,sqlserver.session_id,sqlserver.sql_text,sqlserver.tsql_stack,sqlserver.username))
ADD TARGET package0.event_file(SET filename=N'C:\temp\deadlocks.xel',max_file_size=(10))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)
GO
Catalog Views for XE
• sys.server_event_sessions – session definitions
• sys.server_event_session_events – events to collect
• sys.server_event_session_actions – actions to collect
• sys.server_event_session_fields – list of data to collect
• sys.server_event_session_targets – where to store the collected data
DMVs for XE
• sys.dm_xe_sessions – session that have been created
• sys.dm_xe_session_events
• sys.dm_xe_session_event_actions
• sys.dm_xe_session_object_columns
• sys.dm_xe_session_targets
GUI Walkthrough
• To create a new session can use either a wizard manually go through screens
system_health Session
• Default session used to collect deadlocks, waits, errors and other information
• Started by Default
• Collects 17 Events into Ring Buffer
• Suggest Adding File Target
• Do not modify, instead create other sessions to customize events and actions
system_health General
system_health Events – Select
List of All Events
Chosen Events
system_health Events – Configure Chosen Events Event
Options
system_health Events – Predicates Chosen Events Filters or
Predicates
system_health Targets
Targets
system_health Advanced
Data Loss
Write to File after 2 min or
4MB in memory
system_health View Data
system_health View Data
system_health Filter Data
system_health Data Details
Deadlock Monitoring
with system_health Session
system_health Deadlock Graph
• The system_health session monitors deadlocks by default
• Can use it to see deadlock graph
Collecting SQL Statement and Wait
With Custom Session
XE Session for SQLs and Waits
• Fields defined the default data to collect when the highlighted event fires
• These change based on the highlighted event
XE Session – Global Fields
• Events of when a SQL (sproc or adhoc) or wait (internal or external) completes
• Global Fields tab defines the optional data that gets collected when the event fires
XE Session – Filters
• Define the sessions to watch• Do not collect SPIDs doing something in system databases
• Do not collect data for background sessions
• Collect for 1 out of 5 sessions to reduce load on SQL Server
• Collect if the duration is >= 0.1 seconds
XE Session – Data Storage
• File – longer term storage of data• Specify where to store them, how large and retention
• Can query it using sys.fn_xe_file_target_read_file
• Ring Buffer – shorter term storage in memory
XE Session – Starting
• Can manually start when needed• Also an option to start automatically when instance starts
• Can export a script for creation on other instances
• Modify it with Properties option
Response Time Analysis
• Now that we have data, what do we do with it?
• Can analyze from Management Studio• Right-Click on the file output and use View Target Data
Analysis – Sort, Group, Modify
• Left click on any column to sort
• Right click on columns to group and aggregate• For example, right click on query_hash and group by it
• Right click on duration column and sum it by query_hash
• Can also add/remove columns to display
Analysis - Filtering
• Having problems with a specific application or database• Filter the response time data by those columns
• Can also filter by a point in time when problem was occurring
Analysis - Filtering
Filter by a point in time
Filter by any collected value
Capturing Actual Plans
Capture Actual Plan
• Use query_post_execution_showplan event
• Collect the SQL text action as well
• It collects actual plan information immediately after a SQL executes
• Much like SSMS actual plan collection
XE Event Configuration
Showing the Actual Plan
• Note the Actual Number of Rows along with Estimated Rows
• This allows us to know which plan was really used for a query
Analysis - Queries
• Can also analyze the data by using XML queries
• Read data from the XE files using sys.fn_xe_file_target_read_file
• Many queries on the web, but my favorite is from Jeremiah Peschka on brentozar.com
• If you are using Ring Buffer output, can also query against that• Data is aged out much quicker
• There are limitations as noted by Jonathan Keyhais on sqlskills.com
Summary
• Extended Events are light weight
• Quickly / continuously gather information
• 2012+ - Easy to capture, store and view data• Via Sessions, Events, Actions, Filters, & Targets
• Can be used to troubleshoot issues
• Replaces Profiler
• Deprecated???
• Still need to use for Trace Capture of Analysis Services
• Replaces SQL Trace
• Stored procedures, functions and catalog views