查看作业详细情况
查看作业名称,执行频率,执行时间等。。。
DECLARE @WeekDays TABLE ( freq_interval INT, weekdays NVARCHAR(120) ) INSERT INTO @WeekDays SELECT 1 ,N'星期日 ' UNION ALL SELECT 2 ,N'星期一 ' UNION ALL SELECT 4 ,N'星期二 ' UNION ALL SELECT 8 ,N'星期三 ' UNION ALL SELECT 16 ,N'星期四 ' UNION ALL SELECT 32 ,N'星期五 ' UNION ALL SELECT 118 ,N'星期一,二,四,五,六 ' UNION ALL SELECT 122 ,N'星期一,三,四,五,六 ' UNION ALL SELECT 124 ,N'星期二,三,四,五,六 ' UNION ALL SELECT 63 ,N'星期日,一,二,三,四,五' UNION ALL SELECT 95 ,N'星期日,一,二,三,四,六' UNION ALL SELECT 111 ,N'星期日,一,二,三,五,六' UNION ALL SELECT 119 ,N'星期日,一,二,四,五,六' UNION ALL SELECT 123 ,N'星期日,一,三,四,五,六' UNION ALL SELECT 125 ,N'星期日,二,三,四,五,六' UNION ALL SELECT 126 ,N'星期一,二,三,四,五,六' UNION ALL SELECT 127 ,N'星期日,一,二,三,四,五,六'; SELECT d.job_id AS job_id , j.name AS job_name , CASE WHEN j.enabled =1 THEN N'启用' ELSE N'禁用' END AS job_staus, CASE WHEN freq_type=1 THEN N'运行一次' WHEN freq_type=4 THEN N'每天执行' WHEN freq_type=8 THEN N'每周执行' WHEN freq_type=16 THEN N'每月执行' WHEN freq_type=32 THEN N'每月执行' WHEN freq_type=64 THEN N'代理服务启动时运行' WHEN freq_type=128 THEN N'在计算机空闲时运行' END AS freq_type, CASE WHEN freq_type=1 THEN N'选项无意义' WHEN freq_type=4 THEN ( CASE WHEN freq_subday_type=4 THEN N'每隔' + CONVERT(NVARCHAR(4),freq_subday_interval) +N'分钟执行一次' WHEN freq_subday_type=8 THEN N'每隔' + CONVERT(NVARCHAR(4),freq_subday_interval) +N'小时执行一次' ELSE N'每天执行'+ CONVERT(NVARCHAR(4),freq_interval) + N'次' END ) WHEN freq_type=8 THEN ( SELECT w.weekdays FROM @WeekDays w WHERE w.freq_interval= s.freq_interval ) WHEN freq_type=16 THEN N'每月' + CONVERT(NVARCHAR(4),freq_interval) + N'号执行' WHEN freq_type=32 THEN N'每月星期' + CONVERT(NVARCHAR(4),freq_interval) + N'执行' END AS freq_relative_interval, CASE WHEN freq_subday_type =1 THEN N'指定时间点执行一次' WHEN freq_subday_type =2 THEN N'每隔:' + CAST(freq_subday_interval AS VARCHAR(2)) + N'秒执行一次' WHEN freq_subday_type =4 THEN N'每隔:' + CAST(freq_subday_interval AS VARCHAR(2)) + N'分执行一次' WHEN freq_subday_type =8 THEN N'每隔:' + CAST(freq_subday_interval AS VARCHAR(2)) + N'小时执行一次' END AS freq_subday_type, CASE WHEN freq_subday_type =1 THEN N'开始时间点:' + CAST(active_start_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N'分' WHEN freq_subday_type =2 THEN N'开始时间点:' + CAST(active_start_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N'分' WHEN freq_subday_type =4 THEN N'开始时间点:' + CAST(active_start_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N'分' WHEN freq_subday_type =8 THEN N'开始时间点:' + CAST(active_start_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N'分' END AS job_start_time, CASE WHEN freq_subday_type =1 THEN N'结束时间点:' + CAST(active_end_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N'分' WHEN freq_subday_type =2 THEN N'结束时间点:' + CAST(active_end_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N'分' WHEN freq_subday_type =4 THEN N'结束时间点:' + CAST(active_end_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N'分' WHEN freq_subday_type =8 THEN N'结束时间点:' + CAST(active_end_time / 10000 AS VARCHAR(2)) + N'点' + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N'分' END AS job_end_time, freq_type, freq_interval, freq_subday_type, freq_subday_interval, active_start_date, active_start_time FROM msdb.dbo.sysschedules s INNER JOIN msdb.dbo.sysjobschedules d ON s.schedule_id=d.schedule_id INNER JOIN msdb.dbo.sysjobs j ON d.job_id = j.job_id ORDER BY j.name
找出执行时间过长的作业
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%"