Skip to content

Commit 1f4b7f7

Browse files
authored
Initial commit for Database File Metrics
1 parent 26a5615 commit 1f4b7f7

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

database-file-metrics-install.sql

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/**************************************************************************
2+
DATABASE FILE METRICS
3+
Author: Eric Cobb - http://www.sqlnuggets.com/
4+
Supported Versions: SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, and SQL Server 2016
5+
License:
6+
MIT License
7+
8+
Copyright (c) 2017 Eric Cobb
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy
11+
of this software and associated documentation files (the "Software"), to deal
12+
in the Software without restriction, including without limitation the rights
13+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
copies of the Software, and to permit persons to whom the Software is
15+
furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
***************************************************************************/
28+
29+
--Change this to whatever database you want to create the Metrics objects in.
30+
USE [SQLMetrics]
31+
GO
32+
33+
34+
/**************************************************************************
35+
Create Table(s)
36+
***************************************************************************/
37+
GO
38+
--If our DatabaseFileMetrics table doesn't already exist, create it.
39+
IF OBJECT_ID('dbo.DatabaseFileMetrics') IS NULL
40+
BEGIN
41+
CREATE TABLE [dbo].[DatabaseFileMetrics](
42+
[ID] [bigint] IDENTITY(1,1) NOT NULL CONSTRAINT [PK_DatabaseFileMetrics] PRIMARY KEY CLUSTERED,
43+
[DatabaseID] [smallint] NULL,
44+
[DatabaseName] [nvarchar](128) NULL,
45+
[FileID] [int] NOT NULL,
46+
[FileName] [nvarchar](128) NOT NULL,
47+
[FileType] [nvarchar](60) NULL,
48+
[FileLocation] [nvarchar](260) NOT NULL,
49+
[CurrentState] [nvarchar](60) NULL,
50+
[isReadOnly] [bit] NOT NULL,
51+
[CurrentSizeMB] [decimal](10, 2) NULL,
52+
[SpaceUsedMB] [decimal](10, 2) NULL,
53+
[PercentUsed] [decimal](10, 2) NULL,
54+
[FreeSpaceMB] [decimal](10, 2) NULL,
55+
[PercentFree] [decimal](10, 2) NULL,
56+
[AutoGrowth] [nvarchar](128) NULL,
57+
[CaptureDate] [datetime] NOT NULL CONSTRAINT [DF_DatabaseFileMetrics_CaptureDate] DEFAULT (getdate())
58+
);
59+
60+
END
61+
GO
62+
63+
64+
/**************************************************************************
65+
Create Stored Procedure(s)
66+
***************************************************************************/
67+
GO
68+
--If our procedure doesn't already exist, create one with a dummy query to be overwritten.
69+
IF OBJECT_ID('dbo.loadDatabaseFileMetrics') IS NULL
70+
EXEC sp_executesql N'CREATE PROCEDURE dbo.loadDatabaseFileMetrics AS SELECT 1;';
71+
GO
72+
73+
ALTER PROCEDURE [dbo].[loadDatabaseFileMetrics]
74+
@DBName sysname --Name of the Database you want to collect Database File Metrics for.
75+
AS
76+
77+
/**************************************************************************
78+
Author: Eric Cobb - http://www.sqlnuggets.com/
79+
License:
80+
MIT License
81+
Copyright (c) 2017 Eric Cobb
82+
View full license disclosure: https://github.com/ericcobb/SQL-Server-Metrics-Pack/blob/master/LICENSE
83+
84+
Purpose:
85+
This stored procedure is used to collect Database File Metrics from multiple DMVs,
86+
this data is persisted in the dbo.DatabaseFileMetrics table.
87+
88+
Parameters:
89+
@DBName - Name of the Database you want to collect Index Metrics for.
90+
91+
Usage:
92+
--Collect Database File Metrics for the MyDB database
93+
EXEC [dbo].[loadDatabaseFileMetrics] @DBName='MyDB';
94+
95+
Knows Issues:
96+
@DBName currently only supports a single database, so the loadDatabaseFileMetrics procedure will have to be run individually for
97+
each database that you want to gather Index Metrics for. Future enhancements will allow for muliple databases.
98+
***************************************************************************/
99+
100+
BEGIN
101+
102+
SET NOCOUNT ON
103+
104+
DECLARE @sql NVARCHAR(MAX)
105+
DECLARE @crlf NCHAR(2) = NCHAR(13)+NCHAR(10)
106+
DECLARE @CaptureDate [datetime] = SYSDATETIME()
107+
108+
CREATE TABLE #DBFileInfo(
109+
[DatabaseID] [smallint] NULL,
110+
[DatabaseName] [nvarchar](128) NULL,
111+
[FileID] [int] NOT NULL,
112+
[FileName] [nvarchar](128) NOT NULL,
113+
[FileType] [nvarchar](60) NULL,
114+
[FileLocation] [nvarchar](260) NOT NULL,
115+
[CurrentState] [nvarchar](60) NULL,
116+
[isReadOnly] [bit] NOT NULL,
117+
[CurrentSizeMB] [decimal](10, 2) NULL,
118+
[SpaceUsedMB] [decimal](10, 2) NULL,
119+
[PercentUsed] [decimal](10, 2) NULL,
120+
[FreeSpaceMB] [decimal](10, 2) NULL,
121+
[PercentFree] [decimal](10, 2) NULL,
122+
[AutoGrowth] [varchar](128) NULL
123+
) ON [PRIMARY]
124+
125+
126+
SET @sql = '
127+
USE '+ @DBName +'
128+
' + @crlf
129+
130+
--Load the Index Metrics into our Temp table
131+
SET @sql = @sql + 'INSERT INTO #DBFileInfo ([DatabaseID],[DatabaseName],[FileID],[FileName],[FileType],[FileLocation],[CurrentState],[isReadOnly],[CurrentSizeMB],[SpaceUsedMB],[PercentUsed],[FreeSpaceMB],[PercentFree],[AutoGrowth])
132+
SELECT [DatabaseID] = DB_ID()
133+
,[DatabaseName] = DB_NAME()
134+
,[FileID] = f.file_id
135+
,[FileName] = f.name
136+
,[FileType] = f.type_desc
137+
,[FileLocation] = f.physical_name
138+
,[CurrentState] = f.state_desc
139+
,[isReadOnly] = f.is_read_only
140+
,[CurrentSizeMB] = CONVERT(DECIMAL(10,2),f.SIZE/128.0)
141+
,[SpaceUsedMB] = CONVERT(DECIMAL(10,2),CAST(FILEPROPERTY(f.name, ''SPACEUSED'') AS INT)/128.0)
142+
,[PercentUsed] = CAST((CAST(FILEPROPERTY(f.name, ''SPACEUSED'')/128.0 AS DECIMAL(10,2))/CAST(f.SIZE/128.0 AS DECIMAL(10,2)))*100 AS DECIMAL(10,2))
143+
,[FreeSpaceMB] = CONVERT(DECIMAL(10,2),f.SIZE/128.0 - CAST(FILEPROPERTY(f.name, ''SPACEUSED'') AS INT)/128.0)
144+
,[PercentFree] = CONVERT(DECIMAL(10,2),((f.SIZE/128.0 - CAST(FILEPROPERTY(f.name, ''SPACEUSED'') AS INT)/128.0)/(f.SIZE/128.0))*100)
145+
,[AutoGrowth] = ''By '' + CASE is_percent_growth
146+
WHEN 0 THEN CAST(f.GROWTH/128 AS VARCHAR(10)) + '' MB -''
147+
WHEN 1 THEN CAST(f.GROWTH AS VARCHAR(10)) + ''% -'' ELSE '''' END
148+
+ CASE max_size
149+
WHEN 0 THEN ''DISABLED''
150+
WHEN -1 THEN '' Unrestricted''
151+
ELSE '' Restricted to '' + CAST(max_size/(128*1024) AS VARCHAR(10)) + '' GB'' END
152+
FROM sys.master_files f --use sys.master_files instead of sys.database_files, because if the database is hosted by an AlwaysOn readable secondary replica, sys.database_files.physical_name indicates the file location of the primary replica database instead.
153+
WHERE f.database_id = DB_ID()
154+
OPTION (MAXDOP 2);'
155+
156+
--run the generated T-SQL
157+
EXECUTE sp_executesql @sql
158+
159+
INSERT INTO [dbo].[DatabaseFileMetrics]
160+
([DatabaseID]
161+
,[DatabaseName]
162+
,[FileID]
163+
,[FileName]
164+
,[FileType]
165+
,[FileLocation]
166+
,[CurrentState]
167+
,[isReadOnly]
168+
,[CurrentSizeMB]
169+
,[SpaceUsedMB]
170+
,[PercentUsed]
171+
,[FreeSpaceMB]
172+
,[PercentFree]
173+
,[AutoGrowth]
174+
,[CaptureDate])
175+
SELECT [DatabaseID]
176+
,[DatabaseName]
177+
,[FileID]
178+
,[FileName]
179+
,[FileType]
180+
,[FileLocation]
181+
,[CurrentState]
182+
,[isReadOnly]
183+
,[CurrentSizeMB]
184+
,[SpaceUsedMB]
185+
,[PercentUsed]
186+
,[FreeSpaceMB]
187+
,[PercentFree]
188+
,[AutoGrowth]
189+
,@CaptureDate
190+
FROM #DBFileInfo
191+
192+
DROP TABLE #DBFileInfo
193+
END
194+
GO
195+
196+
197+
/**************************************************************************
198+
Create View(s)
199+
***************************************************************************/
200+
GO
201+
--If our view doesn't already exist, create one with a dummy query to be overwritten.
202+
IF OBJECT_ID('dbo.vwDBFileMetrics_CurrentFileSizes') IS NULL
203+
EXEC sp_executesql N'CREATE VIEW [dbo].[vwDBFileMetrics_CurrentFileSizes] AS SELECT [DatabaseName] FROM [dbo].[DatabaseFileMetrics];';
204+
GO
205+
206+
ALTER VIEW dbo.vwDBFileMetrics_CurrentFileSizes
207+
AS
208+
209+
/**************************************************************************
210+
Author: Eric Cobb - http://www.sqlnuggets.com/
211+
License:
212+
MIT License
213+
Copyright (c) 2017 Eric Cobb
214+
View full license disclosure: https://github.com/ericcobb/SQL-Server-Metrics-Pack/blob/master/LICENSE
215+
Purpose:
216+
This view queries the DatabaseFileMetrics table to return the most recently recorded
217+
data and log file metrics.
218+
***************************************************************************/
219+
220+
SELECT [DatabaseName]
221+
,[FileName]
222+
,[FileType]
223+
,[CurrentSize]
224+
,[SpaceUsed]
225+
,[PercentUsed]
226+
,[FreeSpace]
227+
,[PercentFree]
228+
,[AutoGrowth]
229+
,[CaptureDate]
230+
FROM (SELECT [DatabaseName]
231+
,[FileName]
232+
,[FileType]
233+
,[CurrentSize] = Cast([CurrentSizeMB] AS varchar(25))+' MB'
234+
,[SpaceUsed] = Cast([SpaceUsedMB] AS varchar(25))+' MB'
235+
,[PercentUsed] = Cast([PercentUsed] AS varchar(25))+'%'
236+
,[FreeSpace] = Cast([FreeSpaceMB] AS varchar(25))+' MB'
237+
,[PercentFree] = Cast([PercentFree] AS varchar(25))+'%'
238+
,[AutoGrowth]
239+
,[CaptureDate]
240+
,ROW_NUMBER() OVER (PARTITION BY [DatabaseID],[FileID] ORDER BY [CaptureDate] DESC) AS rn
241+
FROM [dbo].[DatabaseFileMetrics]
242+
) fm
243+
WHERE fm.rn = 1;
244+
245+
GO

0 commit comments

Comments
 (0)