我正在尝试编写脚本以将 sql server 中的扩展事件数据导入表中。
我目前正在使用以下查询,
select getdate() as [check_date],
xml.value('(./@name)[1]','nvarchar(60)') as [event_name],
dateadd(hour, 9, xml.value('(./@timestamp)[1]', 'datetime')) AS [timestamp],
xml.value('(data[@name="wait_type"]/text)[1]','nvarchar(max)') as [wait_type],
xml.value('(data[@name="opcode"]/text)[1]','nvarchar(max)') as [opcode],
xml.value('(data[@name="duration"]/value)[1]','bigint') as [duration],
xml.value('(data[@name="signal_duration"]/value)[1]','bigint') as [signal_duration],
xml.value('(data[@name="wait_resource"]/value)[1]','nvarchar(max)') as [wait_resource],
xml.value('(action[@name="client_app_name"]/value)[1]', 'nvarchar(max)') as [client_app_name],
xml.value('(action[@name="client_hostname"]/value)[1]', 'nvarchar(max)') as [client_hostname],
xml.value('(action[@name="database_id"]/value)[1]', 'int') as [database_id],
xml.value('(action[@name="server_principal_name"]/value)[1]', 'nvarchar(max)') as [server_principal_name],
xml.value('(action[@name="sql_text"]/value)[1]', 'nvarchar(max)') as [sql_text],
xml.value('(action[@name="attach_activity_id_xfer"]/value)[1]', 'char(36)') as [attach_activity_id_xfer],
xml.value('(action[@name="attach_activity_id"]/value)[1]', 'char(36)') as [attach_activity_id_guid] ,
substring(reverse(xml.value('(action[@name="attach_activity_id"]/value)[1]', 'nvarchar(max)')), 0,charindex('-', reverse(xml.value('(action[@name="attach_activity_id"]/value)[1]', 'nvarchar(100)')))) as [attach_activity_id_seq]
from [my_temporaly_xml_table_here] with (nolock)
CROSS APPLY target_xml.nodes('/RingBufferTarget/event[ @name=sql:variable("@p_event_name") and @timestamp > sql:variable("@p_lasttimestamp") ]') AS x(xml)
这些是我的索引,我决定使用选择性索引来提高性能,看起来使用选择性索引执行得很好,但结果并不好。此查询当前正在发生不可接受的逻辑读取。
好吧,当然数百个事件基本上是在生产服务器中触发的。
CREATE SELECTIVE XML INDEX sxidx_001
ON [my_temporaly_xml_table_here] (xml)
FOR
(
--eventnode = '/RingBufferTarget/event' as xquery 'node()',
eventdatanode = '/RingBufferTarget/event/data' as xquery 'node()',
eventactionnode = '/RingBufferTarget/event/action' as xquery 'node()',
dataname = '/RingBufferTarget/event/data/@name' as xquery 'xs:string' singleton,
actionname = '/RingBufferTarget/event/action/@name' as xquery 'xs:string' singleton,
timestamp= '/RingBufferTarget/event/@timestamp' as xquery 'xs:dateTime' singleton
)
有什么更好的性能建议吗?