web-dev-qa-db-ja.com

1つのジョブの各ステップの実行時間を分単位で取得するにはどうすればよいですか?

Run_Duration完了したジョブのsysjobhistory内。これを取得する方法はありますか? [msdb].[dbo].[sysjobactivity]には最後のステップしかありません。

1
Michael

これで始められるはずです...

select 
    jobs.name
    ,jobs.description
    ,steps.step_id
    ,steps.step_name
    ,steps.last_run_outcome
    ,last_run_time = stuff(stuff(right('00000' + cast(steps.last_run_time as varchar),6),3,0,':'),6,0,':')
    ,last_run_duration = stuff(stuff(right('00000' + cast(steps.last_run_duration as varchar),6),3,0,':'),6,0,':')
from [msdb].[dbo].[sysjobs] jobs
inner join [msdb].[dbo].[sysjobsteps] steps on
steps.job_id = jobs.job_id
order by jobs.job_id, steps.step_id
3
scsimon

この場合、[sysjobHistory]にすべてのデータが保存されている[sysjobsteps]を使用します。これは秒単位の期間です。私のおすすめ:

SELECT top 1000 j.name as JobName, step_id, jh.step_name StepName, CONVERT(CHAR(10), CAST(STR(jh.run_date,8, 0) AS dateTIME), 112) as RunDate, STUFF(STUFF(RIGHT('000000' + CAST ( jh.run_time AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') RunTime, STUFF(STUFF(RIGHT('000000' + CAST ( jh.run_duration AS VARCHAR(6 ) ) ,6),5,0,':'),3,0,':') RunDuration, case jh.run_status when 0 then 'failed' when 1 then 'Succeded' when 2 then 'Retry' when 3 then 'Cancelled' when 4 then 'In Progress' end as ExecutionStatus, FROM sysjobhistory jh inner join sysjobs j ON j.job_id = jh.job_id ORDER BY j.name, jh.run_date, jh.run_time

2
Michael

おかげで、それは非常に役立ちました。期間は秒単位ではないため、HHMMSSにある小さなバグがあります。

私はそれをこのように修正しました:

SELECT x.executable_name
, x.start_time
, x.end_time AS end_time
, datediff(minute, x.start_time, x.end_time) AS Duration
FROM (
SELECT DISTINCT --TOP 100 PERCENT 
    cast([sJOB].[job_id] AS VARCHAR(max)) AS execution_id
    , [sJSTP].[step_name] AS executable_name
    , [sJOB].[name] AS package_name
    , CASE [sJSTP].[run_date]
        WHEN 0
            THEN NULL
        ELSE CAST(CAST([sJSTP].[run_date] AS CHAR(8)) + ' ' + STUFF(STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6), 3, 0, ':'), 6, 0, ':') AS DATETIME)
        END AS start_time
    , dateadd(ss, --run_duration

  substring(cast(100000000 + [sJSTP].run_duration AS NVARCHAR), 2, 4) * 60*60 -- Hours
+ substring(cast(100000000 + [sJSTP].run_duration AS NVARCHAR), 6, 2) * 60 -- Minutes
+ substring(cast(100000000 + [sJSTP].run_duration AS NVARCHAR), 8, 2)  -- Seconds

    , CASE [sJSTP].[run_date]
            WHEN 0
                THEN NULL
            ELSE CAST(CAST([sJSTP].[run_date] AS CHAR(8)) + ' ' + STUFF(STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6), 3, 0, ':'), 6, 0, ':') AS DATETIME)
            END) end_time
    --    , [sJSTP].[run_duration] [looptijd in minuten]
    , CASE [sJSTP].[run_status]
        WHEN 0
            THEN 'Failed'
        WHEN 1
            THEN 'Success'
        WHEN 2
            THEN 'Retry'
        WHEN 3
            THEN 'Cancelled'
        WHEN 5
            THEN 'Unknown'
        END AS execution_result_description
FROM [msdb].[dbo].[sysjobhistory] AS [sJSTP]
INNER JOIN [msdb].[dbo].[sysjobs] AS [sJOB]
    ON [sJSTP].[job_id] = [sJOB].[job_id]
INNER JOIN [msdb].[dbo].[sysjobsteps] steps
    ON [sJSTP].[job_id] = [steps].[job_id]
WHERE [sJSTP].[run_date] <> 0
    AND CASE [sJSTP].[run_date]
        WHEN 0
            THEN NULL
        ELSE CAST(CAST([sJSTP].[run_date] AS CHAR(8)) + ' ' + STUFF(STUFF(RIGHT('000000' + CAST([sJSTP].[run_time] AS VARCHAR(6)), 6), 3, 0, ':'), 6, 0, ':') AS DATETIME)
        END BETWEEN dateadd(day, - 20, getdate())
        AND getdate()
    AND [sJSTP].[step_name] NOT IN ('(Job outcome)')
    AND [sJOB].name = '<JOB Name>'
    AND [sJSTP].[step_name] = '<Step Name>'
    --order by start_time desc
) x
ORDER BY start_time DESC
1
Sascha Müller