sys.fn_xe_file_target_read_file()DMF结果的扩展事件强制顺序

时间:2013-09-10 09:38:04

标签: sql sql-server-2008 sql-server-2008-r2 monitoring extended-events

  

Microsoft SQL Server 2008 R2(SP2) - 10.50.4000.0(X64)2012年6月28日   08:36:30版权所有(c)Microsoft Corporation Enterprise Edition   Windows NT 6.1(Build 7601:Service Pack 1)上的(64位)

使用sys.fn_xe_file_target_read_file()DMF读取扩展事件.xel日志文件时,有没有办法保证结果的顺序?理想情况下,我想根据它们插入.xel文件的顺序对记录1..X进行编号。

我需要多次轮询文件目标并将新记录插入表中。如果我可以保证结果的顺序,那么我可以添加元数据列“条目号”,并使用此列来避免在多次执行中插入相同的记录,即

SELECT  entries.EntryNumber
        ,entries.file_name
        ,entries.data
        ,entries.file_offset
FROM    (
        SELECT  ROW_NUMBER() OVER (ORDER BY <GUARANTEE ORDERING BASED ON ORDER OF INSERTION>) AS EntryNumber,
                file_name,
                CONVERT (XML, event_data) AS data
                ,file_offset
        FROM    sys.fn_xe_file_target_read_file ('C:\ExEvent_0_123456789.xel', 'C:\ExEvent_0_123456789.xem', NULL, NULL)
                ) entries
WHERE   NOT EXISTS 
        (
        SELECT  1
        FROM    dbo.ExEventLog d
        WHERE   entries.EntryNumber = d.EntryNumber                         
        )

如果无法做到这一点,我是否可以保证所有具有相同“file_offset”值的记录在一个批处理中加载到.xel文件中?例如,如果我在.xel文件中查询“file_offset”为123的所有记录,我可以保证随后不会加载“file_offset”为123的其他记录吗?

这是基于创建扩展事件WITH MAX_DISPATCH_LATENCY = 1 SECONDS并每60秒轮询一次.xel文件。

由于此错误/功能,我觉得使用@initial_file_name和@initial_offset参数感觉不舒服:http://connect.microsoft.com/SQLServer/feedback/details/714286/sys-fn-file-target-read-file-reads-incorrect-files-when-offset-reading#details

2 个答案:

答案 0 :(得分:1)

您可以使用timestamp中根节点的event_data属性。

SELECT  entries.EntryNumber
        ,entries.file_name
        ,entries.data
        ,entries.file_offset
FROM    (
        SELECT  ROW_NUMBER() OVER (ORDER BY X.data.value('(/event/@timestamp)[1]', 'datetime')) AS EntryNumber
                ,file_name
                ,X.data
                ,file_offset
        FROM    sys.fn_xe_file_target_read_file ('C:\ExEvent_0_123456789.xel', 'C:\ExEvent_0_123456789.xem', NULL, NULL)
          CROSS APPLY (SELECT CONVERT(XML, event_data) AS data) AS X
        ) entries

答案 1 :(得分:0)

如果您想根据插入顺序进行保证订购,请使用带有标识(int,1,1)列的临时表:

SELECT  
        file_name
        ,CONVERT (XML, event_data) AS data
        ,file_offset
        ,identity(int, 1, 1) AS EntryNumber
into #tmp
FROM    sys.fn_xe_file_target_read_file ('C:\ExEvent_0_123456789.xel', 'C:\ExEvent_0_123456789.xem', NULL, NULL)

SELECT  entries.EntryNumber
    ,entries.file_name
    ,entries.data
    ,entries.file_offset
FROM #tpm entries
WHERE   NOT EXISTS 
    (
    SELECT  1
    FROM    dbo.ExEventLog d
    WHERE   entries.EntryNumber = d.EntryNumber                         
    )

DROP TABLE #tmp

Row_Number并不意味着这样做。