您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

4. SQL Server数据库状态监控

2025/4/17 5:12:25发布38次查看
有很多地方可以设置定时任务,比如:windows的计划任务,linux下的crontab,各种开发工具里的timer组件。sqlserver也有它的定时任务组件sqlserveragent,基于它
有很多地方可以设置定时任务,比如:windows的计划任务,linux下的crontab,各种开发工具里的timer组件。sql server也有它的定时任务组件 sql server agent,基于它可以方便的部署各种数据库相关的作业(job)。
一. 作业历史纪录
作业的历史纪录按时间采用fifo原则,当累积的作业历史纪录达到上限时,就会删除最老的纪录。
1. 作业历史纪录数配置
所有作业总计纪录条数默认为1000,最多为999999条;单个作业总计记录条数默认为100,最多为999999条。有下面2种方式可以进行修改:
(1) ssms/sql server agent/属性/历史;
(2) 未记载的扩展存储过程,sql server 2005及以后版本适用,以下脚本将记录数设回默认值:
exec msdb.dbo.sp_set_sqlagent_properties  @jobhistory_max_rows=-1, @jobhistory_max_rows_per_job=-1go
2. 删除作业历史纪录
(1) ssms/sql server agent/右击作业文件夹或某个作业/查看历史纪录/清除
在sql server 2000中会一次清除所有作业历史记录,sql server 2005 及以后版本可以有选择的清除某个作业/某个时间之前的历史纪录;
(2) sql server 2005及以后版本,提供了系统存储过程如下:
--清除所有作业15天前的纪录declare @oldestdate datetimeset @oldestdate = getdate()-15exec msdb.dbo.sp_purge_jobhistory @oldest_date=@oldestdate--清除作业”test”3天前的纪录declare @oldestdate datetimedeclare @jobname varchar(256)set @oldestdate = getdate()-3set @jobname = 'test'exec msdb.dbo.sp_purge_jobhistory @job_name=@jobname, @oldest_date=@oldestdate作业历史纪录数有上限,通常不需要手动去删除。
3. 保留作业历史纪录
即便设置了历史记录上限到999999,如果作业很多,加之作业运行很频繁,最终历史记录还是会被慢慢删除掉。
如果想要保留某些作业历史的记录,可以打开作业属性/步骤/编辑/高级,选择将这个步骤的历史记录输出到文件/自定义表中,如下图:
二. 作业运行状态
界面上可以通过: ssms/sql server agent/右击作业文件夹或某个作业/查看历史纪录,如下用sql 语句检查作业状态。
1. 作业上次运行状态及时长
利用系统表msdb.dbo.sysjobhistory:
(1) 表中的run_status字段表示作业上次运行状态,有0~3共4种状态值,详见帮助文档,另外在2005的帮助文档中写到:sysjobhistory的run_status为4表示运行中,经测试是错误的,在2008的帮助中已没有4这个状态;
(2) 表中run_duration字段表示作业上次运行时长,格式为hhmmss,比如20000则表示运行了2小时。
如下脚本查看所有作业最后一次运行状态及时长:
if object_id('tempdb..#tmp_job') is not null    drop table #tmp_job --只取最后一次结果select job_id,       run_status,       convert(varchar(20),run_date) run_date,       convert(varchar(20),run_time) run_time,       convert(varchar(20),run_duration)run_duration  into #tmp_job  from msdb.dbo.sysjobhistoryjh1 where jh1.step_id = 0   and(select count(1) from msdb.dbo.sysjobhistory jh2         wherejh2.step_id = 0          and(jh1.job_id = jh2.job_id)          and(jh1.instance_id 
2. 作业当前运行状态及时长
什么时候可能要检查作业的当前状态?
(1) 需要关闭sql server或sql server agent服务时;
(2) 等到当前作业完成,有后续动作;
(3) 纯粹只是查看当前作业运行到哪个步骤等等。
通过ssms/sql server agent/右击作业文件夹或某个作业/查看历史纪录,看到的作业历史记录存放在:
select * from msdb.dbo.sysjobhistory
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product