Job Duration

  • Hello,

    I have found some script which I find very usefull but would like to append an extra column and do not have enough tsql knowledge to do this :blush:

    The script shows jobname schedules, start, end time, but is it possible to also show last runtime Duration of the job?

    I'm just asking 😉

    Thx & Brgd

    T.

    select

    'Server' = left(@@ServerName,20),

    'JobName' = left(S.name,30),

    'ScheduleName' = left(ss.name,25),

    'Enabled' = CASE (S.enabled)

    WHEN 0 THEN 'No'

    WHEN 1 THEN 'Yes'

    ELSE '??'

    END,

    'Frequency' = CASE(ss.freq_type)

    WHEN 1 THEN 'Once'

    WHEN 4 THEN 'Daily'

    WHEN 8 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Weeks' else 'Weekly' end)

    WHEN 16 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' else 'Monthly' end)

    WHEN 32 THEN 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' -- RELATIVE

    WHEN 64 THEN 'SQL Startup'

    WHEN 128 THEN 'SQL Idle'

    ELSE '??'

    END,

    'Interval' = CASE

    WHEN (freq_type = 1) then 'One time only'

    WHEN (freq_type = 4 and freq_interval = 1) then 'Every Day'

    WHEN (freq_type = 4 and freq_interval > 1) then 'Every ' + convert(varchar(10),freq_interval) + ' Days'

    WHEN (freq_type = 8) then (select 'Weekly Schedule' = D1+ D2+D3+D4+D5+D6+D7

    from (select ss.schedule_id,

    freq_interval,

    'D1' = CASE WHEN (freq_interval & 1 <> 0) then 'Sun ' ELSE '' END,

    'D2' = CASE WHEN (freq_interval & 2 <> 0) then 'Mon ' ELSE '' END,

    'D3' = CASE WHEN (freq_interval & 4 <> 0) then 'Tue ' ELSE '' END,

    'D4' = CASE WHEN (freq_interval & 8 <> 0) then 'Wed ' ELSE '' END,

    'D5' = CASE WHEN (freq_interval & 16 <> 0) then 'Thu ' ELSE '' END,

    'D6' = CASE WHEN (freq_interval & 32 <> 0) then 'Fri ' ELSE '' END,

    'D7' = CASE WHEN (freq_interval & 64 <> 0) then 'Sat ' ELSE '' END

    from msdb..sysschedules ss

    where freq_type = 8

    ) as F

    where schedule_id = sj.schedule_id

    )

    WHEN (freq_type = 16) then 'Day ' + convert(varchar(2),freq_interval)

    WHEN (freq_type = 32) then (select freq_rel + WDAY

    from (select ss.schedule_id,

    'freq_rel' = CASE(freq_relative_interval)

    WHEN 1 then 'First'

    WHEN 2 then 'Second'

    WHEN 4 then 'Third'

    WHEN 8 then 'Fourth'

    WHEN 16 then 'Last'

    ELSE '??'

    END,

    'WDAY' = CASE (freq_interval)

    WHEN 1 then ' Sun'

    WHEN 2 then ' Mon'

    WHEN 3 then ' Tue'

    WHEN 4 then ' Wed'

    WHEN 5 then ' Thu'

    WHEN 6 then ' Fri'

    WHEN 7 then ' Sat'

    WHEN 8 then ' Day'

    WHEN 9 then ' Weekday'

    WHEN 10 then ' Weekend'

    ELSE '??'

    END

    from msdb..sysschedules ss

    where ss.freq_type = 32

    ) as WS

    where WS.schedule_id =ss.schedule_id

    )

    END,

    'Time' = CASE (freq_subday_type)

    WHEN 1 then left(stuff((stuff((replicate('0', 6 - len(Active_Start_Time)))+ convert(varchar(6),Active_Start_Time),3,0,':')),6,0,':'),8)

    WHEN 2 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' seconds'

    WHEN 4 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' minutes'

    WHEN 8 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' hours'

    ELSE '??'

    END,

    'Next Run Time' = CASE SJ.next_run_date

    WHEN 0 THEN cast('n/a' as char(10))

    ELSE convert(char(10), convert(datetime, convert(char(8),SJ.next_run_date)),120) + ' ' + left(stuff((stuff((replicate('0', 6 - len(next_run_time)))+ convert(varchar(6),next_run_time),3,0,':')),6,0,':'),8)

    END

    from msdb.dbo.sysjobschedules SJ

    join msdb.dbo.sysjobs S on S.job_id = SJ.job_id

    join msdb.dbo.sysschedules SS on ss.schedule_id = sj.schedule_id

    order by S.name

  • This may be of help for you:

    with duration as (

    Select job_id

    ,MinDuration = min((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60)

    ,AvgDuration = avg((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60)

    ,MaxDuration = max((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60)

    ,StdevDuration = stdev((run_duration/10000*3600 + (run_duration/100)%100*60 + run_duration%100 + 31 ) / 60)

    From msdb.dbo.sysjobhistory

    Group by job_id

    )

    select

    'Server' = left(@@ServerName,20),

    'JobName' = left(S.name,30),

    'ScheduleName' = left(ss.name,25),

    'Enabled' = CASE (S.enabled)

    WHEN 0 THEN 'No'

    WHEN 1 THEN 'Yes'

    ELSE '??'

    END,

    'Frequency' = CASE(ss.freq_type)

    WHEN 1 THEN 'Once'

    WHEN 4 THEN 'Daily'

    WHEN 8 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Weeks' else 'Weekly' end)

    WHEN 16 THEN

    (case when (ss.freq_recurrence_factor > 1)

    then 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' else 'Monthly' end)

    WHEN 32 THEN 'Every ' + convert(varchar(3),ss.freq_recurrence_factor) + ' Months' -- RELATIVE

    WHEN 64 THEN 'SQL Startup'

    WHEN 128 THEN 'SQL Idle'

    ELSE '??'

    END,

    'Interval' = CASE

    WHEN (freq_type = 1) then 'One time only'

    WHEN (freq_type = 4 and freq_interval = 1) then 'Every Day'

    WHEN (freq_type = 4 and freq_interval > 1) then 'Every ' + convert(varchar(10),freq_interval) + ' Days'

    WHEN (freq_type = 8) then (select 'Weekly Schedule' = D1+ D2+D3+D4+D5+D6+D7

    from (select ss.schedule_id,

    freq_interval,

    'D1' = CASE WHEN (freq_interval & 1 <> 0) then 'Sun ' ELSE '' END,

    'D2' = CASE WHEN (freq_interval & 2 <> 0) then 'Mon ' ELSE '' END,

    'D3' = CASE WHEN (freq_interval & 4 <> 0) then 'Tue ' ELSE '' END,

    'D4' = CASE WHEN (freq_interval & 8 <> 0) then 'Wed ' ELSE '' END,

    'D5' = CASE WHEN (freq_interval & 16 <> 0) then 'Thu ' ELSE '' END,

    'D6' = CASE WHEN (freq_interval & 32 <> 0) then 'Fri ' ELSE '' END,

    'D7' = CASE WHEN (freq_interval & 64 <> 0) then 'Sat ' ELSE '' END

    from msdb..sysschedules ss

    where freq_type = 8

    ) as F

    where schedule_id = sj.schedule_id

    )

    WHEN (freq_type = 16) then 'Day ' + convert(varchar(2),freq_interval)

    WHEN (freq_type = 32) then (select freq_rel + WDAY

    from (select ss.schedule_id,

    'freq_rel' = CASE(freq_relative_interval)

    WHEN 1 then 'First'

    WHEN 2 then 'Second'

    WHEN 4 then 'Third'

    WHEN 8 then 'Fourth'

    WHEN 16 then 'Last'

    ELSE '??'

    END,

    'WDAY' = CASE (freq_interval)

    WHEN 1 then ' Sun'

    WHEN 2 then ' Mon'

    WHEN 3 then ' Tue'

    WHEN 4 then ' Wed'

    WHEN 5 then ' Thu'

    WHEN 6 then ' Fri'

    WHEN 7 then ' Sat'

    WHEN 8 then ' Day'

    WHEN 9 then ' Weekday'

    WHEN 10 then ' Weekend'

    ELSE '??'

    END

    from msdb..sysschedules ss

    where ss.freq_type = 32

    ) as WS

    where WS.schedule_id =ss.schedule_id

    )

    END,

    'Time' = CASE (freq_subday_type)

    WHEN 1 then left(stuff((stuff((replicate('0', 6 - len(Active_Start_Time)))+ convert(varchar(6),Active_Start_Time),3,0,':')),6,0,':'),8)

    WHEN 2 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' seconds'

    WHEN 4 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' minutes'

    WHEN 8 then 'Every ' + convert(varchar(10),freq_subday_interval) + ' hours'

    ELSE '??'

    END,

    'Next Run Time' = CASE SJ.next_run_date

    WHEN 0 THEN cast('n/a' as char(10))

    ELSE convert(char(10), convert(datetime, convert(char(8),SJ.next_run_date)),120) + ' ' + left(stuff((stuff((replicate('0', 6 - len(next_run_time)))+ convert(varchar(6),next_run_time),3,0,':')),6,0,':'),8)

    END

    ,sh.AvgDuration,sh.MaxDuration,sh.MinDuration,sh.StdevDuration

    from msdb.dbo.sysjobschedules SJ

    Inner Join msdb.dbo.sysjobs S

    On S.job_id = SJ.job_id

    Inner Join msdb.dbo.sysschedules SS

    On ss.schedule_id = sj.schedule_id

    Inner Join duration sh

    On sj.job_id = sh.job_id

    order by S.name

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • One last note - the duration columns are all in minutes.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Heeeeey, this is great! Thanks for your help & effort Jason!

    thxalot

    Grtz

    T.

  • You're welcome.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Jason,

    Is it also possible to show the last_duration? Avg & Max is nice as it is, but bit misleading for me now.

    I hope not too much work 😉

    Thx 4 effort anyway....

  • I would like to put in a plug for SQLJobVis - a wonderful FREE utility that helps with agent job analysis: http://www.sqlsoft.co.uk/sqljobvis.php

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • hey, thanks for the reply. This looks very interesting indeed, will have a look at it now.

    Such things should be make forum-sticky 😉

    Thx!

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply