· Uwe Janke
Commands / Set-sqmTempDbFileCount

Set-sqmTempDbFileCount

Performance sqmSQLTool v1.9.215+ · Set ✓ -WhatIf supported
Adds missing TempDB data files until the wanted count is reached. It is the counterpart of Test-sqmTempDbFileCount: the test reports that the number of data files does not match the logical cores, this function fixes it.

How many. Without -FileCount the same recommendation applies as in the test: the smaller of the logical cores of the target instance and -MaxFiles (default 8, or the configuration value CheckTempDbMaxFiles).

The new files are created online with ALTER DATABASE tempdb ADD FILE, no restart needed. They get the size of the largest existing data file, because proportional fill distributes unevenly otherwise, the same fixed growth step as the first file (64 MB if that one grows by percent), and they live in the folder of the first data file or in -Path. Names follow the installation: temp2, temp3 with tempdb_mssql_2.ndf, tempdb_mssql_3.ndf. Names and paths already in use are skipped.

Safety. Before the first change the function checks that the target volume has room for all new files. If not, it aborts without changing anything. Removing files is deliberately not supported: a used TempDB file cannot be emptied reliably online (DBCC SHRINKFILE ... EMPTYFILE fails under load, often only a restart helps). If there are already more files than wanted, the result is Skipped.

Parameters

ParameterTypeRequiredDefaultNotes
-SqlInstancestringOptional$env:COMPUTERNAMESQL Server instance.
-SqlCredentialPSCredentialOptional, SQL authentication.
-FileCountint (1-64)Optionalcores, max 8Wanted number of data files. Without it: min(logical cores, -MaxFiles).
-MaxFilesint (1-64)Optional8Upper limit for the automatic recommendation (or configuration CheckTempDbMaxFiles). Has no effect on an explicit -FileCount.
-PathstringOptionalfolder of first fileFolder for the new files, a path on the SQL Server. Must exist and be writable for the service account.
-FileGrowthMBint (1-65536)Optionallike first fileAutogrowth step of the new files in MB. Default: like the first file, 64 MB for percent growth.
-EqualizeSizeswitchSwitch$falseAfter creating, bring all data files to the same size through Set-sqmTempDbFileSize.
-EnableExceptionswitchSwitch$falseThrow instead of logging and returning an error object.
-WhatIf / -ConfirmswitchOptional, Shows the files that would be created together with the T-SQL.

Execution Flow

START Read data files and cores of the target tempdb.sys.database_files (type 0), sys.dm_os_sys_info.cpu_count target count: -FileCount, otherwise min(logical cores, -MaxFiles) Fewer files than wanted? NO Status Skipped nothing to do, or removing not supported YES Plan the new files size of the largest existing file (proportional fill), growth like the first file percent growth or none: 64 MB, or -FileGrowthMB; folder of the first file or -Path names like the installation: temp2, temp3, tempdb_mssql_2.ndf; used names are skipped Pre-check free space sys.dm_os_volume_stats: do all new files fit on the target volume? too little space: abort without any change; no VIEW SERVER STATE: warning, check skipped ALTER DATABASE tempdb ADD FILE online, no restart; one object per file: Created, WhatIf or Failed a failed file stops the loop, files already created stay With -EqualizeSize Set-sqmTempDbFileSize brings the smaller existing files to the same size END

Return value

One object per new file with SqlInstance, LogicalName, PhysicalName, SizeMB, GrowthMB, OldFileCount, NewFileCount, Status and Message. If there is nothing to do, exactly one object with status Skipped.

StatusMeaning
CreatedThe file was added.
WhatIf-WhatIf: the message holds the statement that would run.
SkippedThe instance already has the wanted number of files, or more.
FailedAdding the file failed (the message names the reason). Remaining files are not tried.

Needs ALTER ANY DATABASE (sysadmin). The space check uses sys.dm_os_volume_stats and therefore VIEW SERVER STATE; without it the check is skipped with a warning.

Examples

Which files would be created
Set-sqmTempDbFileCount -SqlInstance "SQL01" -WhatIf
Create the missing files and equalise all sizes
Set-sqmTempDbFileCount -SqlInstance "SQL01" -EqualizeSize
Fixed count and a dedicated folder
Set-sqmTempDbFileCount -SqlInstance "SQL01" -FileCount 8 -Path "T:\TempDB"
Test first, then fix
if ((Test-sqmTempDbFileCount -SqlInstance "SQL01" -NoOpen).Status -ne "OK") {
    Set-sqmTempDbFileCount -SqlInstance "SQL01" -EqualizeSize
}