当前位置:网站首页>实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
2022-07-07 18:12:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
--假设已经存在Event Session删除
IF EXISTS (SELECT * FROM sys.server_event_sessions WHERE name='MonitorLongQuery')
DROP EVENT SESSION MonitorLongQuery ON SERVER
GO
--创建Extended Event session
CREATE EVENT SESSION MonitorLongQuery ON SERVER
--添加Event(SQL完毕事件)
ADD EVENT sqlserver.sql_statement_completed
(
--指定收集的Event信息
ACTION
(
sqlserver.database_id,
sqlserver.session_id,
sqlserver.username,
sqlserver.client_hostname,
sqlserver.sql_text,
sqlserver.tsql_stack
)
--Filter信息(CPU超过或者整个执行时间超过10S)
WHERE sqlserver.sql_statement_completed.cpu> 10000
OR sqlserver.sql_statement_completed.duration> 10000
)
--指定收集的Event信息储存位置(能够存储到内存也能够到文件)
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
--启动Event Session捕获数据
ALTER EVENT SESSION MonitorLongQuery
ON SERVER STATE = START
GO
--查询
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
-停掉Event Session
ALTER EVENT SESSION MonitorLongQuery
ON SERVER STATE = STOP
GO
--删除Event Session
IF EXISTS(SELECT * FROM sys.server_event_sessions WHERE name='MonitorLongQuery')
DROP EVENT SESSION MonitorLongQuery ON SERVER
GO
------------将XML转换为常规的表格式
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
--获取cpu
SET @cpu = @xmlData.query('//data[@name="cpu"]/value').value('(value)[1]',
'INT')
--获取database_id
SET @database_id = @xmlData.query('//action[@name="database_id"]/value').value('(value)[1]',
'INT')
--获取username
SET @username = @xmlData.query('//action[@name="username"]/value').value('(value)[1]',
'NVARCHAR(100)')
--获取hostname
SET @client_hostname = @xmlData.query('//action[@name="client_hostname"]/value').value('(value)[1]',
'NVARCHAR(100)')
--获取sql_text
SET @sql_text = @xmlData.query('//action[@name="sql_text"]/value').value('(value)[1]',
'NVARCHAR(MAX)')
--開始插入数据
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
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116529.html原文链接:https://javaforall.cn
边栏推荐
- 力扣 2315.统计星号
- Sword finger offer II 013 Sum of two-dimensional submatrix
- torch.nn.functional.pad(input, pad, mode=‘constant‘, value=None)记录
- BI的边界:BI不适合做什么?主数据、MarTech?该如何扩展?
- 剑指 Offer II 013. 二维子矩阵的和
- Opencv learning notes high dynamic range (HDR) imaging
- How to implement safety practice in software development stage
- 九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
- Implement secondary index with Gaussian redis
- The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
猜你喜欢
Detailed explanation of Flink parallelism and slot
Openeuler prize catching activities, to participate in?
PHP method of obtaining image information
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
Implement secondary index with Gaussian redis
With st7008, the Bluetooth test is completely grasped
一. 基础概念
vulnhub之tre1
力扣 2319. 判断矩阵是否是一个 X 矩阵
openEuler 资源利用率提升之道 01:概论
随机推荐
How to cooperate among multiple threads
Traversée des procédures stockées Oracle
力扣 2319. 判断矩阵是否是一个 X 矩阵
浅尝不辄止系列之试试腾讯云的TUIRoom(晚上有约,未完待续...)
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
EasyGBS级联时,上级平台重启导致推流失败、画面卡住该如何解决?
MRS离线数据分析:通过Flink作业处理OBS数据
[sword finger offer] sword finger offer II 012 The sum of left and right subarrays is equal
Oracle 存储过程之遍历
MIT科技评论文章:围绕Gato等模型的AGI炒作可能使人们忽视真正重要的问题
Mongodb由浅入深学习
数据孤岛是企业数字化转型遇到的第一道险关
深度学习模型压缩与加速技术(七):混合方式
有用的win11小技巧
剑指 Offer II 013. 二维子矩阵的和
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
力扣 1037.有效的回旋镖
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
Force buckle 912 Sort array
Creation of kubernetes mysql8