查看job运行时间,以便不影响生产数据库正常运行

简介:
You may find yourself in a position where you need to look at what jobs have been running over a specified
period of time. SQL Server has a stored procedure sp help job that will allow you to look at the jobs and 
what their status is currently, but maybe you need to do some additional processing like sending a database
mail email to someone so that they know to look at the job. Perhaps you want to have the system check
itself before the start of business each day to ensure that no overnight jobs got stuck and are still running
which could cause degraded performance or problems with processing during the day. The following code
will work on SQL 2000, 2005, and 2008. 


JobID to ProcessID Function

This function is used to convert the sysjobs.jobid field into the processid that will show up in 
sysprocesses.program_name field for an executing job.

 
CREATE FUNCTION dbo.udf_SysJobs_GetProcessid(@job_id uniqueidentifier)
RETURNS VARCHAR(8)
AS
BEGIN
RETURN (substring(left(@job_id,8),7,2) +
		substring(left(@job_id,8),5,2) +
		substring(left(@job_id,8),3,2) +
		substring(left(@job_id,8),1,2))
END
 

Return to Top


TSQL Code to Find Jobs Running Over x Minutes

The following code will return a row for each job that is currently running and has been running for 
over the number of minutes set in the @MaxMinutes variable. To adjust the time frame it looks for,
just change this variable value.

 
DECLARE @MaxMinutes int
SET @MaxMinutes = 30
 
SELECT	p.spid, 
	j.name, 
	p.program_name, 
	isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) [MinutesRunning], 
	last_batch
FROM master..sysprocesses p
JOIN msdb..sysjobs j ON dbo.udf_sysjobs_getprocessid(j.job_id) = substring(p.program_name,32,8)
WHERE program_name like 'SQLAgent - TSQL JobStep (Job %'
  AND isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) > @MaxMinutes
 

You may find yourself in a position where you need to look at what jobs have been running over a specified
period of time. SQL Server has a stored procedure sp help job that will allow you to look at the jobs and 
what their status is currently, but maybe you need to do some additional processing like sending a database
mail email to someone so that they know to look at the job. Perhaps you want to have the system check
itself before the start of business each day to ensure that no overnight jobs got stuck and are still running
which could cause degraded performance or problems with processing during the day. The following code
will work on SQL 2000, 2005, and 2008. 


JobID to ProcessID Function

This function is used to convert the sysjobs.jobid field into the processid that will show up in 
sysprocesses.program_name field for an executing job.

 
CREATE FUNCTION dbo.udf_SysJobs_GetProcessid(@job_id uniqueidentifier)
RETURNS VARCHAR(8)
AS
BEGIN
RETURN (substring(left(@job_id,8),7,2) +
		substring(left(@job_id,8),5,2) +
		substring(left(@job_id,8),3,2) +
		substring(left(@job_id,8),1,2))
END
 

Return to Top


TSQL Code to Find Jobs Running Over x Minutes

The following code will return a row for each job that is currently running and has been running for 
over the number of minutes set in the @MaxMinutes variable. To adjust the time frame it looks for,
just change this variable value.

 
DECLARE @MaxMinutes int
SET @MaxMinutes = 30
 
SELECT	p.spid, 
	j.name, 
	p.program_name, 
	isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) [MinutesRunning], 
	last_batch
FROM master..sysprocesses p
JOIN msdb..sysjobs j ON dbo.udf_sysjobs_getprocessid(j.job_id) = substring(p.program_name,32,8)
WHERE program_name like 'SQLAgent - TSQL JobStep (Job %'
  AND isnull(DATEDIFF(mi, p.last_batch, getdate()), 0) > @MaxMinutes



本文转自 Fanr_Zh 博客园博客,原文链接:http://www.cnblogs.com/Amaranthus/archive/2011/03/30/1999544.html,如需转载请自行联系原作者




相关文章
|
1月前
|
Oracle 关系型数据库 数据库
|
3月前
|
NoSQL Java 关系型数据库
基于Java swing和mysql实现的学生选课管理系统(源码+数据库+运行指导视频)
基于Java swing和mysql实现的学生选课管理系统(源码+数据库+运行指导视频)
|
3月前
|
Java 关系型数据库 MySQL
基于java swing和mysql实现的仓库商品管理系统(源码+数据库+运行指导视频)
基于java swing和mysql实现的仓库商品管理系统(源码+数据库+运行指导视频)
|
3月前
|
NoSQL Java 关系型数据库
基于java swing和mysql实现的汽车租赁管理系统(源码+数据库+文档+运行指导视频)
基于java swing和mysql实现的汽车租赁管理系统(源码+数据库+文档+运行指导视频)
|
3月前
|
NoSQL Java 关系型数据库
基于Java swing和mysql实现酒店管理系统(源码+数据库+运行指导视频+系统用户使用手册+系统PPT+数据库设计说明书+系统概要说明书+需求说明书+详细说明书)
基于Java swing和mysql实现酒店管理系统(源码+数据库+运行指导视频+系统用户使用手册+系统PPT+数据库设计说明书+系统概要说明书+需求说明书+详细说明书)
|
3月前
|
前端开发 IDE Java
基于Springboot+MYSQL+Maven实现的宠物医院管理系统(源码+数据库+运行指导文档+项目运行指导视频)
基于Springboot+MYSQL+Maven实现的宠物医院管理系统(源码+数据库+运行指导文档+项目运行指导视频)
163 0
|
29天前
|
SQL 编解码 数据库
MyKtv点歌系统前台主要功能实现,内附数据库脚本,可以直接运行
MyKtv点歌系统前台主要功能实现,内附数据库脚本,可以直接运行
12 1
MyKtv点歌系统前台主要功能实现,内附数据库脚本,可以直接运行
|
1月前
|
Java 数据库连接 数据库
解决docker中运行的jar包连不上数据库
解决docker中运行的jar包连不上数据库
|
2月前
|
SQL 安全 Devops
一个简单的代码拼写错误导致17个生产数据库被删!微软Azure DevOps宕机10小时始末
一个简单的代码拼写错误导致17个生产数据库被删!微软Azure DevOps宕机10小时始末
21 0
|
2月前
|
Kubernetes 关系型数据库 数据库
运行在容器中 Postgres 数据库数据损坏后如何恢复?
运行在容器中 Postgres 数据库数据损坏后如何恢复?