SQL Server TempDB Best Practices: Configuration and Troubleshooting
Every SQL Server instance has one tempdb, and every database on that instance shares it. When tempdb is slow or full, everything slows down or fails at once. A few configuration choices and monitoring habits prevent most tempdb problems.

What uses tempdb
- Your objects:
#temptables and table variables. - Internal work: sorts, hash joins and aggregations that spill out of memory, spools, and some index operations such as
SORT_IN_TEMPDB. - The version store: row versions for read committed snapshot isolation (RCSI), snapshot isolation, online index rebuilds and triggers. (With Accelerated Database Recovery, versions are kept in the user database instead.)
tempdb is recreated empty every time SQL Server restarts.
Use multiple, equally sized data files
With a single data file, many sessions creating temp objects at once compete for the same allocation pages, which shows up as PAGELATCH_* waits on tempdb pages. Multiple data files spread that work out. The widely used starting point is:
- One data file per logical CPU core, up to 8 files.
- If contention remains with 8, add more in groups of 4.
- Keep all data files the same size with the same growth setting, so SQL Server spreads allocations evenly across them.
Since SQL Server 2016, setup suggests multiple files automatically, and the old trace flags 1117 and 1118 are no longer needed for tempdb because that behaviour is now the default.
Pre-size it and use fixed growth
Because tempdb is recreated at startup, an undersized tempdb grows again and again after every restart, and growth events pause activity. Size it for your normal peak usage up front, and set autogrowth in fixed megabytes rather than percentages:
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, SIZE = 8192MB, FILEGROWTH = 512MB);
-- repeat for each data file (temp2, temp3, ...), keeping sizes identicalInstant file initialization (granting the SQL Server service account the "Perform volume maintenance tasks" right) makes data file growth much faster. It does not apply to log files.
Put it on fast storage
tempdb is write-heavy and does not need to survive a restart, so it is ideal for the fastest local SSD or NVMe storage you have. On Azure virtual machines, many teams place tempdb on the local temporary disk for low latency, taking care that the folder exists at startup.
Spot allocation contention
SELECT session_id, wait_type, wait_duration_ms, resource_description
FROM sys.dm_os_waiting_tasks
WHERE wait_type LIKE 'PAGELATCH%'
AND resource_description LIKE '2:%'; -- database_id 2 = tempdbFrequent results here, especially on pages like 2:1:1, 2:1:2 or 2:1:3, point to allocation contention: add data files if you have fewer than eight.
Memory-optimized tempdb metadata
From SQL Server 2019, contention on tempdb's system tables in very busy systems can be reduced by moving tempdb metadata into memory-optimized tables. It needs a restart and has some limitations, so enable it when you have confirmed metadata contention:
ALTER SERVER CONFIGURATION SET MEMORY_OPTIMIZED TEMPDB_METADATA = ON;Find out who is using the space
-- Overall breakdown
SELECT
SUM(user_object_reserved_page_count) * 8 / 1024 AS user_objects_mb,
SUM(internal_object_reserved_page_count) * 8 / 1024 AS internal_objects_mb,
SUM(version_store_reserved_page_count) * 8 / 1024 AS version_store_mb,
SUM(unallocated_extent_page_count) * 8 / 1024 AS free_mb
FROM tempdb.sys.dm_db_file_space_usage;
-- Sessions using the most
SELECT TOP (10) session_id,
(user_objects_alloc_page_count + internal_objects_alloc_page_count) * 8 / 1024 AS allocated_mb
FROM sys.dm_db_session_space_usage
ORDER BY allocated_mb DESC;Watch the version store
If you use RCSI or snapshot isolation, a single long-running transaction keeps old row versions alive, and the version store can grow until tempdb fills. When version_store_mb keeps rising, look for the oldest open transaction (for example in sys.dm_tran_active_snapshot_database_transactions) and deal with it.
Azure SQL Database
In Azure SQL Database, tempdb is managed for you; its size depends on the service tier. You still benefit from reducing spills and avoiding huge temp tables, but file layout is not something you configure.
Quick checklist
- Up to 8 equally sized data files to start.
- Pre-size for peak use; fixed-size autogrowth.
- Fast local storage.
- Monitor
PAGELATCHwaits on database 2. - Track space by type and by session, and watch the version store.
Related reading:
Comments
Post a Comment