Sql-Server

如何按 sp 執行時間(秒)過濾擴展事件會話?

  • May 15, 2022

嘗試查看執行時間超過給定秒數的命名儲存過程的所有執行。你如何過濾執行時間(秒)?這是我到目前為止拼湊起來的

CREATE EVENT SESSION [Slow SP Executions]
ON SERVER
   ADD EVENT sqlserver.module_end
   (SET collect_statement = (1)
    ACTION
    (
        sqlserver.host,                       
        sqlserver.database_name,  
        sqlserver.client_app_name, 
        sqlserver.session_server_principal_name, 
        sqlserver.username,                      
        sqlserver.sql_text,
        sqlserver.tsql_stack
    )
    WHERE (
              [object_type] = 'P ' 
              AND [sqlserver].[database_name] = N'MyDB'
              AND [object_name] = N'MySproc'              
          )
   )
   

這應該有效,持續時間過濾器以微秒為單位。

CREATE EVENT SESSION [captureProcDuration] ON SERVER 
ADD EVENT sqlserver.module_end(
   ACTION(
       sqlserver.client_app_name,
       sqlserver.client_hostname,
       sqlserver.database_name,
       sqlserver.server_principal_name,
       sqlserver.sql_text,
       sqlserver.tsql_stack)
   WHERE (
       [object_name] = N'testProcDuration'
       AND [package0].[greater_than_uint64]([duration], (5000)))
   )
ADD TARGET package0.event_file(
   SET filename = N'captureProcDuration', max_file_size = (256)
)
WITH (
   MAX_MEMORY = 4096 KB,
   EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS,
   MAX_DISPATCH_LATENCY = 30 SECONDS,
   MAX_EVENT_SIZE = 0 KB,
   MEMORY_PARTITION_MODE = NONE,
   TRACK_CAUSALITY = OFF,
   STARTUP_STATE = OFF
)
GO

我的測試有效的過程:

CREATE OR ALTER PROC testProcDuration
AS
BEGIN
   WAITFOR DELAY '00:00:07'
END

引用自:https://dba.stackexchange.com/questions/312112