Many presentations I have seen about SQL Server extended events focus on the mechanics of setting them up and querying the definitions back from the DMVs. While useful, I had always been missing the points about when and how to use extended events. What are they and why should you be using them? This presentation will show three real-world examples of using extended events. Each example will include demos on configuring the event and using the data and you will learn:
• Use extended events rather than queries against DMVs or tracing
• Collect query information
• Catch and examine deadlocks
• Collect actual plans
2. Who Am I
• 20+ years of experience in SQL Server and Oracle
• Speak at many user groups throughout US
• Owner of DRConsulting
• Dean.Richards@databaseperformance.guru
• @ConfioDean
• Focus on application and database performance
• Review performance for hundreds of customers per year
• Common question – how do I do performance tuning?
3. 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
4. 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
5. 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
6. 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
7. 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
8. Creation Session DDL Example
CREATE EVENT SESSION [Deadlocks] ON SERVER
ADD EVENT sqlserver.xml_deadlock_report(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,s
qlserver.database_id,sqlserver.session_id,sqlserver.sql_text,sqls
erver.tsql_stack,sqlserver.username))
ADD TARGET package0.event_file(SET
filename=N'C:tempdeadlocks.xel',max_file_size=(10))
WITH (MAX_MEMORY=4096
KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,M
AX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0
KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,
STARTUP_STATE=OFF)
GO
9. 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
10. 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
11. GUI Walkthrough
• To create a new session can use either a wizard manually
go through screens
12. 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
26. 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
27. 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
28. 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
29. 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
30. 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
31. 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
32. 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
33. 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
36. 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
38. 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
39. 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
40. 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
Editor's Notes
Full link to Jeremiah’s blog on this subject with queries you can use:
http://www.brentozar.com/archive/2014/04/collecting-detailed-performance-measurements-extended-events/
Full link to Jonathan Keyhais article on ring buffer issues
https://www.sqlskills.com/blogs/jonathan/why-i-hate-the-ring_buffer-target-in-extended-events/