2017-06-20 76 views
-3

我需要執行的存儲過程統計信息:邏輯讀取,物理讀取,持續時間,寫入,行計數。或者,也許我們可以簡化這一點。它足以擁有相同的會話數據。就像SET STATISTICS IO ON/OFF一樣,但是,我需要將結果保存到表格中。所以,SET STATISTICS IO不是選項。 這裏的任何知識?SQL Server(2012+)執行的過程統計信息

我希望是這樣的:

EXEC [my_proc]; 

INSERT INTO [sp_exec_stats] 
([lreads], [preads], ..., [rcount]) 
VALUES(?, ?, ..., ?); 
+0

可能重複:https://dba.stackexchange.com/questions/16493/tracking-stored-procedure-usage –

+0

感謝downvotes,但它不是重複的。請進一步探討這個問題。 – Juozas

+0

請提供一些示例輸入,預期輸出以及您嘗試過的任何內容。這就是我覺得它是重複的原因。 –

回答

0

最後,我發現解決方案由我自己。感謝我的友好社區。

對於真正瞭解問題的同事。請使用此方法(整個測試腳本「選擇」部分是穴):

CREATE PROCEDURE [my_dummy_sp] 
    @rowcount INT OUTPUT 
AS 
BEGIN 

    SELECT TOP 10 * FROM [sys].[objects]; 
    SET @rowcount = @@ROWCOUNT; 

    RETURN 0; 

END;  

GO 


DECLARE @sp_exec_stats TABLE 
(
    [rec_id]   BIGINT IDENTITY(1, 1) 
    ,[sp_name]   NVARCHAR(256) 
    ,[db_name]   NVARCHAR(256) 
    ,[sys_dt]   DATETIMEOFFSET(7) 
    ,[logical_reads] INT 
    ,[physical_reads] INT 
    ,[duration]   INT 
    ,[writes]   INT 
    ,[rowcount]   INT 
    ,[cpu_time]   INT 
); 

DECLARE @rowcount INT; 

EXECUTE [my_dummy_sp] 
    @rowcount = @rowcount OUTPUT; 

INSERT INTO @sp_exec_stats 
(
    [sp_name] 
    ,[db_name]   
    ,[sys_dt]   
    ,[logical_reads]  
    ,[physical_reads] 
    ,[duration]   
    ,[writes]   
    ,[rowcount] 
    ,[cpu_time]  
) 
SELECT TOP 1 
    [sp_name]   = 'my_dummy_sp' 
    ,[db_name]   = DB_NAME()  
    ,[sys_dt]   = [last_execution_time] 
    ,[logical_reads] = [last_logical_reads] 
    ,[physical_reads] = [last_physical_reads] 
    ,[duration]   = [last_elapsed_time] 
    ,[writes]   = [last_logical_writes] 
    ,[rowcount]   = @rowcount 
    ,[cpu_time]   = [last_worker_time]  
FROM 
    [sys].[dm_exec_procedure_stats] 
WHERE 
     [object_id]  = OBJECT_ID('my_dummy_sp')  
ORDER BY 
    [last_execution_time] DESC; 


DROP PROCEDURE [my_dummy_sp]; 
SELECT * FROM @sp_exec_stats; 
相關問題