Logo
HOW TO
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Server

SQL Server 2008 R2 : Performance Monitoring Tools (part 9) - Creating an Extended Events Session

5/10/2013 6:11:59 PM
Creating an Extended Events Session

Microsoft uses the same common paradigm (CREATE, ALTER, DROP) for most of its newer capabilities, such as Create Audit.. (for SQL Auditing), Create Endpoint.. (for database mirroring), and many others. SSEE follows the same path in that you basically create an Extended Events session, alter it to start the monitoring session, alter it again to stop the monitoring, and then leverage the catalog and dynamic management views before, during, and after the session monitoring to view the event information.

Creating events (event sessions) is fairly easy to do using the CREATE, ALTER and DROP EVENT statements. These Data Definition Language (DDL) statements completely control the creation and activation of Extended Events. All the SSEE objects are created in the msdb database. Only those users with CONTROL SERVER permissions can create, alter, or drop SSEE objects. To use the catalog and dynamic management views, you need at least VIEW SERVER STATE permission.

In this section, you quickly set up and define a new Extended Events session object that includes operating system IO requests and SQL Server lock-acquired counts. The purpose is to isolate the database objects (tables) that are being hit hardest with locks to better understand the behavior of the database design.

Locate this SQL script now; then start SSMS and open a new query connection with the CREATE EVENT SESSION script in it. Figure 18 shows a current connection to SQL Server with TheMostLocks.sql file open and the event session named TheMostLocks ready to be created.

Figure 18. SSMS creating an event session named TheMostLocks.

As you examine the CREATE EVENT SESSION T-SQL code, notice that two events are being created with the ADD EVENT statements. One will gather async IO requests, and the other will retrieve SQL Server–acquired locks on an object’s information. Also, notice the TARGET statement, which uses a predefined target location that allows you to retrieve the results within SQL Server during execution and that filters on showing only the lock’s acquired information.

The CREATE EVENT SESSION T-SQL code is as follows:

IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='TheMostLocks')
DROP EVENT session TheMostLocks ON SERVER;

CREATE EVENT SESSION TheMostLocks
ON SERVER
       ADD EVENT sqlos.async_io_requested,
       ADD EVENT sqlserver.lock_acquired
       ADD TARGET package0.synchronous_bucketizer (
       SET filtering_event_name='sqlserver.lock_acquired',
source_type=0, source='resource_0')
       WITH (MAX_MEMORY=4MB, MAX_EVENT_SIZE=4MB);


					  

Alternatively, you can direct the TARGET output to a file by using the following TARGET statement, for example:

ADD TARGET package0.etw_classic_sync_target
 (SET default_etw_session_logfile_path  = N'C:\TEMP\EESessionFile.etl' )

You could then use the resulting file with the ETW to assemble this and other events from other portions of your environment.

After the event session is created, you are able to start it by using the ALTER EVENT SESSION command, generate some activity on your server that will be captured by the event session, view the dynamic results during the execution of the activity, and then stop the event session.

Go ahead and create the event session as it is listed now. Simply highlight the CREATE EVENT SESSION T-SQL statements shown previously and execute this code from SSMS. When it is complete, you are ready to start the event session by using the following command:

-- START EVENT SESSION
ALTER EVENT SESSION TheMostLocks ON SERVER STATE=start;

Using the AdventureWorks2008 database, you can generate some simple activity that is known to acquire share locks on tables. Use the following T-SQL that references the [Sales].[vSalesPersonSalesByFiscalYears] view:

--
-- Generate some lock acquiring workload
--
USE AdventureWorks2008;

SELECT TOP 1 * FROM [Sales].[vSalesPersonSalesByFiscalYears];

As soon as the preceding SELECT statement is generated, the Extended Event begins doing its job of collecting information. As you can see in Figure 19, this fairly complex join statement grabs the results of this Extended Event and displays them in a very nice readable fashion. 

Figure 19. Displaying the results of acquired locks from the Extended Events session.

As you can see, this session isolates the table objects that have the most acquired locks on them during execution. This capability is very powerful.

The T-SQL code is as follows:

-------------------------------------------------------------------
-- The following query turns the xml data that is accumulating   --
-- from the Extended Event Session and displays it more clearly  --
--  TableName, TableObjectID, and number of AcquiredLocks        --
-------------------------------------------------------------------
SELECT name      AS TableName,
       object_id AS TableObjectID,
       LocksX    AS AcquiredLocks
FROM (SELECT objstats.value('.','bigint') AS ObjectX,
        objstats.value('@count', 'bigint') AS LocksX
      FROM (SELECT CAST(xest.target_data AS XML)
      LockData
      FROM sys.dm_xe_session_targets xest
      JOIN sys.dm_xe_sessions xes ON xes.address = xest.event_session_address
      JOIN sys.server_event_sessions ses ON xes.name = ses.name
      WHERE xest.target_name = 'synchronous_bucketizer'
      AND xes.name = 'TheMostLocks'
            ) Locks
      CROSS APPLY LockData.nodes('//BucketizerTarget/Slot') AS T(objstats)
     ) LockedObjects
INNER JOIN sys.objects o
ON LockedObjects.ObjectX = o.object_id
WHERE o.type != 'S'
  AND o.type  = 'U'
ORDER BY LocksX desc;


					  

To turn off the Extended Events session, you simply issue another ALTER EVENT SESSION command with the STATE equal to stop, as shown here:

-- STOP EVENT SESSION
ALTER EVENT SESSION TheMostLocks
ON SERVER STATE=stop ;

Various Extended Events can be defined for monitoring purposes within the SQL Server environment, your application environment, and at the operating system level. You will likely build up a complete library of Extended Events that represent what you are most interested in monitoring about your environment. They will then become valuable tools for years to come. We also expect forums and Microsoft to create many templates of Extended Events to aid you in creating this extensive library of monitoring capability.

Other -----------------
- Microsoft Systems Management Server 2003 : Package Distribution and Management - Monitoring Status
- Microsoft Systems Management Server 2003 : Configuring the Client (part 4) - Managing the Advanced Client Download Cache, Advertised Programs Process Flow
- Microsoft Systems Management Server 2003 : Configuring the Client (part 3)
- Microsoft Systems Management Server 2003 : Configuring the Client (part 2) - Running Advertised Programs on Clients - Advertised Programs Wizard
- Microsoft Systems Management Server 2003 : Configuring the Client (part 1)
- Microsoft Systems Management Server 2003 : Package Distribution and Management - Creating an Advertisement
- SharePoint 2010 : Connecting and Disconnecting Servers with Windows PowerShell, Additional Functionality in SharePoint 2010
- Microsoft Dynamics CRM 4 : Digital Phone Integration (part 3) - c360 CTI for Microsoft CRM
- Microsoft Dynamics CRM 4 : Digital Phone Integration (part 2) - Cisco Unified CallConnector for Microsoft Dynamics CRM
- Microsoft Dynamics CRM 4 : Digital Phone Integration (part 1)
 
 
REVIEW
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
 
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
 
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
Top 10
- Microsoft Excel : How to Use the VLookUp Function
- Fix and Tweak Graphics and Video (part 3) : How to Fix : My Screen Is Sluggish - Adjust Hardware Acceleration
- Fix and Tweak Graphics and Video (part 2) : How to Fix : Text on My Screen Is Too Small
- Fix and Tweak Graphics and Video (part 1) : How to Fix : Adjust the Resolution
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro