有很多地方可以設置定時任務,比如: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=-1 GO
2. 刪除作業歷史紀錄
(1) SSMS/SQL Server Agent/右擊作業文件夾或某個作業/查看歷史紀錄/清除
在SQL Server 2000中會一次清除所有作業歷史記錄,SQL Server 2005 及以后版本可以有選擇的清除某個作業/某個時間之前的歷史紀錄;
(2) SQL Server 2005及以后版本,提供了系統存儲過程如下:
--清除所有作業15天前的紀錄 DECLARE @OldestDate datetime SET @OldestDate = GETDATE()-15 EXEC msdb.dbo.sp_purge_jobhistory @oldest_date=@OldestDate --清除作業”Test”3天前的紀錄 DECLARE @OldestDate datetime DECLARE @JobName varchar(256) SET @OldestDate = GETDATE()-3 SET @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.sysjobhistory jh1 where jh1.step_id = 0 and (select COUNT(1) from msdb.dbo.sysjobhistory jh2 where jh2.step_id = 0 and (jh1.job_id = jh2.job_id) and (jh1.instance_id <= jh2.instance_id))=1 --排除syspolicy_purge_history這個系統作業 select a.name job_name, case b.run_status when 0 then 'Failed' when 1 then 'Succeeded' when 2 then 'Retry' when 3 then 'Canceled' else 'Unknown' end as job_status, LEFT(run_date,4)+'-'+SUBSTRING(run_date,5,2)+'-'+RIGHT(run_date,2) +SPACE(1) +LEFT(RIGHT(1000000+run_time,6),2)+':' +SUBSTRING(RIGHT(1000000+run_time,6),3,2)+':' +RIGHT(RIGHT(1000000+run_time,6),2) as job_started_time, +LEFT(RIGHT(1000000+run_duration,6),2)+':' +SUBSTRING(RIGHT(1000000+run_duration,6),3,2)+':' +RIGHT(RIGHT(1000000+run_duration,6),2) as job_duration from msdb.dbo.sysjobs a left join #tmp_job b on a.job_id=b.job_id where a.name not in ('syspolicy_purge_history') and a.enabled = 1 order by b.run_status asc,a.name,b.run_duration desc
2. 作業當前運行狀態及時長
什么時候可能要檢查作業的當前狀態?
(1) 需要關閉SQL Server或SQL Server Agent服務時;
(2) 等到當前作業完成,有后續動作;
(3) 純粹只是查看當前作業運行到哪個步驟等等。
通過SSMS/SQL Server Agent/右擊作業文件夾或某個作業/查看歷史紀錄,看到的作業歷史記錄存放在:
select * from msdb.dbo.sysjobhistory
需要注意的是:至少作業已完成第一步運行,sysjobhistory表中才會有作業歷史紀錄,若當前作業沒有完成任何一個步驟,那表里就不會有本次運行紀錄。所以作業當前狀態用有時無法通過sysjobhistory查看,尤其是作業只有1個步驟且運行時間很長時。
2.1. SQL Server 2005及以后版本
(1) 當前運行狀態:系統存儲過程msdb.dbo.sp_help_job,返回所有作業的運行狀態(current_execution_status),共7種狀態值,詳見幫助文檔。查看所有作業狀態如下:
exec msdb..sp_help_job
(2) 當前運行時長:系統存儲過程sp_help_job無法獲得作業運行時長,可通過新增的系統表sysjobactivity來查看。查看正在運行的作業如下:
select a.name, b.start_execution_date, DATEDIFF(MI,b.start_execution_date,GETDATE()) as job_duration from msdb..sysjobs a inner join msdb..sysjobactivity b on a.job_id = b.job_id where b.start_execution_date is not null and b.stop_execution_date is null
以下腳本結合sp_help_job和sysjobactivity,得到作業的當前狀態及時長:
exec sp_configure 'show advanced options',1 RECONFIGURE exec sp_configure 'Ad Hoc Distributed Queries',1 RECONFIGURE if OBJECT_ID('tempdb..#jobinfo') is not null drop table #jobinfo select * into #jobinfo from openrowset('sqloledb', 'server=(local);trusted_connection=yes','exec msdb.dbo.sp_help_job') select a.name, j.current_execution_status, b.start_execution_date, DATEDIFF(MI,b.start_execution_date,GETDATE()) as job_duration_minute from msdb..sysjobs a inner join msdb..sysjobactivity b on a.job_id = b.job_id inner join #jobinfo j on a.job_id = j.job_id where b.start_execution_date is not null and b.stop_execution_date is null
2.2. SQL Server 2000沿用過來的方法
在SQL Server 2000時,沒有sysjobactivity這個系統表,通常借助sysprocesses監視作業的當前運行狀態及時長。
select j.name, p.status as current_execution_status, p.last_batch as start_execution_date, ISNULL(DATEDIFF(MI, p.last_batch, GETDATE()), 0) as job_duration_minute from msdb.dbo.sysjobs j, master..sysprocesses p where p.program_name like 'SQLAgent - TSQL JobStep (Job%' and substring((cast(j.job_id as varchar(36))),7,2) + substring((cast(j.job_id as varchar(36))),5,2) + substring((cast(j.job_id as varchar(36))),3,2) + substring((cast(j.job_id as varchar(36))),1,2) + substring((cast(j.job_id as varchar(36))),12,2) + substring((cast(j.job_id as varchar(36))),10,2) + substring((cast(j.job_id as varchar(36))),17,2) + substring((cast(j.job_id as varchar(36))),15,2) + substring((cast(j.job_id as varchar(36))),20,4) + substring((cast(j.job_id as varchar(36))),25,12) = substring((cast(p.program_name as varchar(75))),32,32)
sysprocesses里獲得的作業編號跟sysjobs里是不一致的,所以上面進行了轉換,通常只轉換job_id的前8位字符也行,如下腳本做了job_id的簡化轉換,並檢查作業已運行超過30分鍾:
declare @MaxMinutes int set @MaxMinutes = 30 select j.name, p.status as current_execution_status, p.last_batch as start_execution_date, ISNULL(DATEDIFF(MI, p.last_batch, GETDATE()), 0) as job_duration_minute from msdb..sysjobs j inner join master..sysprocesses p on substring(left(cast(j.job_id as varchar(36)),8),7,2) + substring(left(cast(j.job_id as varchar(36)),8),5,2) + substring(left(cast(j.job_id as varchar(36)),8),3,2) + substring(left(cast(j.job_id as varchar(36)),8),1,2) = substring(p.program_name,32,8) where p.program_name like 'SQLAgent - TSQL JobStep (Job%' and ISNULL(DATEDIFF(MI, p.last_batch, GETDATE()), 0) > @MaxMinutes
還有種比較笨的方法,在要監視的所有作業中增加一個步驟,如 : select GETDATE() 放在第一步,這樣在sysjobhistory中就會有步驟1的運行紀錄了,以此為起點,可以計算已運行時長。如果有很多已經部署的job,這確實不是個好辦法。
又或者,在每個作業最后一步,放一個檢查的步驟,這樣所有狀態時長全都監視到了,問題是如果作業運行時間過長,最后的檢查步驟根本無法被運行到。
三. 作業狀態告警
作業在完成后,自己有狀態檢查和告警機制,通常選擇郵件告警,如下圖:
但這僅限對作業最終運行狀態監視:
(1) 沒有運行結束的作業無法告警,或者說對作業的運行時長沒有監視;
(2) 如果作業在某個中間步驟設置了:失敗后繼續下一步,后續的作業步驟都成功,那么作業最終狀態不會顯示會失敗,不會觸發告警,如下腳本檢查每個作業的所有步驟最后一次運行狀態:
if OBJECT_ID('tempdb..#tmp_job_step') is not null drop table #tmp_job_step select jh1.job_id, jh1.step_id, jh1.run_status, CONVERT(varchar(20),jh1.run_date) run_date, CONVERT(varchar(20),jh1.run_time) run_time, CONVERT(varchar(20),jh1.run_duration) run_duration into #tmp_job_step from msdb.dbo.sysjobhistory jh1 where (select COUNT(1) from msdb.dbo.sysjobhistory jh2 where (jh1.job_id = jh2.job_id and jh1.step_id = jh2.step_id) and (jh1.instance_id <= jh2.instance_id))=1 select a.name job_name, s.step_name, case b.run_status when 0 then 'Failed' when 1 then 'Succeeded' when 2 then 'Retry' when 3 then 'Canceled' else 'Unknown' end as job_status, LEFT(run_date,4)+'-'+SUBSTRING(run_date,5,2)+'-'+RIGHT(run_date,2) +SPACE(1) +LEFT(RIGHT(1000000+run_time,6),2)+':' +SUBSTRING(RIGHT(1000000+run_time,6),3,2)+':' +RIGHT(RIGHT(1000000+run_time,6),2) as job_started_time, +LEFT(RIGHT(1000000+run_duration,6),2)+':' +SUBSTRING(RIGHT(1000000+run_duration,6),3,2)+':' +RIGHT(RIGHT(1000000+run_duration,6),2) as job_duration from msdb.dbo.sysjobs a left join #tmp_job_step b on a.job_id=b.job_id inner join msdb.dbo.sysjobsteps s on b.job_id = s.job_id and b.step_id = s.step_id where a.name not in ('syspolicy_purge_history') and a.enabled = 1 order by b.run_status asc,a.name,b.run_duration desc
小結
SQL Server Agent作業自身的告警機制,有時並不夠用,所以還需要部署另外的作業,來檢查其他所有作業的運行狀況,大致步驟如下 :
(1) 部署數據庫郵件;
(2) 部署作業:定時檢查其他所有作業/步驟狀態,發郵件告警;
作業運行時長可以在這一並檢查,有時一些作業運行了很多天沒結束還沒人知道,也可以考慮放在性能監控里,和其他數據庫請求一起監控。但是對於時長,通常需要有個性能基線,如果沒有的話直接和歷史最大值相比也是不錯的選擇。