-
Notifications
You must be signed in to change notification settings - Fork 6
/
SQLVLF_Reduction.ps1
90 lines (75 loc) · 2.75 KB
/
SQLVLF_Reduction.ps1
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
:connect <<pSQLServerName>>
print 'Executing on server: ' + @@ServerName
USE [<<pDatabase>>]
GO
DECLARE @query VARCHAR(1000) ,
@count INT,
@file_name sysname ,
@file_size INT ,
@Chunk_Size INT ,
@shrink_command NVARCHAR(MAX) ,
@alter_command NVARCHAR(MAX)
CREATE TABLE #log_info
(
fileid TINYINT ,
file_size BIGINT ,
start_offset BIGINT ,
FSeqNo INT ,
[status] TINYINT ,
parity TINYINT ,
create_lsn NUMERIC(25, 0)
)
SET @query = 'DBCC loginfo (' + '''' + DB_NAME() + ''') '
INSERT INTO #log_info
EXEC ( @query
)
SET @count = @@rowcount
SELECT DB_NAME() AS dbname ,
@count AS num_of_VLFs
IF @Count > 50
BEGIN
SET @Chunk_Size = 0
SELECT @file_name = name ,
@file_size = ( size / 128 )
FROM sys.database_files
WHERE type_desc = 'log'
SELECT @file_name AS LogFileName ,
@file_size AS LogFileSizeMB
IF @File_Size > 12000 --Large tran log so round up to next 4000MB
SET @File_Size = ( (@File_Size-1) / 4000 ) * 4000 + 4000 --round up to next 4000MB
ELSE
IF @File_Size > 6000 --Medium size to round up to next 1000MB
SET @File_Size = ((@File_Size-1) / 1000 ) * 1000 + 1000 --round up to next 1000MB
ELSE
SET @File_Size = ( (@File_Size-1) / 100 ) * 100 + 100 --round up to next 100MB
SELECT @file_name AS LogFileName ,
@file_size AS LogFileNewSizeMB
SELECT @shrink_command = 'DBCC SHRINKFILE (N''' + @file_name
+ ''' , 0, TRUNCATEONLY)'
RAISERROR (@shrink_command, 0, 1) WITH NOWAIT
EXEC sp_executesql @shrink_command
SELECT @shrink_command = 'DBCC SHRINKFILE (N''' + @file_name
+ ''' , 0)'
RAISERROR (@shrink_command, 0, 1) WITH NOWAIT
EXEC sp_executesql @shrink_command
WHILE @Chunk_Size < @file_size
BEGIN
IF @File_Size - @Chunk_Size < 8000
SET @chunk_Size = @file_size
ELSE
SET @Chunk_Size = @Chunk_Size + 8000 --Grow in 8000MB chunks
SELECT @alter_command = 'ALTER DATABASE [' + DB_NAME()
+ '] MODIFY FILE (NAME = N''' + @file_name
+ ''', SIZE = ' + CAST(@chunk_size AS NVARCHAR)
+ 'MB)'
RAISERROR (@alter_command, 0, 1) WITH NOWAIT
EXEC sp_executesql @alter_command
END
INSERT INTO #log_info
EXEC ( @query
)
SET @count = @@rowcount
SELECT DB_NAME() AS dbname ,
@count AS num_of_VLFs
END
DROP TABLE #log_info