One of the simplest yet most impactful performance optimizations for SQL Server is relocating the tempdb system database to a fast, dedicated disk—especially if that disk is temporary or ephemeral. This is a common practice in cloud environments and high-performance database systems where tempdb churn is high and doesn’t require persistence across reboots.
In this post, we’ll walk you through:
Why Move tempdb?
The tempdb database is heavily used by SQL Server for:
Since tempdb is recreated every time SQL Server restarts, its contents don’t need to be persisted. If it resides on a slow or busy disk, this can create a performance bottleneck for the entire instance.
Benefits of Moving tempdb:
When to Use a Temporary or Ephemeral Disk
In many cloud platforms (like Azure, AWS, or GCP), virtual machines come with a temporary disk (often labeled D: or /mnt). These disks offer fast, local storage but do not persist after a reboot. This makes them ideal for tempdb, as long as:
How to Move tempdb to a Temporary Disk
Here’s a safe step-by-step guide for moving tempdb to, for example, D:\SQLData\TempDB.
1. Verify Current tempdb File Locations
USE tempdb; GO EXEC sp_helpfile;
You should see something like this:
Keep this information handy, just in case things go south.
2. Prepare the New Location
Ensure the target directory (e.g., D:\TempDB) exists. Create it manually or via startup scripts if using a temporary drive. Make sure the SQL startup account has read/write access to the new directory.
3. Run ALTER DATABASE Commands to Move Tempdb
USE master; GO ALTER DATABASE tempdb MODIFY FILE (NAME = tempdev, FILENAME = 'D:\TempDB\tempdb.mdf'); GO ALTER DATABASE tempdb MODIFY FILE (NAME = templog, FILENAME = 'D:\TempDB\templog.ldf'); GO ALTER DATABASE tempdb2 MODIFY FILE (NAME = temp2, FILENAME = 'D:\TempDB\temp2.mdf'); GO ALTER DATABASE tempdb3 MODIFY FILE (NAME = temp3, FILENAME = 'D:\TempDB\temp3.mdf'); GO ALTER DATABASE tempdb4 MODIFY FILE (NAME = temp4, FILENAME = 'D:\TempDB\temp4.mdf'); GO ALTER DATABASE tempdb5 MODIFY FILE (NAME = temp5, FILENAME = 'D:\TempDB\temp5.mdf'); GO ALTER DATABASE tempdb6 MODIFY FILE (NAME = temp6, FILENAME = 'D:\TempDB\temp6.mdf'); GO ALTER DATABASE tempdb7 MODIFY FILE (NAME = temp7, FILENAME = 'D:\TempDB\temp7.mdf'); GO ALTER DATABASE tempdb8 MODIFY FILE (NAME = temp8, FILENAME = 'D:\TempDB\temp8.mdf'); GO
If you have multiple data files (common in performance-tuned systems), update them all. This current example has 8 files and a log file.
4. Windows PowerShell Script to Recreate tempdb Folder
Create a PowerShell script that runs at system startup:
# Configuration $SQLService="SQL Server (MSSQLSERVER)" $SQLAgentService="SQL Server Agent (MSSQLSERVER)" $tempdbFolder="D:\TEMPDB" # Make tempdb directory on the SSD if (!(Test-Path -Path $tempdbFolder)) { New-Item -ItemType Directory -Path $tempdbFolder } # Start the services Start-Service $SQLService Start-Service $SQLAgentService
We saved ours as C:\Startup\CreateTempdbFolder.ps1.
5. Create a Task Scheduler Task to Run the Tempdb Folder Powershell Script
Using Task Scheduler
-ExecutionPolicy Bypass -File “C:\Startup\CreateTempDBFolder.ps1”
6. Restart SQL Server
Changes won’t take effect until you restart the SQL Server service.
7. Clean Up Old Files
After restarting and confirming tempdb is using the new location, delete the old files.
Tips and Considerations
Conclusion
Moving tempdb to a temporary or dedicated disk is a low-risk, high-reward optimization for SQL Server performance. In modern deployments—especially cloud-based environments—this can lead to dramatic performance improvements with minimal effort.
Make sure to automate your directory creation and monitor disk space to keep your setup resilient across reboots. Happy moving!
Jeff Horner has been working in IT for 30+ years and has 26 years of SQL Server DBA experience. Jeff has worked in the transportation and distribution industry and is a super experienced DBA despite being the new guy. Jeff is located in Indiana and enjoys cooking and playing keyboard at his church.
Session expired
Please log in again. The login page will open in a new tab. After logging in you can close it and return to this page.