Practical examples of using extended events

40
Extended Events Practical Examples Dean Richards

Transcript of Practical examples of using extended events

Page 1: Practical examples of using extended events

Extended EventsPractical Examples

Dean Richards

Page 2: Practical examples of using extended events

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?

Page 3: Practical examples of using extended events

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

Page 4: Practical examples of using extended events

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

Page 5: Practical examples of using extended 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

Page 6: Practical examples of using extended events

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

Page 7: Practical examples of using extended events

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

Page 8: Practical examples of using extended events

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

Page 9: Practical examples of using extended events

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

Page 10: Practical examples of using extended events

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

Page 11: Practical examples of using extended events

GUI Walkthrough

• To create a new session can use either a wizard manually go through screens

Page 12: Practical examples of using extended events

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

Page 13: Practical examples of using extended events

system_health General

Page 14: Practical examples of using extended events

system_health Events – Select

List of All Events

Chosen Events

Page 15: Practical examples of using extended events

system_health Events – Configure Chosen Events Event

Options

Page 16: Practical examples of using extended events

system_health Events – Predicates Chosen Events Filters or

Predicates

Page 17: Practical examples of using extended events

system_health Targets

Targets

Page 18: Practical examples of using extended events

system_health Advanced

Data Loss

Write to File after 2 min or

4MB in memory

Page 19: Practical examples of using extended events

system_health View Data

Page 20: Practical examples of using extended events

system_health View Data

Page 21: Practical examples of using extended events

system_health Filter Data

Page 22: Practical examples of using extended events

system_health Data Details

Page 23: Practical examples of using extended events

Deadlock Monitoring

with system_health Session

Page 24: Practical examples of using extended events

system_health Deadlock Graph

• The system_health session monitors deadlocks by default

• Can use it to see deadlock graph

Page 25: Practical examples of using extended events

Collecting SQL Statement and Wait

With Custom Session

Page 26: Practical examples of using extended events

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

Page 27: Practical examples of using extended events

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

Page 28: Practical examples of using extended events

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

Page 29: Practical examples of using extended events

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

Page 30: Practical examples of using extended events

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

Page 31: Practical examples of using extended events

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

Page 32: Practical examples of using extended events

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

Page 33: Practical examples of using extended events

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

Page 34: Practical examples of using extended events

Analysis - Filtering

Filter by a point in time

Filter by any collected value

Page 35: Practical examples of using extended events

Capturing Actual Plans

Page 36: Practical examples of using extended events

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

Page 37: Practical examples of using extended events

XE Event Configuration

Page 38: Practical examples of using extended events

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

Page 39: Practical examples of using extended events

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

Page 40: Practical examples of using extended events

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