Use T-SQL to find a job that takes too long to execute

Source: Internet
Author: User

Source: Use T-SQL to find jobs that take too long

Sometimes, some jobs run into problems, so I wrote a script that can track the history, find out how long it takes to perform, and find these jobs in a timely manner and resolve them as soon as possible, with the following code:

SELECT Sj.name
   , Sja.start_execution_date,datediff (SECOND as Executedmin,ja. Avgruntimeonsucceed
 from  as Sja
INNER JOIN  as  on INNER
Join
(
    SELECT job_id,
    Avg
    ((run_duration/10000 * 3600) + ((run_duration%10000)/100*60) + (run_duration%10000)%100)
    +
    Nullif (0,stdev
     as ' Avgruntimeonsucceed '
      from Msdb.dbo.sysjobhistory
    WHERE  and run_status = 1
    GROUP  by
     on sj.job_id = ja.job_id
WHERE  is  not NULL --The job has a start
    and  is NULL --The job is not over
    and Sja.start_execution_date>dateadd (Day, -2,getdate ())--Jobs start within 2 days
   and DATEDIFF (SECOND , Sja.start_execution_date,getdate ()) >ja. Avgruntimeonsucceed *1.5--Job execution time is 50% more than the historical average time

If the job frequently has problems, you can use the script to monitor early detection of problems.

Use T-SQL to find a job that takes too long to execute

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.