-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtsql-set-tempdb-max-size.sql
More file actions
63 lines (49 loc) · 1.92 KB
/
tsql-set-tempdb-max-size.sql
File metadata and controls
63 lines (49 loc) · 1.92 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
set nocount on
go
DECLARE @DriveSizeGB INT = 149 -- if windows says 149 GB, use 149 GB here - don't round up to 150 GB
,@FileCount INT = 9 -- 9 is best (8 data + 1 log)
,@DrivePath VARCHAR(100) = 't:\sqltemp' -- set this in case we need to add tempdb files
-- used by script
declare @RowID INT
,@FileSize VARCHAR(10)
,@DriveSizeMB int
,@InitialFileSize VARCHAR(10);
/* Converts GB to MB */
set @DriveSizeMB = @DriveSizeGB * 1024;
--print @DriveSizeMB
set @DriveSizeMB = @DriveSizeMB - (@DriveSizeMB * 0.15) -- alarm trips at 10%
/* Splits size by the files */
set @FileSize = @DriveSizeMB / @FileCount;
set @InitialFileSize = @FileSize / @FileCount;
if right(@DrivePath, '1') <> '\'
set @DrivePath = @DrivePath + '\'
/* Table to house requisite SQL statements that will modify the files to the standardized name, and size */
DECLARE @Command TABLE
(
RowID INT IDENTITY(1, 1)
,Command NVARCHAR(MAX)
);
INSERT INTO @Command (Command)
SELECT 'ALTER DATABASE tempdb MODIFY FILE (NAME = [' + f.name + ']'
+ ', MAXSIZE = ' + @FileSize + ' MB);'
FROM sys.master_files AS f
WHERE f.database_id = DB_ID(N'tempdb');
select @RowID = count(*) from sys.master_files WHERE database_id = DB_ID(N'tempdb');
/* If there are less files than indicated in @FileCount, add missing lines as ADD FILE commands */
WHILE @RowID < @FileCount
BEGIN
INSERT INTO @Command (Command)
SELECT 'ALTER DATABASE tempdb ADD FILE (NAME = [temp' + CAST(@RowID AS VARCHAR) + '],' + ' FILENAME = ''' + @DrivePath + 'temp'+ CAST(@RowID AS VARCHAR)+'.mdf''' + ', SIZE = ' + @InitialFileSize + ' MB, MAXSIZE = ' + @FileSize + ' MB);'
SET @RowID = @RowID + 1
END
/* Execute each line to process */
WHILE @RowID > 0
BEGIN
DECLARE @WorkingSQL NVARCHAR(MAX)
SELECT @WorkingSQL = Command
FROM @Command
WHERE RowID = (@FileCount - @Rowid) + 1
print @WorkingSQL
--EXEC (@WorkingSQL)
SET @RowID = @RowID - 1
END