找回密码
 立即注册
首页 业界区 安全 sqlserver系统表查出job的下一次运行时间异常现象 ...

sqlserver系统表查出job的下一次运行时间异常现象

稞冀 昨天 23:28
前两天某SQLServer服务器断断续续出现性能问题,综合排查之后怀疑是job定时任务引起的,于是查了一下job的schedule和最近一次执行情况,大部分job的schedule都没有问题,由于当前实例是启用了复制分发,无意中喵到'Distribution clean up: distribution这个job的下一次执行时间明显不正常,启用了复制分发之后,自动生成的Distribution clean up: distribution这个job还是比较熟悉的,该job是每10分钟运行一次,这里查出来的LastRunDateTime和NextRunDate竟然是一样的?一度怀疑这个SQL有问题,不过也用了很多年了,之前也没有留意有这个问题。
1.png

但是通过作业活动监视器(job activity monitor)看到job的下一次运行时间又是正常的。
2.png

查了下资料,有人指出这种方式查询出来的下一次运行时间有同样的问题,原来这是一个一直存在的问题,评论区里出高手:
The only problem with this query is that the next_run_time value could be not accurate for jobs with an interval less then 20min because the sysjobschedules view is refreshed at the same interval, 20min. So the view (and the query from the article) will return a next_run_time that is actually in the past until the next time it will be refreshed. 
就是说SQLServer系统表刷新间隔是20mins一次,如果一个job执行完之后,还没有来得及刷新,查出来的最后执行时间以及下一次执行时间可能是不准确的,可以通过exec msdb.dbo.sp_get_composite_job_info这个系统存储过程来查询。
https://blog.sqlauthority.com/2008/12/22/sql-server-find-next-running-time-of-scheduled-job-using-t-sql/ 参考评论区
3.png

4.png

T-SQL代码
  1. select
  2.          j.name as 'JobName'
  3.          ,j.job_id
  4.          ,case h.run_status
  5.                 when 0 then 'Failed'
  6.                 when 1 then 'Succeeded'
  7.                 when 2 then 'Retry'
  8.                 when 3 then 'Canceled'
  9.                 when 4 then 'In Progress'
  10.         else 'unknow' end as RunStatus
  11.          ,msdb.dbo.agent_datetime(h.run_date, h.run_time) as 'LastRunDateTime'
  12.          ,run_duration as [RunDuration]
  13.          ,sj.next_run_date
  14.          ,sj.next_run_time
  15.          ,case
  16.                  when sj.next_run_date>0 and sj.next_run_time>0
  17.                         then  msdb.dbo.agent_datetime(sj.next_run_date, sj.next_run_time)
  18.                  else
  19.                         null  end as 'NextRunDateTime'
  20. From msdb.dbo.sysjobs j
  21. cross apply (select top 1 * from msdb.dbo.sysjobhistory h where j.job_id = h.job_id order by instance_id desc )h
  22. left join msdb.dbo.sysjobschedules sj on j.job_id = sj.job_id  
  23. where  j.name  = 'Distribution clean up: distribution'
  24. order by JobName desc;
  25. --怀疑上面SQL有问题,单独查原始表,结果也是一样的
  26. select * from msdb.dbo.sysjobs where job_id = 'BECFCA04-E800-4F8B-9FCA-F3DFDD279C11';
  27. select * from msdb.dbo.sysjobschedules where job_id = 'BECFCA04-E800-4F8B-9FCA-F3DFDD279C11';
  28. --启用OPENROWSET
  29. EXEC sp_configure 'show advanced options', 1;
  30. RECONFIGURE;
  31. EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
  32. RECONFIGURE;
  33. --通过OPENROWSET执行,可以筛选出想要的字段
  34. SELECT *
  35. FROM OPENROWSET('SQLNCLI', 'server=localhost,2433;UID=sa;PWD=******;','exec msdb.dbo.sp_get_composite_job_info')
  36. --或者执行执行sp_get_composite_job_info
  37. exec msdb.dbo.sp_get_composite_job_info
复制代码
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册