Skip to content

Commit b83e5b3

Browse files
committed
Added Functionality to get result for any type of drives
Added Functionality to get result for any type of drives
1 parent d3a3975 commit b83e5b3

File tree

4 files changed

+4402
-12
lines changed

4 files changed

+4402
-12
lines changed
0 Bytes
Binary file not shown.

SpaceCapacity-Automation/Automation - Restrict File growth.sql

+98-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GO
1414
-- DECLARE @_errorOccurred BIT; EXEC @_errorOccurred = tempdb..[usp_AnalyzeSpaceCapacity] ; SELECT CASE WHEN @_errorOccurred = 1 THEN 'fail' ELSE 'pass' END AS [Pass/Fail];
1515
ALTER PROCEDURE [dbo].[usp_AnalyzeSpaceCapacity]
1616
@getInfo TINYINT = 0, @getLogInfo TINYINT = 0, @volumeInfo TINYINT = 0, @help TINYINT = 0, @addDataFiles TINYINT = 0, @addLogFiles TINYINT = 0, @restrictDataFileGrowth TINYINT = 0, @restrictLogFileGrowth TINYINT = 0, @generateCapacityException TINYINT = 0, @unrestrictFileGrowth TINYINT = 0, @removeCapacityException TINYINT = 0, @UpdateMountPointSecurity TINYINT = 0, @restrictMountPointGrowth TINYINT = 0, @expandTempDBSize TINYINT = 0, @optimizeLogFiles TINYINT = 0,
17-
@newVolume VARCHAR(50) = NULL, @oldVolume VARCHAR(50) = NULL, @mountPointGrowthRestrictionPercent TINYINT = 79, @tempDBMountPointPercent TINYINT = 89, @DBs2Consider VARCHAR(1000) = NULL, @mountPointFreeSpaceThreshold_GB INT = 60
17+
@newVolume VARCHAR(50) = NULL, @oldVolume VARCHAR(50) = NULL, @mountPointGrowthRestrictionPercent TINYINT = 79, @tempDBMountPointPercent TINYINT = NULL, @tempDbMaxSizeThresholdInGB INT = NULL, @DBs2Consider VARCHAR(1000) = NULL, @mountPointFreeSpaceThreshold_GB INT = 60
1818
,@verbose TINYINT = 0 ,@testAllOptions TINYINT = 0 ,@forceExecute TINYINT = 0 ,@allowMultiVolumeUnrestrictedFiles TINYINT = 0 ,@output4IdealScenario TINYINT = 0
1919
AS
2020
BEGIN
@@ -43,6 +43,20 @@ BEGIN
4343
ErrorDetails TEXT NOT NULL,
4444
TSQLCode TEXT NULL
4545
);
46+
47+
-- Declare table for Successful/Failure Output
48+
DECLARE @OutputMessages TABLE
49+
(
50+
MessageID INT IDENTITY(1,1),
51+
Status VARCHAR(15),
52+
Category VARCHAR(100), -- 'Compilation Error', 'Runtime Time', 'ALTER DATABASE Error', 'Add Data File', 'Add Log File'
53+
DBName SYSNAME NULL,
54+
[FileGroup] SYSNAME NULL,
55+
[FileName] SYSNAME NULL,
56+
MessageDetails TEXT NOT NULL,
57+
TSQLCode TEXT NULL
58+
);
59+
4660
-- Declare variable to check if any error occurred
4761
DECLARE @_errorOccurred BIT
4862
SET @_errorOccurred = 0;
@@ -71,8 +85,10 @@ BEGIN
7185
,@_loopCounts SMALLINT
7286
,@_loopSQLText VARCHAR(MAX)
7387
,@_dbName VARCHAR(255)
88+
,@_fileGroup SYSNAME
7489
,@_name VARCHAR(255)
7590
,@_newName VARCHAR(255)
91+
,@_oldVolumesSpecified VARCHAR(200) -- Store comma separated volume names for @oldVolume
7692
,@_capacityExceptionSQLText VARCHAR(MAX)
7793
,@_svrName VARCHAR(255)
7894
,@_sqlGetMountPointVolumes VARCHAR(400)
@@ -87,6 +103,7 @@ BEGIN
87103
,@_productVersion VARCHAR(20)
88104
,@_SpaceToBeFreed_MB DECIMAL(20,2)
89105
,@_sqlText NVARCHAR(4000) -- Can be used for any dynamic queries
106+
,@_procSTMT_Being_Executed VARCHAR(2000);
90107

91108
DECLARE @_logicalCores TINYINT
92109
,@_fileCounts TINYINT
@@ -190,7 +207,7 @@ DECLARE @_logicalCores TINYINT
190207
[Volume] [varchar](200) NULL,
191208
[VolumeName] [varchar](20) NULL,
192209
[VolumeSize_MB] [decimal](20, 2) NULL
193-
,[isToBeDeleted] AS CASE WHEN [VolumeName] LIKE '%TempDB%' THEN 0 ELSE 1 END
210+
,[isToBeDeleted] BIT DEFAULT 0
194211
);
195212

196213

@@ -236,6 +253,38 @@ DECLARE @_logicalCores TINYINT
236253
SET @getInfo = 1;
237254
END
238255

256+
-- Set Final Size thresholds for TempDb
257+
IF (@expandTempDBSize = 1)
258+
BEGIN
259+
IF @verbose=1
260+
PRINT ' Evaluation value of @tempDbMaxSizeThresholdInGB and @tempDBMountPointPercent variables';
261+
262+
-- If both parameter are provided, throw error.
263+
IF (@tempDbMaxSizeThresholdInGB IS NOT NULL AND @tempDBMountPointPercent IS NOT NULL)
264+
BEGIN
265+
SET @_errorMSG = 'Kindly provide only one parameter. Either @tempDbMaxSizeThresholdInGB or @tempDBMountPointPercent.';
266+
IF (select CAST(LEFT(CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(50)),charindex('.',CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR(50)))-1) AS INT)) >= 12
267+
EXEC sp_executesql N'THROW 50000,@_errorMSG,1',N'@_errorMSG VARCHAR(200)', @_errorMSG;
268+
ELSE
269+
EXEC sp_executesql N'RAISERROR (@_errorMSG, 16, 1)', N'@_errorMSG VARCHAR(200)', @_errorMSG;
270+
END
271+
272+
-- If both parameters are NULL, use @tempDbMaxSizeThresholdInGB with default
273+
IF (@tempDbMaxSizeThresholdInGB IS NULL AND @tempDBMountPointPercent IS NULL)
274+
BEGIN
275+
SET @_errorMSG = ' /* Value for neither @tempDbMaxSizeThresholdInGB or @tempDBMountPointPercent is provided. So, proceeding with @tempDbMaxSizeThresholdInGB = ';
276+
SET @tempDbMaxSizeThresholdInGB = 16;
277+
278+
IF @forceExecute = 0
279+
BEGIN
280+
SET @_errorMSG = @_errorMSG + CAST( @tempDbMaxSizeThresholdInGB AS VARCHAR(5)) + '
281+
*/';
282+
PRINT @_errorMSG;
283+
END
284+
END
285+
END
286+
287+
-- Check if valid parameter is selected for procedure
239288
IF (COALESCE(@getInfo,@help,@addDataFiles,@addLogFiles,@restrictDataFileGrowth,@restrictLogFileGrowth,@generateCapacityException,@unrestrictFileGrowth
240289
,@restrictMountPointGrowth,@expandTempDBSize,-999) = -999)
241290
BEGIN
@@ -246,6 +295,7 @@ DECLARE @_logicalCores TINYINT
246295
EXEC sp_executesql N'RAISERROR (@_errorMSG, 16, 1)', N'@_errorMSG VARCHAR(200)', @_errorMSG;
247296
END
248297

298+
-- Check if valid parameter is selected for procedure
249299
IF (@help = 1 AND (@addDataFiles=1 OR @addLogFiles=1 OR @restrictDataFileGrowth=1 OR @restrictLogFileGrowth=1 OR @generateCapacityException=1 OR @unrestrictFileGrowth=1 OR @removeCapacityException=1 OR @expandTempDBSize=1 ))
250300
BEGIN
251301
SET @_errorMSG = '@help=1 is incompatible with any other parameters.';
@@ -255,6 +305,7 @@ DECLARE @_logicalCores TINYINT
255305
EXEC sp_executesql N'RAISERROR (@_errorMSG, 16, 1)', N'@_errorMSG VARCHAR(200)', @_errorMSG;
256306
END
257307

308+
-- Check if valid parameter is selected for procedure
258309
IF (@generateCapacityException = 1 AND (@addDataFiles=1 OR @restrictDataFileGrowth=1 OR @unrestrictFileGrowth=1 OR @help=1 OR @removeCapacityException=1))
259310
BEGIN
260311
SET @_errorMSG = '@generateCapacityException=1 is incompatible with any other parameters.';
@@ -264,6 +315,7 @@ DECLARE @_logicalCores TINYINT
264315
EXEC sp_executesql N'RAISERROR (@_errorMSG, 16, 1)', N'@_errorMSG VARCHAR(200)', @_errorMSG;
265316
END
266317

318+
-- Check if valid parameter is selected for procedure
267319
IF (@unrestrictFileGrowth = 1 AND (@addDataFiles=1 OR @restrictDataFileGrowth=1 OR @generateCapacityException=1 OR @help=1 OR @removeCapacityException=1))
268320
BEGIN
269321
SET @_errorMSG = '@unrestrictFileGrowth=1 is incompatible with any other parameters.';
@@ -273,6 +325,7 @@ DECLARE @_logicalCores TINYINT
273325
EXEC sp_executesql N'RAISERROR (@_errorMSG, 16, 1)', N'@_errorMSG VARCHAR(200)', @_errorMSG;
274326
END
275327

328+
-- Check if valid parameter is selected for procedure
276329
IF (@removeCapacityException = 1 AND (@addDataFiles=1 OR @restrictDataFileGrowth=1 OR @generateCapacityException=1 OR @help=1 OR @unrestrictFileGrowth=1))
277330
BEGIN
278331
SET @_errorMSG = '@removeCapacityException=1 is incompatible with any other parameters.';
@@ -282,6 +335,7 @@ DECLARE @_logicalCores TINYINT
282335
EXEC sp_executesql N'RAISERROR (@_errorMSG, 16, 1)', N'@_errorMSG VARCHAR(200)', @_errorMSG;
283336
END
284337

338+
-- Check if valid parameter is selected for procedure
285339
IF ( (@addDataFiles=1 OR @addLogFiles=1) AND (@newVolume IS NULL OR @oldVolume IS NULL))
286340
BEGIN
287341
SET @_errorMSG = '@oldVolume & @newVolume parameters must be specified with '+(CASE WHEN @addDataFiles=1 THEN '@addDataFiles' ELSE '@addLogFiles' END)+' = 1 parameter.';
@@ -291,6 +345,7 @@ DECLARE @_logicalCores TINYINT
291345
EXEC sp_executesql N'RAISERROR (@_errorMSG, 16, 1)', N'@_errorMSG VARCHAR(200)', @_errorMSG;
292346
END
293347

348+
-- Check if valid parameter is selected for procedure
294349
IF ( (@restrictDataFileGrowth=1 OR @restrictLogFileGrowth=1 OR @restrictMountPointGrowth=1) AND (@oldVolume IS NULL))
295350
BEGIN
296351
SET @_errorMSG = '@oldVolume parameters must be specified with '+(CASE WHEN @restrictDataFileGrowth=1 THEN '@restrictDataFileGrowth' WHEN @restrictLogFileGrowth=1 THEN '@restrictLogFileGrowth' ELSE '@restrictMountPointGrowth' END)+' = 1 parameter.';
@@ -1197,6 +1252,8 @@ DECLARE @_logicalCores TINYINT
11971252
END
11981253
FROM @DBFiles AS f
11991254
)
1255+
1256+
12001257
,T_Volumes_Derived AS
12011258
(
12021259
SELECT Volume
@@ -1385,6 +1442,7 @@ DECLARE @_logicalCores TINYINT
13851442
-- Truncate temp table
13861443
TRUNCATE TABLE #stage;
13871444

1445+
13881446
SELECT @_dbName = DBName FROM @Databases WHERE ID = @_loopCounter ;
13891447
SET @_loopSQLText = 'DBCC LOGINFO ('+QUOTENAME(@_dbName)+')
13901448
WITH NO_INFOMSGS;';
@@ -1573,6 +1631,10 @@ DECLARE @_logicalCores TINYINT
15731631
UNION ALL
15741632
--
15751633
SELECT '@getInfo','TINYINT','0','Displays distribution of Data Files across multiple data volumes. It presents file details like database name, its file groups, db status, logical name and autogrowth setting, and volume details like free space and total space.'
1634+
--
1635+
UNION ALL
1636+
--
1637+
SELECT '@volumeInfo','TINYINT','0','Displays Total size, Used Space, Free Space and percentage for all Volumes/disk drives.'
15761638
--
15771639
UNION ALL
15781640
--
@@ -1635,7 +1697,11 @@ DECLARE @_logicalCores TINYINT
16351697
SELECT '@tempDBMountPointPercent','TINYINT','79','Threshold value in percentage for restricting tempdb data files on @oldVolume. This will be used with @expandTempDBSize parameter to re-size the tempdb files if space is added on volume.'
16361698
--
16371699
UNION ALL
1638-
--
1700+
--
1701+
SELECT '@tempDbMaxSizeThresholdInGB','INT','16','Threshold value for total size of all data files of tempdb database.'
1702+
--
1703+
UNION ALL
1704+
--
16391705
SELECT '@DBs2Consider','VARCHAR(1000)',NULL,'Comma (,) separated database names to filter the result set action'
16401706
--
16411707
UNION ALL
@@ -1675,6 +1741,8 @@ DECLARE @_logicalCores TINYINT
16751741
|
16761742
@getLogInfo = { 1 | 0 } [,@DBs2Consider = <comma separated database names>]
16771743
|
1744+
@volumeInfo = { 1 | 0 }
1745+
|
16781746
@help = { 1 | 0 }
16791747
|
16801748
@addDataFiles = { 1 | 0 } ,@newVolume = <drive_name>, @oldVolume = <drive_name> [,@DBs2Consider = <comma separated database names>] [,@forceExecute = 1]
@@ -1695,7 +1763,7 @@ DECLARE @_logicalCores TINYINT
16951763
|
16961764
@restrictMountPointGrowth = { 1 | 0}, @oldVolume = <drive_name> [,@mountPointGrowthRestrictionPercent = <value> ] [,@DBs2Consider = <comma separated database names>] [,@forceExecute = 1]
16971765
|
1698-
@expandTempDBSize = { 1 | 0} [,@tempDBMountPointPercent = <value> ] [,@output4IdealScenario = 1] [,@forceExecute = 1]
1766+
@expandTempDBSize = { 1 | 0} [,@tempDBMountPointPercent = <value> ] [,@tempDbMaxSizeThresholdInGB = <value> ] [,@output4IdealScenario = 1] [,@forceExecute = 1]
16991767
} [;]
17001768
17011769
<drive_name> :: { ''E:\Data\'' | ''E:\Data01'' | ''E:\Data2'' | ... }
@@ -1776,6 +1844,7 @@ DECLARE @_logicalCores TINYINT
17761844
EXEC [dbo].[usp_AnalyzeSpaceCapacity] @expandTempDBSize = 1
17771845
EXEC [dbo].[usp_AnalyzeSpaceCapacity] @expandTempDBSize = 1, @output4IdealScenario = 1
17781846
EXEC [dbo].[usp_AnalyzeSpaceCapacity] @expandTempDBSize = 1, @tempDBMountPointPercent = 89
1847+
EXEC [dbo].[usp_AnalyzeSpaceCapacity] @expandTempDBSize = 1, @tempDbMaxSizeThresholdInGB
17791848
17801849
This generates TSQL code for expanding tempdb data files upto @tempDBMountPointPercent % of total tempdb volume capacity.
17811850
When @output4IdealScenario set to 1, will generate TSQL code to add/remove data files based on the number Logical cores on server upto 8, and delete extra data files created on non-tempdb volumes, and re-size TempdDB data files to occupy 89% of mount point volume.
@@ -3113,7 +3182,7 @@ ALTER DATABASE ['+DbName+'] MODIFY FILE ( NAME = N'''+[FileName]+''', SIZE = '+C
31133182
PRINT '/********* '+ @_loopSQLText+' */';
31143183
END
31153184

3116-
3185+
-- Get TempDb data files
31173186
INSERT @tempDBFiles
31183187
([DBName], [LogicalName], [physical_name], [FileSize_MB], [Volume], [VolumeName], [VolumeSize_MB])
31193188
SELECT DB_NAME(mf.database_id) as DBName, mf.name as LogicalName, mf.physical_name, ((mf.size*8.0)/1024) as FileSize_MB,
@@ -3139,14 +3208,16 @@ ALTER DATABASE ['+DbName+'] MODIFY FILE ( NAME = N'''+[FileName]+''', SIZE = '+C
31393208
IF @verbose = 1
31403209
PRINT ' Logical CPU = '+CAST(@_logicalCores AS VARCHAR(10));
31413210

3211+
-- Get Integer value for [tempdb] data files. For example, value would be 8 from file name [tempdev8].
31423212
SET @_maxFileNO = (SELECT MAX( CAST(RIGHT(REPLACE(REPLACE(LogicalName,']',''),'[',''),PATINDEX('%[a-zA-Z_ ]%',REVERSE(REPLACE(REPLACE(LogicalName,']',''),'[','')))-1) AS BIGINT)) FROM @tempDBFiles);
31433213

31443214
IF @verbose = 1
31453215
PRINT ' @_maxFileNO for Tempdb files = '+CAST(@_maxFileNO AS VARCHAR(10));
31463216

3217+
-- Get count of Valid tempdb data files
31473218
SET @_fileCounts = (SELECT COUNT(*) FROM @tempDBFiles as f WHERE [isToBeDeleted] = 0);
31483219
IF @verbose = 1
3149-
PRINT ' Current TempDB data files (@_fileCounts) = '+CAST(@_fileCounts AS VARCHAR(10));
3220+
PRINT ' Current Valid TempDB data files (@_fileCounts) = '+CAST(@_fileCounts AS VARCHAR(10));
31503221

31513222
IF @_fileCounts <> (CASE WHEN @_logicalCores >= 8 THEN 8 ELSE @_logicalCores END)
31523223
SET @_counts_of_Files_To_Be_Created = (CASE WHEN @_logicalCores >= 8 THEN 8 ELSE @_logicalCores END) - @_fileCounts;
@@ -3161,6 +3232,7 @@ ALTER DATABASE ['+DbName+'] MODIFY FILE ( NAME = N'''+[FileName]+''', SIZE = '+C
31613232
IF @verbose = 1
31623233
PRINT ' Dropping and creating temp table #tempDBFiles';
31633234

3235+
/* This table will contain tempdb files upto 8 */
31643236
IF OBJECT_ID('tempdb..#tempDBFiles') IS NOT NULL
31653237
DROP TABLE #tempDBFiles
31663238
SELECT O.*
@@ -3204,7 +3276,7 @@ ALTER DATABASE ['+DbName+'] MODIFY FILE ( NAME = N'''+[FileName]+''', SIZE = '+C
32043276
CROSS JOIN
32053277
(SELECT TOP 1 * FROM @tempDBFiles WHERE [isToBeDeleted] = 0 ORDER BY LogicalName DESC) AS t
32063278
WHERE FileIterator_Table.FileNo_Add <= @_counts_of_Files_To_Be_Created
3207-
AND @output4IdealScenario = 1
3279+
--AND @output4IdealScenario = 1
32083280
) AS df
32093281
ON 1 = 2
32103282
) AS O;
@@ -3269,7 +3341,7 @@ ALTER DATABASE ['+DbName+'] MODIFY FILE ( NAME = N'''+[FileName]+''', SIZE = '+C
32693341
END -- End Block of Loop
32703342
END
32713343

3272-
-- If no of tempdb files is not as per no of CPUs
3344+
-- If number of tempdb files is less than CPUs
32733345
IF EXISTS (SELECT * FROM #tempDBFiles WHERE isToBeCreated = 1) AND @output4IdealScenario = 1
32743346
BEGIN
32753347
DELETE @T_Files_Final_Add;
@@ -3324,7 +3396,12 @@ USE [master];
33243396
END
33253397

33263398
IF @verbose = 1
3327-
PRINT ' Implementing logic to resize tempdb data files upto 89% of tempdb volume size';
3399+
BEGIN
3400+
IF @tempDbMaxSizeThresholdInGB IS NOT NULL
3401+
PRINT ' Implementing logic to resize tempdb data files upto @tempDbMaxSizeThresholdInGB = '+CAST(@tempDbMaxSizeThresholdInGB AS VARCHAR(5))+' GB. ';
3402+
ELSE
3403+
PRINT ' Implementing logic to resize tempdb data files upto @tempDBMountPointPercent = '+CAST(@tempDBMountPointPercent AS VARCHAR(5))+' %. ';
3404+
END
33283405

33293406
;WITH T_Files_01 AS
33303407
(
@@ -3336,16 +3413,25 @@ USE [master];
33363413
)
33373414
,T_Volume_Details_01 AS
33383415
(
3339-
-- Find tempdb volume detaile
3416+
-- Find tempdb volume details
33403417
SELECT Volume, MAX(VolumeSize_MB) AS VolumeSize_MB, COUNT(*) AS FileCount
33413418
FROM T_Files_01
33423419
GROUP BY Volume
33433420
)
33443421
,T_Volume_Details_02 AS
33453422
(
33463423
SELECT Volume, VolumeSize_MB, FileCount
3347-
-- CapacityThresholdSize_MB = Volume size - space of Tempdb log files if log files is in same as data volume
3348-
,CapacityThresholdSize_MB = ((@tempDBMountPointPercent * VolumeSize_MB)/100.00) - ISNULL((SELECT ((SUM(size) * 8.00)/1024) as size_MB FROM sys.master_files AS mf WHERE mf.database_id = DB_ID('tempdb') AND mf.type_desc = 'LOG' AND mf.physical_name LIKE (v.Volume + '%') ),0)
3424+
/* CapacityThresholdSize_MB = Total Size to be given to Data/Log files of [tempdb] database
3425+
if @tempDbMaxSizeThresholdInGB is set, then use it
3426+
else (@tempDBMountPointPercent - space of Tempdb log files if log files is in same as data volume)
3427+
*/
3428+
,CapacityThresholdSize_MB = CASE WHEN @tempDbMaxSizeThresholdInGB IS NOT NULL
3429+
THEN @tempDbMaxSizeThresholdInGB * 1024
3430+
ELSE ( (@tempDBMountPointPercent * VolumeSize_MB)/100.00) - ISNULL( (SELECT ((SUM(size) * 8.00)/1024) as size_MB
3431+
FROM sys.master_files AS mf
3432+
WHERE mf.database_id = DB_ID('tempdb') AND mf.type_desc = 'LOG'
3433+
AND mf.physical_name LIKE (v.Volume + '%') ),0)
3434+
END
33493435
--,CapacityThresholdSizePerFile_MB = CAST((((@tempDBMountPointPercent) * VolumeSize_MB)/100.0)/FileCount AS NUMERIC(20,2))
33503436
FROM T_Volume_Details_01 AS v
33513437
)

SpaceCapacity-Automation/SpaceCapacity-Automation.ssmssqlproj

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
<AssociatedConnUserName />
5050
<FullPath>Automation - Restrict File growth.sql</FullPath>
5151
</FileNode>
52+
<FileNode Name="v3.5 - Automation - Restrict File growth.sql">
53+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:TUL1CIPCNPDB1:True</AssociatedConnectionMoniker>
54+
<AssociatedConnSrvName>TUL1CIPCNPDB1</AssociatedConnSrvName>
55+
<AssociatedConnUserName />
56+
<FullPath>v3.5 - Automation - Restrict File growth.sql</FullPath>
57+
</FileNode>
5258
</Items>
5359
</LogicalFolder>
5460
<LogicalFolder Name="Miscellaneous" Type="3" Sorted="true">

0 commit comments

Comments
 (0)