sqlserver体系表查出job的下一次运行时间非常征象

[复制链接]
发表于 2025-9-15 18:15:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
前两天某SQLServer服务器断断续续出现性能题目,综合排查之后猜疑是job定时任务引起的,于是查了一下job的schedule和迩来一次执行情况,大部门job的schedule都没有题目,由于当前实例是启用了复制分发,偶然中喵到'Distribution clean up: distribution这个job的下一次执行时间明显不正常,启用了复制分发之后,自动天生的Distribution clean up: distribution这个job照旧比力熟悉的,该job是每10分钟运行一次,这里查出来的LastRunDateTime和NextRunDate竟然是一样的?一度猜疑这个SQL有题目,不过也用了很多年了,之前也没有注意有这个题目。

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

查了下资料,有人指出这种方式查询出来的下一次运行时间有同样的题目,原来这是一个不绝存在的题目,批评区里出高手:
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/ 参考批评区


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
复制代码
 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表