Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dwentzel
SQLScripts
Commits
cff0578a
Commit
cff0578a
authored
Oct 01, 2014
by
dwentzel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated sysjobhistory purge
parent
ca0ceedc
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
34 additions
and
71 deletions
+34
-71
sysjobhistoryPurge/DBA_PM_Addin_sysjobhistory_purge.sfu_dbo_sproc.sql
...yPurge/DBA_PM_Addin_sysjobhistory_purge.sfu_dbo_sproc.sql
+0
-44
sysjobhistoryPurge/DBA_sp_jobhistory_row_limiter.sql
sysjobhistoryPurge/DBA_sp_jobhistory_row_limiter.sql
+7
-7
sysjobhistoryPurge/DBA_sysjobhistory_config_tbl.sql
sysjobhistoryPurge/DBA_sysjobhistory_config_tbl.sql
+2
-2
sysjobhistoryPurge/DBA_sysjobhistory_driver.sql
sysjobhistoryPurge/DBA_sysjobhistory_driver.sql
+4
-4
sysjobhistoryPurge/DBA_sysjobhistory_driver_Tests.sql
sysjobhistoryPurge/DBA_sysjobhistory_driver_Tests.sql
+0
-0
sysjobhistoryPurge/README.MD
sysjobhistoryPurge/README.MD
+18
-0
sysjobhistoryPurge/README.txt
sysjobhistoryPurge/README.txt
+0
-4
sysjobhistoryPurge/seeddata.sql
sysjobhistoryPurge/seeddata.sql
+3
-10
No files found.
sysjobhistoryPurge/DBA_PM_Addin_sysjobhistory_purge.sfu_dbo_sproc.sql
deleted
100644 → 0
View file @
ca0ceedc
if
exists
(
select
*
from
sysobjects
where
id
=
object_id
(
'DBA_PM_Addin_sysjobhistory_purge'
)
and
sysstat
&
0
xf
=
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
sysjobhistoryPurge/DBA_
PM_
sp_jobhistory_row_limiter.
sfu_dbo_sproc.
sql
→
sysjobhistoryPurge/DBA_sp_jobhistory_row_limiter.sql
View file @
cff0578a
...
...
@@ -4,14 +4,14 @@
and to handle looping through deletes smartly.
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
&
0
xf
=
4
)
DROP
procedure
dbo
.
DBA_
PM_
sp_jobhistory_row_limiter
if
exists
(
select
*
from
sysobjects
where
id
=
object_id
(
'DBA_sp_jobhistory_row_limiter'
)
and
sysstat
&
0
xf
=
4
)
DROP
procedure
dbo
.
DBA_sp_jobhistory_row_limiter
GO
CREATE
PROCEDURE
DBA_
PM_
sp_jobhistory_row_limiter
CREATE
PROCEDURE
DBA_sp_jobhistory_row_limiter
@
job_id
UNIQUEIDENTIFIER
AS
BEGIN
...
...
@@ -33,11 +33,11 @@ BEGIN
@
NumDaysToRetain
=
COALESCE
(
c_byJob
.
NumDaysToRetain
,
c_byCat
.
NumDaysToRetain
,
c_Default
.
NumDaysToRetain
,
30
)
,
@
NumFailureDaysToRetain
=
COALESCE
(
c_byJob
.
NumFailureDaysToRetain
,
c_byCat
.
NumFailureDaysToRetain
,
c_Default
.
NumFailureDaysToRetain
,
30
)
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
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
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
c_Default
.
JobName
=
'(default)'
AND
c_Default
.
CategoryName
=
'(default)'
...
...
sysjobhistoryPurge/DBA_
PM_
sysjobhistory_config_tbl.sql
→
sysjobhistoryPurge/DBA_sysjobhistory_config_tbl.sql
View file @
cff0578a
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
CREATE
TABLE
DBA_
PM_
sysjobhistory_config
(
CREATE
TABLE
DBA_sysjobhistory_config
(
JobName
varchar
(
256
),
--(name of the job for this retention setting)
CategoryName
varchar
(
256
),
--(only JobName or CategoryName should be completed)
NumDaysToRetain
int
,
--(number of days to retain for this entry)
...
...
sysjobhistoryPurge/DBA_
PM_
sysjobhistory_driver.
sfu_dbo_sproc.
sql
→
sysjobhistoryPurge/DBA_sysjobhistory_driver.sql
View file @
cff0578a
if
exists
(
select
*
from
sysobjects
where
id
=
object_id
(
'DBA_
PM_
sysjobhistory_driver'
)
and
sysstat
&
0
xf
=
4
)
DROP
procedure
dbo
.
DBA_
PM_
sysjobhistory_driver
if
exists
(
select
*
from
sysobjects
where
id
=
object_id
(
'DBA_sysjobhistory_driver'
)
and
sysstat
&
0
xf
=
4
)
DROP
procedure
dbo
.
DBA_sysjobhistory_driver
GO
CREATE
PROCEDURE
DBA_
PM_
sysjobhistory_driver
CREATE
PROCEDURE
DBA_sysjobhistory_driver
AS
BEGIN
SET
DEADLOCK_PRIORITY
LOW
...
...
@@ -25,7 +25,7 @@ EXEC msdb.dbo.sp_set_sqlagent_properties @jobhistory_max_rows=-1,
WHILE
(
@@
FETCH_STATUS
=
0
)
BEGIN
--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
END
CLOSE
CURSER
...
...
sysjobhistoryPurge/DBA_
PM_
sysjobhistory_driver_Tests.
sfu_dbo_test.
sql
→
sysjobhistoryPurge/DBA_sysjobhistory_driver_Tests.sql
View file @
cff0578a
File moved
sysjobhistoryPurge/README.MD
0 → 100644
View file @
cff0578a
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.
sysjobhistoryPurge/README.txt
deleted
100644 → 0
View file @
ca0ceedc
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
sysjobhistoryPurge/seeddata.sql
View file @
cff0578a
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
)
BEGIN
--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_
PM_
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
,
'ETL'
,
1
,
90
)
INSERT
INTO
DBA_sysjobhistory_config
VALUES
(
'(default)'
,
'(default)'
,
3
,
90
)
INSERT
INTO
DBA_sysjobhistory_config
VALUES
(
NULL
,
'SFU TOOL'
,
1
,
90
)
END
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment