码迷,mamicode.com
首页 > 其他好文 > 详细

查看作业详细情况

时间:2016-06-17 19:13:37      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

查看作业名称,执行频率,执行时间等。。。

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%"

 

查看作业详细情况

标签:

原文地址:http://www.cnblogs.com/davidhou/p/5594803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!