Commit cff0578a authored by dwentzel's avatar dwentzel

updated sysjobhistory purge

parent ca0ceedc
if exists (select * from sysobjects where id = object_id('DBA_PM_Addin_sysjobhistory_purge') and sysstat & 0xf = 4)
DROP procedure dbo.DBA_PM_Addin_sysjobhistory_purge
GO
CREATE PROCEDURE DBA_PM_Addin_sysjobhistory_purge
@job_id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON
DECLARE @ConfigValueText DATETIME
DECLARE @ConfigValueInt INT
DECLARE @now DATETIME
SELECT @now = GETDATE()
SELECT @ConfigValueText = COALESCE(ConfigValueText,'1/1/1980')
FROM DBA_PM_Config
WHERE ConfigSetting = 'LastSysJobHistoryPurgeRun'
SELECT @ConfigValueInt = COALESCE(ConfigValueInt,24)
FROM DBA_PM_Config
WHERE ConfigSetting = 'DBA_PM_Addin_sysjobhistory_purge'
if datediff (hh,COALESCE(@ConfigValueText,'1/1/1980'),@now) > @ConfigValueInt
BEGIN
--call the driver which handles cycling through the list of jobs and calling the purge procedure.
--it also sets the Agent sysjobhistory to unlimited. We do this in a subproc to make TSQLT testing easier
--since this proc handles the scaffolding around how SFU works...and we don't want to have to work around the SFU scheduler
--to get the TSQLT tests to work.
EXEC DBA_PM_sysjobhistory_driver
--now update the config table to reflect a successful run
UPDATE DBA_PM_Config
SET ConfigValueText = convert(varchar(100),@now)
WHERE ConfigSetting = 'LastSysJobHistoryPurgeRun'
END;
END;
GO
...@@ -4,14 +4,14 @@ ...@@ -4,14 +4,14 @@
and to handle looping through deletes smartly. and to handle looping through deletes smartly.
no TABLOCKX no TABLOCKX
tests are in DBA_PM_sysjobhistory_driver_Tests.sfu_dbo_test.sql tests are in DBA_sysjobhistory_driver_Tests.sfu_dbo_test.sql
*/ */
if exists (select * from sysobjects where id = object_id('DBA_PM_sp_jobhistory_row_limiter') and sysstat & 0xf = 4) if exists (select * from sysobjects where id = object_id('DBA_sp_jobhistory_row_limiter') and sysstat & 0xf = 4)
DROP procedure dbo.DBA_PM_sp_jobhistory_row_limiter DROP procedure dbo.DBA_sp_jobhistory_row_limiter
GO GO
CREATE PROCEDURE DBA_PM_sp_jobhistory_row_limiter CREATE PROCEDURE DBA_sp_jobhistory_row_limiter
@job_id UNIQUEIDENTIFIER @job_id UNIQUEIDENTIFIER
AS AS
BEGIN BEGIN
...@@ -33,11 +33,11 @@ BEGIN ...@@ -33,11 +33,11 @@ BEGIN
@NumDaysToRetain = COALESCE(c_byJob.NumDaysToRetain,c_byCat.NumDaysToRetain,c_Default.NumDaysToRetain,30) , @NumDaysToRetain = COALESCE(c_byJob.NumDaysToRetain,c_byCat.NumDaysToRetain,c_Default.NumDaysToRetain,30) ,
@NumFailureDaysToRetain = COALESCE(c_byJob.NumFailureDaysToRetain,c_byCat.NumFailureDaysToRetain,c_Default.NumFailureDaysToRetain,30) @NumFailureDaysToRetain = COALESCE(c_byJob.NumFailureDaysToRetain,c_byCat.NumFailureDaysToRetain,c_Default.NumFailureDaysToRetain,30)
FROM v_sysjobs j FROM v_sysjobs j
LEFT JOIN DBA_PM_sysjobhistory_config c_byJob --first we prefer to JOIN first by JobName LEFT JOIN DBA_sysjobhistory_config c_byJob --first we prefer to JOIN first by JobName
ON j.JobName = c_byJob.JobName ON j.JobName = c_byJob.JobName
LEFT JOIN DBA_PM_sysjobhistory_config c_byCat --next we prefer to JOIN first by Category LEFT JOIN DBA_sysjobhistory_config c_byCat --next we prefer to JOIN first by Category
ON j.CategoryName = c_byCat.CategoryName ON j.CategoryName = c_byCat.CategoryName
CROSS JOIN DBA_PM_sysjobhistory_config c_Default --last, get me the defaults. CROSS JOIN DBA_sysjobhistory_config c_Default --last, get me the defaults.
WHERE j.job_id = @job_id AND WHERE j.job_id = @job_id AND
c_Default.JobName = '(default)' AND c_Default.CategoryName = '(default)' c_Default.JobName = '(default)' AND c_Default.CategoryName = '(default)'
......
IF NOT EXISTS (select * from sys.objects WHERE name = 'DBA_PM_sysjobhistory_config') IF NOT EXISTS (select * from sys.objects WHERE name = 'DBA_sysjobhistory_config')
BEGIN BEGIN
CREATE TABLE DBA_PM_sysjobhistory_config ( CREATE TABLE DBA_sysjobhistory_config (
JobName varchar(256), --(name of the job for this retention setting) JobName varchar(256), --(name of the job for this retention setting)
CategoryName varchar(256), --(only JobName or CategoryName should be completed) CategoryName varchar(256), --(only JobName or CategoryName should be completed)
NumDaysToRetain int , --(number of days to retain for this entry) NumDaysToRetain int , --(number of days to retain for this entry)
......
if exists (select * from sysobjects where id = object_id('DBA_PM_sysjobhistory_driver') and sysstat & 0xf = 4) if exists (select * from sysobjects where id = object_id('DBA_sysjobhistory_driver') and sysstat & 0xf = 4)
DROP procedure dbo.DBA_PM_sysjobhistory_driver DROP procedure dbo.DBA_sysjobhistory_driver
GO GO
CREATE PROCEDURE DBA_PM_sysjobhistory_driver CREATE PROCEDURE DBA_sysjobhistory_driver
AS AS
BEGIN BEGIN
SET DEADLOCK_PRIORITY LOW SET DEADLOCK_PRIORITY LOW
...@@ -25,7 +25,7 @@ EXEC msdb.dbo.sp_set_sqlagent_properties @jobhistory_max_rows=-1, ...@@ -25,7 +25,7 @@ EXEC msdb.dbo.sp_set_sqlagent_properties @jobhistory_max_rows=-1,
WHILE (@@FETCH_STATUS = 0) WHILE (@@FETCH_STATUS = 0)
BEGIN BEGIN
--PRINT 'Running DBA_PM_sp_jobhistory_row_limiter for @job_id: ' + convert(varchar(200),@job_id) --PRINT 'Running DBA_PM_sp_jobhistory_row_limiter for @job_id: ' + convert(varchar(200),@job_id)
EXEC DBA_PM_sp_jobhistory_row_limiter @job_id = @job_id EXEC DBA_sp_jobhistory_row_limiter @job_id = @job_id
FETCH NEXT FROM CURSER INTO @job_id FETCH NEXT FROM CURSER INTO @job_id
END END
CLOSE CURSER CLOSE CURSER
......
http://www.davewentzel.com/content/blocking-and-contention-sysjobhistory
A utility that customizes what gets purged via sysjobhistory. Allows far more flexibility than what Microsoft
provides natively. Also, the purge is "smart" and will not cause blocking and concurrency
problems on instances with many, frequent SQL Agent jobs. Includes tsqlt unit tests.
`seeddata.sql` is the only file that should need to be customized. You place your jobs and categories and their custom
retention settings here.
`DBA_sysjobhistory_config_tbl.sql` builds the table that handles the custom sysjobhistory retention configuration.
`DBA_sysjobhistory_driver.sql` configures SQL Agent for unlimited history retention, since we'll be handling that now. It
then examines each job in turn and runs `DBA_sp_jobhistory_row_limiter` to retain the custom settings for this job/category.
This procedure should be scheduled to run every day (or so, depending on your requirements).
`DBA_sp_jobhistory_row_limiter.sql` is more or less a copy of MS-provided sp_jobhistory_row_limiter with our custom logic.
`DBA_sysjobhistory_driver_Tests.sql` is the tsqlt unit tests for this feature.
http://www.davewentzel.com/content/blocking-and-contention-sysjobhistory. A utility that
customizes what gets purged via sysjobhistory. Allows far more flexibility than what Microsoft
provides natively. Also, the purge is "smart" and will not cause blocking and concurrency
problems on instances with many, frequent SQL Agent jobs. Includes tsqlt unit tests.
\ No newline at end of file
INSERT INTO #DBA_PM_Config (ConfigSetting,ConfigValueText,ConfigValueInt)
VALUES ('DBA_PM_Addin_sysjobhistory_purge','ConfigValueInt indicates Polling Interval (Hours)',24)
INSERT INTO #DBA_PM_Config (ConfigSetting,ConfigValueText,ConfigValueInt)
VALUES ('LastSysJobHistoryPurgeRun','1/1/1980',NULL)
IF NOT EXISTS (select * from DBA_PM_sysjobhistory_config) IF NOT EXISTS (select * from DBA_PM_sysjobhistory_config)
BEGIN BEGIN
--insert base seed data, but only if this is the first run of SFU installer --insert base seed data, but only if this is the first run of SFU installer
INSERT INTO DBA_PM_sysjobhistory_config VALUES (NULL,'ETL',1,90) INSERT INTO DBA_sysjobhistory_config VALUES (NULL,'ETL',1,90)
INSERT INTO DBA_PM_sysjobhistory_config VALUES ('(default)','(default)',3,90) INSERT INTO DBA_sysjobhistory_config VALUES ('(default)','(default)',3,90)
INSERT INTO DBA_PM_sysjobhistory_config VALUES (NULL,'SFU TOOL',1,90) INSERT INTO DBA_sysjobhistory_config VALUES (NULL,'SFU TOOL',1,90)
END; END;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment