forked from Arvindpoongothai/SQLDBAInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDB--Tempdb AutoGrowth Report
232 lines (215 loc) · 5.48 KB
/
DB--Tempdb AutoGrowth Report
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
--https://thesqlguy.wordpress.com/2011/01/01/detect-when-your-tempdb-files-have-grown-2/
--tempdb - Show files info with initial tempdb size integrated
USE [master]
IF OBJECT_ID('tempdb..#data') IS NULL
CREATE TABLE #data(
dbname VARCHAR(300),
dbcompat VARCHAR(10),
Fileid int NOT NULL,
[FileGroup] int NOT NULL,
TotalExtents int NOT NULL,
UsedExtents int NOT NULL,
[Name] sysname NOT NULL,
[FileName] varchar(300) NOT NULL,
FilePath VARCHAR(500) NULL,
Autogrowth VARCHAR(500) NULL
)
IF OBJECT_ID('tempdb..#log') IS NULL
CREATE TABLE #log(
id int identity(1,1) not null,
groupid INT DEFAULT 0 NULL,
dbname sysname NOT NULL,
dbcompat VARCHAR(10),
LogSize numeric(38,7) NOT NULL,
LogUsed numeric(38,6) NOT NULL,
Status int NOT NULL,
[Name] VARCHAR(300) NULL,
FilePath VARCHAR(500) NULL,
Autogrowth VARCHAR(500) NULL
)
USE [tempdb]
INSERT #data (Fileid, FileGroup, TotalExtents, UsedExtents, Name, FileName)
EXEC('DBCC showfilestats with no_infomsgs')
UPDATE #data SET dbname = 'tempdb'
WHERE dbname IS NULL
UPDATE #data
SET [FilePath] = s.filename,
Autogrowth = 'Autogrowth: '
+
CASE
WHEN (s.status & 0x100000 = 0 AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00)
OR s.growth = 0 THEN 'None'
WHEN s.status & 0x100000 = 0 THEN 'By ' + CONVERT(VARCHAR,CEILING((s.growth * 8192.0)/(1024.0*1024.0))) + ' MB'
ELSE 'By ' + CONVERT(VARCHAR, s.growth) + ' percent' END
+
CASE
WHEN (s.status & 0x100000 = 0 AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00)
OR s.growth = 0 THEN ''
WHEN CAST([maxsize]*8.0/1024 AS DEC(20,2)) <= 0.00 THEN ', unrestricted growth'
ELSE ', restricted growth to ' + CAST(CAST([maxsize]*8.0/1024 AS DEC(20)) AS VARCHAR) + ' MB'
END
FROM #data d
INNER JOIN dbo.sysfiles s
ON d.FileGroup = s.groupid AND d.Fileid = s.fileid
WHERE d.dbname = 'tempdb'
USE [tempdb];
INSERT #log
(
dbname,
LogSize,
LogUsed,
Status
)
EXEC('DBCC sqlperf(logspace) with no_infomsgs');
INSERT #log
(
dbname,
[groupid],
LogSize,
LogUsed,
Status,
[Name]
)
SELECT
'tempdb',
[s].[groupid],
s.[size] * 0.0078125 AS [size],
0,
0,
[name]
FROM dbo.sysfiles s
WHERE [groupid] = 0;
USE [tempdb];
UPDATE #log
SET [#log].[LogUsed] = b.[LogUsed]
FROM [#log] t
INNER JOIN
( SELECT
[l].[dbname],
[LogUsed]
FROM #log l
LEFT JOIN
( SELECT dbname,
Name,
MIN(id) AS id
FROM #log
GROUP BY dbname,
Name
) k
ON l.dbname = k.dbname
AND l.Name = k.Name
AND l.id = k.id
WHERE k.id IS NULL
)
AS b
ON [t].[dbname] = [b].[dbname];
DELETE #log
WHERE Name IS NULL;
USE [master];
UPDATE #log
SET [Name] = s.name,
[FilePath] = s.filename,
Autogrowth = 'Autogrowth: ' +
CASE
WHEN
(
s.status & 0x100000 = 0
AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00
)
OR s.growth = 0
THEN 'None'
WHEN s.status & 0x100000 = 0
THEN 'By ' + CONVERT(VARCHAR,CEILING((s.growth * 8192.0)/(1024.0*1024.0))) + ' MB'
ELSE 'By ' + CONVERT(VARCHAR, s.growth) + ' percent'
END +
CASE
WHEN
(
s.status & 0x100000 = 0
AND CEILING((s.growth * 8192.0)/(1024.0*1024.0)) = 0.00
)
OR s.growth = 0
THEN ''
WHEN CAST([maxsize]*8.0/1024 AS DEC(38,7)) <= 0.00
THEN ', unrestricted growth'
ELSE ', restricted growth to ' + CAST(CAST([maxsize]*8.0/1024 AS DEC(38,7)) AS VARCHAR) + ' MB'
END
FROM #log l
INNER JOIN [tempdb].dbo.sysfiles s
ON l.groupid = s.groupid
AND l.[Name] COLLATE DATABASE_DEFAULT = s.[name] COLLATE DATABASE_DEFAULT
WHERE l.dbname = 'tempdb';
--SELECT * FROM [#data]
--SELECT * FROM [#log] WHERE dbname = 'tempdb'
SELECT
DB_Name,
[Type],
[Name],
[FilePath],
init_size_MB,
[CurrTotalMB],
tempdb_growth_MB =
CASE
WHEN [CurrTotalMB] - init_size_MB < 0 THEN 0
ELSE [CurrTotalMB] - init_size_MB
END,
UsedMB,
FreeMB,
Autogrowth
FROM
(
SELECT
dbname AS [DB_Name],
'Data File' AS [Type],
[Name] AS [Name],
[FilePath],
init_size_MB = a.Initial_Size_MB,
(TotalExtents*64)/1024.0 AS [CurrTotalMB],
(UsedExtents*64)/1024.0 AS [UsedMB],
(TotalExtents*64)/1024.0 - (UsedExtents*64)/1024.0 AS [FreeMB],
[Autogrowth]
FROM #data d
INNER JOIN
(
SELECT
[DB_Name] = 'tempdb',
f.[name] AS logical_file_Name,
(CAST(f.[size] AS DECIMAL) * 8192) / 1024 /1024 AS Initial_Size_MB
FROM master..sysaltfiles f
INNER JOIN master..sysdatabases d
ON f.dbid = d.dbid
WHERE d.name = 'tempdb') AS a
ON d.dbname = a.[DB_Name]
AND d.[Name] = a.logical_file_Name
WHERE [dbname] = 'tempdb'
UNION
SELECT
dbname AS [DB_Name],
'TLog File',
[Name] AS [NAME],
[FilePath],
init_size_MB = a.Initial_Size_MB,
LogSize,
((LogUsed/100)*LogSize),
LogSize - ((LogUsed/100)*LogSize),
[Autogrowth]
FROM #log l
INNER JOIN
(
SELECT
[DB_Name] = 'tempdb',
f.[name] AS logical_file_Name,
(CAST(f.[size] AS DECIMAL) * 8192) / 1024 /1024 AS Initial_Size_MB
FROM master..sysaltfiles f
INNER JOIN master..sysdatabases d
ON f.dbid = d.dbid
WHERE d.name = 'tempdb') AS a
ON l.dbname = a.[DB_Name]
AND l.[Name] = a.logical_file_Name
WHERE [dbname] = 'tempdb'
) AS r
ORDER BY 1, [Type], [Name]
IF OBJECT_ID('tempdb..#data') IS NOT NULL
DROP TABLE [#data]
IF OBJECT_ID('tempdb..#log') IS NOT NULL
DROP TABLE [#log]