有些時候,有些作業遇到問題執行時間過長,因此我寫了一個腳本可以根據歷史記錄,找出執行時間過長的作業,在監控中就可以及時發現這些作業並儘早解決,代碼如下: SELECT sj.name , sja.start_execution_date,DATEDIFF (SECOND ,sja.start_exe...
有些時候,有些作業遇到問題執行時間過長,因此我寫了一個腳本可以根據歷史記錄,找出執行時間過長的作業,在監控中就可以及時發現這些作業並儘早解決,代碼如下:
SELECT sj.name
, sja.start_execution_date,DATEDIFF (SECOND ,sja.start_execution_date,GETDATE() ) AS ExecutedMin,ja.AvgRuntimeOnSucceed
FROM msdb.dbo.sysjobactivity AS sja
INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id INNER
join
(
SELECT job_id,
AVG
((run_duration/10000 * 3600) + ((run_duration%10000)/100*60) + (run_duration%10000)%100)
+
NULLIF(0,STDEV
((run_duration/10000 * 3600) + ((run_duration%10000)/100*60) + (run_duration%10000)%100)) AS 'AvgRuntimeOnSucceed'
FROM msdb.dbo.sysjobhistory
WHERE step_id = 0 AND run_status = 1
GROUP BY job_id) ja
ON sj.job_id = ja.job_id
WHERE sja.start_execution_date IS NOT NULL --作業有開始
AND sja.stop_execution_date IS NULL --作業沒結束
AND sja.start_execution_date>DATEADD(DAY,-2,GETDATE()) --作業2天內開始
-- AND DATEDIFF (SECOND ,sja.start_execution_date,GETDATE() )>ja.AvgRuntimeOnSucceed *1.5 --作業執行時間比歷史平均時間超了50%"
如果作業經常出現問題,可以使用該腳本監控儘早發現問題。