当前位置:网站首页>Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]

Actual combat: sqlserver 2008 Extended event XML is converted to standard table format [easy to understand]

2022-07-07 21:20:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

-- The hypothesis already exists Event Session Delete   
  
IF EXISTS (SELECT * FROM sys.server_event_sessions WHERE name='MonitorLongQuery')  
DROP EVENT SESSION MonitorLongQuery ON SERVER  
GO   
  
-- establish Extended Event session  
  
CREATE EVENT SESSION MonitorLongQuery ON SERVER  
-- add to Event(SQL Completion event )  
ADD EVENT sqlserver.sql_statement_completed  
(   
-- Specify collected Event Information   
ACTION   
(   
sqlserver.database_id,  
sqlserver.session_id,  
sqlserver.username,  
sqlserver.client_hostname,  
sqlserver.sql_text,  
sqlserver.tsql_stack   
)   
  
--Filter Information (CPU Exceed or the whole execution time exceeds 10S)  
  
WHERE sqlserver.sql_statement_completed.cpu> 10000  
OR sqlserver.sql_statement_completed.duration> 10000  
)   
-- Specify collected Event Information storage location ( It can be stored in memory or in files )  
ADD TARGET package0.asynchronous_file_target  
(   
SET FILENAME = N's:\monitor\LogQuery.xet',  
METADATAFILE = 'S:\monitor\LongQuery.xem'  
)   
GO   
  
SELECT sessions.name AS SessionName,sevents.package as PackageName,  
sevents.name AS EventName,  
sevents.predicate, sactions.name AS ActionName, stargets.name AS TargetName   
FROM sys.server_event_sessions sessions  
INNER JOIN sys.server_event_session_events sevents  
ON sessions.event_session_id= sevents.event_session_id  
INNER JOIN sys.server_event_session_actions sactions  
ON sessions.event_session_id= sactions.event_session_id  
INNER JOIN sys.server_event_session_targets stargets  
ON sessions.event_session_id= stargets.event_session_id  
WHERE sessions.name='MonitorLongQuery'  
GO   
  
  
-- start-up Event Session Capture data   
  
ALTER EVENT SESSION MonitorLongQuery   
ON SERVER STATE = START   
GO  
  
-- Inquire about   
  
SELECT CAST(event_data AS XML) event_data,*  
FROM sys.fn_xe_file_target_read_file  
  
('s:\monitor\LogQuery_0_129954478780290000.xet',  
 's:\monitor\LongQuery_0_129954478780330000.xem',NULL,NULL)   
go  
  
  
  
- Stop Event Session  
  
ALTER EVENT SESSION MonitorLongQuery   
  
ON SERVER STATE = STOP  
  
GO   
  
   
  
-- Delete Event Session  
  
IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='MonitorLongQuery')  
  
DROP EVENT SESSION MonitorLongQuery ON SERVER  
  
GO   


------------ take XML Convert to regular table format  
IF EXISTS ( SELECT  *
            FROM    tempdb.dbo.sysobjects
            WHERE   id = OBJECT_ID(N'tempdb..#MyData')
                    AND type = 'U' ) 
    DROP TABLE #MyData
go

CREATE TABLE #MyData
    (
      database_id INT NOT NULL ,
      username NVARCHAR(100) NOT NULL,
      client_hostname NVARCHAR(100) NOT NULL,
      sql_text NVARCHAR(MAX) NOT NULL ,
      cpu INT NOT NULL
    )
go


DECLARE @xmlData XML
DECLARE @xmlString NVARCHAR(MAX)
DECLARE @database_id INT
DECLARE @username NVARCHAR(100)
DECLARE @client_hostname NVARCHAR(100)
DECLARE @sql_text NVARCHAR(MAX)
DECLARE @cpu INT

DECLARE myCur CURSOR READ_ONLY
FOR
SELECT TOP 200 event_data --CAST(event_data AS XML)
FROM sys.fn_xe_file_target_read_file

('s:\monitor\LogQuery_0_130638808366940000.xet',
 's:\monitor\LongQuery_0_130638808366940000.xem',NULL,NULL) 

OPEN myCur

FETCH NEXT FROM myCur INTO @xmlString

WHILE @@FETCH_STATUS = 0 

BEGIN
		 BEGIN TRY
		    SET @xmlData = CAST(@xmlString AS XML)
		    --set @cpu = 0
		    -- obtain cpu														  
			SET @cpu = @xmlData.query('//data[@name="cpu"]/value').value('(value)[1]',
																  'INT')
				    
		    -- obtain database_id
			SET @database_id = @xmlData.query('//action[@name="database_id"]/value').value('(value)[1]',
																  'INT')
			-- obtain username												  
            SET @username = @xmlData.query('//action[@name="username"]/value').value('(value)[1]',
																  'NVARCHAR(100)')
			-- obtain hostname												  
            SET @client_hostname = @xmlData.query('//action[@name="client_hostname"]/value').value('(value)[1]',
																  'NVARCHAR(100)')
																  
			-- obtain sql_text
			SET @sql_text = @xmlData.query('//action[@name="sql_text"]/value').value('(value)[1]',
														  'NVARCHAR(MAX)')
			

			-- Start inserting data 
			INSERT #MyData
					( database_id, 
					 sql_text, 
					 username,
					  client_hostname,
					  cpu )
			VALUES  ( @database_id, -- database_id - int
					  @sql_text,  -- sql_text - nvarchar(max)
					  @username,
					  @client_hostname,
					  @cpu
					  )
		 END TRY
		 BEGIN CATCH
		 END CATCH
		
		
        FETCH NEXT FROM myCur INTO @xmlString
END
CLOSE myCur
DEALLOCATE myCur



SELECT b.name,a.username,a.client_hostname,a.sql_text,a.cpu FROM #MyData AS a
inner join sys.databases as b
on a.database_id=b.database_id
order by a.cpu desc
go

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116529.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071810287408.html