forked from himadanreddy/SQL-DBA-SCRIPTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path040.pr_Set_CheckDB_Schedule.sql
More file actions
408 lines (358 loc) · 21.1 KB
/
040.pr_Set_CheckDB_Schedule.sql
File metadata and controls
408 lines (358 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
USE [DBAdmin]
GO
IF dbo.fn_SQLVersion() >= 9
BEGIN
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pr_Set_CheckDB_Schedule]') AND type = N'P')
BEGIN
PRINT 'Dropping Procedure [pr_Set_CheckDB_Schedule] - SQL 2005'
DROP PROCEDURE [dbo].[pr_Set_CheckDB_Schedule]
END
END
IF dbo.fn_SQLVersion() < 9
BEGIN
PRINT 'DBAdmin CheckDB processing is not written for SQL 2000. Use the Standard Maintenance Plan processing.'
PRINT '*** Processing of this script is being aborted ***'
RAISERROR ('DBAdmin CheckDB processing is not written for SQL 2000', 20, 1) WITH LOG
END
GO
CREATE PROCEDURE [dbo].[pr_Set_CheckDB_Schedule]
-- --------------------------------------------------------------------------------------------------
-- Procedure : [pr_Set_CheckDB_Schedule]
-- Version : 1.0
-- Description : Set the Schedule for the CHECKDB / CHECKTABLE process
-- On the first day of each schedule, the schedule is rebuilt. This ensure that any
-- recently added objects are included in the schedule.
--
-- Modification Log
-- When Who Description
-- 12/24/2009 Simon Facer Original Version
-- --------------------------------------------------------------------------------------------------
AS
BEGIN
SET NOCOUNT ON
-- ******************************************************************************************
-- Declare local variables
DECLARE @SQLVersion INT
DECLARE @SQLCmd VARCHAR(MAX)
DECLARE @ScheduleID INT
DECLARE @SplitOverDays INT
DECLARE @DBName SYSNAME
DECLARE @TableName SYSNAME
DECLARE @DBGroup VARCHAR(16)
DECLARE @IncludeDBs VARCHAR(2048)
DECLARE @ExcludeDBs VARCHAR(2048)
DECLARE @REservedPC BIGINT
DECLARE @ScheduleDay INT
-- ******************************************************************************************
-- ******************************************************************************************
-- Create the # temp tables used in the proc
CREATE TABLE #SchedulesToProcess
(ScheduleID INT)
-- Create the # temp table to identify the databases to be processed
CREATE TABLE #Databases
(DBName SYSNAME,
StateDesc VARCHAR(60))
-- Candidate Tables for processing
CREATE TABLE #PoolOfTables (
DatabaseName SYSNAME,
TableName SYSNAME,
ObjectID INT,
ReservedPC BIGINT)
-- Info for tables with XML or Fulltext Indexes
CREATE TABLE #TablesOtherPages (
ObjectID INT,
ReservedPC BIGINT)
-- Tables parsed into Multiple days
CREATE TABLE #CHECKDB_Schedule (
ScheduleDay INT,
DatabaseName SYSNAME NULL,
TableName SYSNAME NULL,
ReservedPC BIGINT NULL,)
-- ******************************************************************************************
-- ******************************************************************************************
-- Determine the target SQL Server's version,
-- 8 = 2000 NOT Supported by this proc
-- 9 = 2005
-- 10 = 2008
SELECT @SQLVersion = dbo.fn_SQLVersion()
-- ******************************************************************************************
-- ******************************************************************************************
-- Identify any Schedules that need to have the Schedule recalculated.
-- All split-day Schedules are recalculated at day 0. This allows for any newly-added
-- tables to be included in the next cycle, and also re-apportions based on changing
-- table sizes.
-- Any split-day Schedule that doesnt have any Schedule records is calculated .
INSERT #SchedulesToProcess
SELECT ScheduleID
FROM CHECKDB_Parms
WHERE dbo.fn_CheckDB_ScheduleDay (ScheduleID, GETDATE()) = 0
AND SplitOverDays > 1
UNION
SELECT t.ScheduleID
FROM (SELECT p.ScheduleID,
COUNT(s.ScheduleID) AS ScheduleRows
FROM CHECKDB_Parms p
LEFT OUTER JOIN CHECKDB_Schedule s
ON p.ScheduleID = s.ScheduleID
WHERE SplitOverDays > 1
GROUP BY p.ScheduleID
HAVING COUNT(s.ScheduleID) = 0) AS t
-- ******************************************************************************************
-- ******************************************************************************************
-- Delete the existing Schedules
DELETE dbo.CHECKDB_Schedule
WHERE ScheduleID IN (SELECT ScheduleID
FROM #SchedulesToProcess)
-- ******************************************************************************************
-- ******************************************************************************************
-- Loop through all the Schedules To Process
DECLARE csrSchedule CURSOR FOR
SELECT ScheduleID
FROM #SchedulesToProcess
-- ******************************************************************************************
-- ******************************************************************************************
-- Open and retrieve the first value from the cursor
OPEN csrSchedule
FETCH NEXT FROM csrSchedule
INTO @ScheduleID
-- ******************************************************************************************
-- ******************************************************************************************
-- Loop through Schedules
WHILE @@FETCH_STATUS = 0
BEGIN
-- + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
-- ******************************************************************************************
SELECT @DBGroup = DBGroup,
@IncludeDBs = IncludeDBs,
@ExcludeDBs = ExcludeDBs,
@SplitOverDays = SplitOverDays
FROM dbo.CHECKDB_Parms
WHERE ScheduleID = @ScheduleID
-- ******************************************************************************************
-- ******************************************************************************************
-- Clear #temp tables
DELETE #Databases
DELETE #PoolOfTables
DELETE #TablesOtherPages
DELETE #CHECKDB_Schedule
-- ******************************************************************************************
-- ******************************************************************************************
-- Populate the #Databases table with all the databases on the server
INSERT #Databases
SELECT DBName, StateDesc
FROM [dbo].[fn_DatabaseDetails] ()
-- ******************************************************************************************
-- ******************************************************************************************
-- Remove offline databases from the list
DELETE #Databases
WHERE UPPER(StateDesc) != 'ONLINE'
-- ******************************************************************************************
-- ******************************************************************************************
-- Process the IncludeDBs parameter to add '[' and ']' values.
SELECT @IncludeDBs = '[' + REPLACE(@IncludeDBs, ',', '],[') + ']'
WHILE CHARINDEX('[ ', @IncludeDBs) > 0
BEGIN
SELECT @IncludeDBs = REPLACE(@IncludeDBs, '[ ', '[')
END
-- ******************************************************************************************
-- ******************************************************************************************
-- Process the ExcludeDBs parameter to add '[' and ']' values.
SELECT @ExcludeDBs = '[' + REPLACE(@ExcludeDBs, ',', '],[') + ']'
WHILE CHARINDEX('[ ', @ExcludeDBs) > 0
BEGIN
SELECT @ExcludeDBs = REPLACE(@ExcludeDBs, '[ ', '[')
END
-- ******************************************************************************************
-- ******************************************************************************************
-- If a Group was specified, filter the database names
IF @DBGroup IS NOT NULL
BEGIN
IF @DBGroup = 'System'
BEGIN
DELETE #Databases
WHERE DBName NOT IN ('master', 'model', 'msdb')
END
ELSE
BEGIN
IF @DBGroup = 'User'
BEGIN
DELETE #Databases
WHERE DBName IN ('master', 'model', 'msdb')
END
END
IF @ExcludeDBs IS NOT NULL AND
LTRIM(RTRIM(@ExcludeDBs)) != ''
BEGIN
DELETE #Databases
WHERE CHARINDEX(('[' + DBName + ']'), @ExcludeDBs) > 0
END
END
-- ******************************************************************************************
-- ******************************************************************************************
-- If a list of databases to include was specified and a DBGroup wasn't, process the
-- include list.
IF @DBGroup IS NULL AND
(LTRIM(RTRIM(@IncludeDBs)) != '')
BEGIN
DELETE #Databases
WHERE CHARINDEX(('[' + DBName + ']'), @IncludeDBs) = 0
END
-- ******************************************************************************************
-- ******************************************************************************************
-- Insert Base records into the CheckDB_Schedule table for each day
INSERT #CHECKDB_Schedule
SELECT Number,
NULL,
NULL,
0
FROM dbo.fn_GenerateNumbers (1, @SplitOverDays)
-- ******************************************************************************************
-- ******************************************************************************************
-- Loop through all the databases to be processed:
WHILE EXISTS (SELECT *
FROM #Databases)
BEGIN
-- ******************************************************************************************
-- ******************************************************************************************
-- Identify the next database to process, and remove that record
SELECT @DBName = t.DBName
FROM (SELECT TOP 1 DBName
FROM #Databases
ORDER BY DBName) as t
DELETE #Databases
WHERE DBName = @DBName
-- ******************************************************************************************
-- ******************************************************************************************
-- Identify all the tables and Reserved Page Counts in each database
IF @SQLVersion = 10
BEGIN
-- User Tables, System Base Table, Indexed Views, Internal Tables
SELECT @SQLCmd =
N'USE [' + @DBName + N'];' +
N'SELECT ''' + @DBName + N''', SCHEMA_NAME(o.schema_id) + N''.'' + o.name, ' +
N' o.object_id, SUM(ps.reserved_page_count)' +
N' FROM [' + @DBName + N'].sys.objects o WITH (NOLOCK)' +
N' INNER JOIN [' + @DBName + N'].sys.dm_db_partition_stats ps WITH (NOLOCK)' +
N' ON o.object_id = ps.object_id' +
N' WHERE o.[type] IN (N''U'', N''S'', N''V'', N''IT'')' +
N' GROUP BY SCHEMA_NAME(o.schema_id) + N''.'' + o.name, o.object_id'
END
ELSE
BEGIN
-- User Tables, Indexed Views, Internal Tables
SELECT @SQLCmd =
N'USE [' + @DBName + N'];' +
N'SELECT ''' + @DBName + N''', SCHEMA_NAME(o.schema_id) + N''.'' + o.name, ' +
N' o.object_id, SUM(ps.reserved_page_count)' +
N' FROM [' + @DBName + N'].sys.objects o WITH (NOLOCK)' +
N' INNER JOIN [' + @DBName + N'].sys.dm_db_partition_stats ps WITH (NOLOCK)' +
N' ON o.object_id = ps.object_id' +
N' WHERE o.[type] IN (N''U'', N''V'', N''IT'')' +
N' GROUP BY SCHEMA_NAME(o.schema_id) + N''.'' + o.name, o.object_id'
END
INSERT INTO #PoolOfTables EXEC (@SQLCmd)
-- ******************************************************************************************
-- ******************************************************************************************
-- Check if tables have XML Indexes or Fulltext Indexes which use internal tables tied to this table
-- Row counts in these internal tables don't contribute towards row count of original table.
SELECT @SQLCmd =
N'USE [' + @DBName + N'];' +
N'SELECT it.object_id, sum(ps.reserved_page_count)' +
N' FROM [' + @DBName + N'].sys.dm_db_partition_stats ps WITH (NOLOCK)' +
N' INNER JOIN [' + @DBName + N'].sys.internal_tables it WITH (NOLOCK)' +
N' ON ps.object_id = it.object_id' +
N' WHERE it.internal_type IN (202,204)' +
N' GROUP BY it.object_id'
INSERT INTO #TablesOtherPages EXEC (@SQLCmd)
-- ******************************************************************************************
-- ******************************************************************************************
-- Check if tables have XML Indexes or Fulltext Indexes which use internal tables tied to this table
UPDATE #PoolOfTables
SET ReservedPC = p.ReservedPC + COALESCE(op.ReservedPC, 0)
FROM #PoolOfTables p
INNER JOIN #TablesOtherPages op
ON p.ObjectID = op.ObjectID
-- ******************************************************************************************
-- ******************************************************************************************
END
-- End Database Loop
-- ******************************************************************************************
-- ******************************************************************************************
-- The previous Loop collected Reserved Page counts for all tables in all databases for the
-- Schedule currently being processed.
-- This loop will apportion those tables over the specified number of days, this processing
-- will even out the processing by day, as much as possible.
WHILE EXISTS (SELECT *
FROM #PoolOfTables)
BEGIN
-- ******************************************************************************************
-- ******************************************************************************************
-- Retrieve the next largest Table
SELECT @DBName = t.DatabaseName,
@TableName = t.TableName,
@ReservedPC = t.ReservedPC
FROM ( SELECT TOP 1
DatabaseName,
TableName,
ReservedPC
FROM #PoolOfTables
ORDER BY ReservedPC DESC) AS t
DELETE #PoolOfTables
WHERE DatabaseName = @DBName
AND TableName = @TableName
-- ******************************************************************************************
-- ******************************************************************************************
-- Identify the ScheduleDay with the smallest total PageCount allocated
SELECT @ScheduleDay = t.ScheduleDay
FROM ( SELECT TOP 1
ScheduleDay,
(SUM(ReservedPC) + @ReservedPC) AS Total_ReservedPC
FROM #CHECKDB_Schedule
GROUP BY ScheduleDay
ORDER BY (SUM(ReservedPC) + @ReservedPC),
ScheduleDay) AS t
-- ******************************************************************************************
-- ******************************************************************************************
-- Insert the record into the #CHECKDB_Schedule table
INSERT #CHECKDB_Schedule
VALUES (@ScheduleDay,
@DbName,
@TableName,
@ReservedPC)
-- ******************************************************************************************
-- ******************************************************************************************
END
-- End Table Scheduling Loop
-- ******************************************************************************************
-- ******************************************************************************************
-- Load the Schedule Data into the permanent table
INSERT dbo.CHECKDB_Schedule
SELECT @ScheduleID,
ScheduleDay,
DatabaseName,
TableName
FROM #CHECKDB_Schedule
WHERE DatabaseName IS NOT NULL
ORDER BY DatabaseName,
TableName
-- ******************************************************************************************
-- ******************************************************************************************
-- Retrieve the next value from the cursor
FETCH NEXT FROM csrSchedule
INTO @ScheduleID
-- ******************************************************************************************
-- + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
END
-- End of the Cursor loop (ScheduleID)
-- ******************************************************************************************
-- ******************************************************************************************
-- Close and Deallocate the Cursor
CLOSE csrSchedule
DEALLOCATE csrSchedule
-- ******************************************************************************************
END
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pr_Set_CheckDB_Schedule]') AND type = N'P')
BEGIN
PRINT 'Procedure [pr_Set_CheckDB_Schedule] Created - SQL 2005'
END