Create tempdb Files
Scenarios
The tempdb system database is a global resource that is available to all users connected to the instance of SQL Server or connected to SQL Database. It is a temporary database that cannot store data permanently. It is used to process intermediate data for various requests in the instance. Physical properties of tempdb in SQL Server are classified into the primary data files (.mdf), secondary data files (.ndf), and log files (.ldf). tempdb is re-created every time SQL Server is started.
There may be some issues or even service interruption if applications frequently create and drop tempdb files, especially in high-concurrency scenarios.
Microsoft recommends that the tempdb files be divided into multiple files. Generally, the number of files depends on the number of vCPUs (logical). If the number of vCPUs is greater than eight, use eight data files and then if contention continues, increase the number of data files by multiples of 4 until the contention is reduced to acceptable levels or make changes to the workload/code.
For more information, see tempdb Database in the Microsoft official website.
Constraints
- By default, each DB instance using SQL Server 2008, 2012, or 2014 Edition has one tempdb file, each instance using SQL Server 2016 Edition has four tempdb files, and each instance using SQL Server 2017 Edition has eight tempdb files.
- Each DB instance has only one log file no matter which SQL Server Edition they use.
Application Scenario
You need to determine the number of tempdb files to be created based on the instance specifications and scenarios. The following uses an example to show how to create 8 tempdb files for a SQL Server 2014 Enterprise Edition instance with 32 vCPUs.
Prerequisites
- SQL Server Management Studio has been installed. The detailed procedure is provided in How Can I Install SQL Server Management Studio?
- A SQL Server 2014 Enterprise Edition instance with 32 vCPUs has been created. The detailed procedure is provided in Buying an RDS Microsoft SQL Server DB Instance.
Procedure
- Start SQL Server Management Studio.
- Choose Connect > Database Engine. In the displayed dialog box, enter login information. Figure 1 Connecting to the server
Table 1 Parameter description Parameter
Description
Server name
Indicates the IP address and port of the DB instance. Use a comma (,) to separate them. For example: x.x.x.x,8080.- The IP address is the EIP that has been bound to the DB instance.
- The port is the database port in the Connection Information area on the Basic Information page of the DB instance on the RDS console.
Authentication
Indicates the authentication mode. Select SQL Server Authentication.
Login
Indicates the RDS database username. The default administrator is rdsuser.
Password
Indicates the password of the RDS database username.
- View the tempdb information.
- Choose Databases > System Databases> tempdb. Right-click tempdb and choose Database Properties. In the displayed dialog box, view the current tempdb information. Figure 2 Viewing current tempdb information
- You can also run the following SQL statements to query the tempdb information:
size*1.0/128 AS FileSizeInMB,
CASE max_size
WHEN 0 THEN 'Autogrowth is off.'
WHEN -1 THEN 'Autogrowth is on.'
ELSE 'Log file grows to a maximum size of 2 TB.'
END,
growth AS 'GrowthValue',
'GrowthIncrement' =
CASE
WHEN growth = 0 THEN 'Size is fixed.'
WHEN growth > 0 AND is_percent_growth = 0
THEN 'Growth value is in 8-KB pages.'
ELSE 'Growth value is a percentage.'
END
FROM tempdb.sys.database_files;
GO
- Choose Databases > System Databases> tempdb. Right-click tempdb and choose Database Properties. In the displayed dialog box, view the current tempdb information.
- Run the following statements to query the tempdb file name of the current DB instance:
SELECT name, physical_name
FROM sys.master_files
WHERE database_id = DB_ID('tempdb');
Figure 3 Viewing tempdb file names
- On the Files tab in 3, view the paths of tempdb files. Figure 4 Viewing tempdb paths
- Run the following statements to migrate the tempdb files to D:\RDSDBDATA\DATA and specify the initial size and growth as required.
USE master;
GO
ALTER DATABASE [tempdb] MODIFY FILE (NAME = tempdev, FILENAME = 'D:\RDSDBDATA\DATA\tempdb.mdf', SIZE = 8MB, FILEGROWTH = 64MB);
GO
ALTER DATABASE [tempdb] MODIFY FILE (NAME = templog, FILENAME = 'D:\RDSDBDATA\DATA\templog.ldf', SIZE = 8MB, FILEGROWTH = 64MB);
GO
Figure 5 Moving tempdb files
- On the Instance Management page of the RDS console, locate the target DB instance and choose in the Operation column to reboot the DB instance.
You can also click the target DB instance. On the displayed page, click Reboot in the upper right corner of the page.
- Run the following SQL statements to check whether the tempdb files are successfully migrated:
SELECT name, physical_name
FROM sys.master_files
WHERE database_id = DB_ID('tempdb');
Figure 6 Viewing the migration results
- Configure the file name, initial size, and growth as required. Add tempdb files using either of the following methods:
- Adding tempdb files through SQL statements
Based on the number of vCPUs and tempdb files to be added, set the initial size to 8 MB and file growth to 64 MB.
USE [master]
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'temp2', FILENAME = N'D:\RDSDBDATA\DATA\tempdb2.ndf', SIZE = 8MB, FILEGROWTH = 64MB)
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'temp3', FILENAME = N'D:\RDSDBDATA\DATA\tempdb3.ndf', SIZE = 8MB, FILEGROWTH = 64MB)
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'temp4', FILENAME = N'D:\RDSDBDATA\DATA\tempdb4.ndf', SIZE = 8MB, FILEGROWTH = 64MB)
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'temp5', FILENAME = N'D:\RDSDBDATA\DATA\tempdb5.ndf', SIZE = 8MB, FILEGROWTH = 64MB)
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'temp6', FILENAME = N'D:\RDSDBDATA\DATA\tempdb6.ndf', SIZE = 8MB, FILEGROWTH = 64MB)
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'temp7', FILENAME = N'D:\RDSDBDATA\DATA\tempdb7.ndf', SIZE = 8MB, FILEGROWTH = 64MB)
GO
ALTER DATABASE [tempdb] ADD FILE ( NAME = N'temp8', FILENAME = N'D:\RDSDBDATA\DATA\tempdb8.ndf', SIZE = 8MB, FILEGROWTH = 64MB)
GO
- Adding tempdb files through SQL Server Management Studio On the Files tab in 3, click Add. Figure 7 Adding tempdb files through the client
- Adding tempdb files through SQL statements
- After the configuration is complete, reboot the DB instance again by referring to 7.
- Repeat 8 to check whether the tempdb files are successfully added. Figure 8 Viewing the added tempdb files
Last Article: Migrating Microsoft SQL Server Data Using DRS
Next Article: Installing a C# CLR Assembly in RDS for SQL Server
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.