🗄️ SQL Server: Fix Tempdb Contention From Too Few Data Files
Tempdb’s allocation pages (PFS, GAM, SGAM) are protected by lightweight in-memory latches rather than full locks, and when a single tempdb data file handles allocation for a server running many concurrent sessions that each create their own temp tables, table variables, or spill a large sort/hash operation to disk, every one of those sessions is competing for latches on the same small set of allocation pages in that one file. SQL Server’s own recommended starting point — one tempdb data file per logical CPU core up to eight, all sized and auto-grown identically — exists specifically to spread that allocation page contention across multiple files so concurrent sessions aren’t all serializing behind the same latch, and a default single-file tempdb left in place on a busy multi-core server is one of the most common, and most fixable, sources of otherwise unexplained CPU-bound waits.
🔎 The Problem
-- Checking current tempdb file configuration: SELECT name, physical_name, size/128 AS size_mb FROM tempdb.sys.database_files WHERE type = 0; -- Returns a single row: tempdev, ...\tempdb.mdf, 8192 MB -- on a server with 16 logical cores -- Checking for the classic symptom - PAGELATCH waits on tempdb allocation pages: SELECT wait_type, waiting_tasks_count, wait_time_ms FROM sys.dm_os_wait_stats WHERE wait_type LIKE 'PAGELATCH%' ORDER BY wait_time_ms DESC; -- PAGELATCH_UP / PAGELATCH_EX waits dominating the wait stats, -- resource_description pointing at tempdb's PFS/GAM pages specifically
✅ Fix: Add Multiple, Equally-Sized Tempdb Data Files
- Add additional tempdb data files until the count matches the number of logical cores, up to a ceiling of eight files for most workloads (going beyond eight rarely helps further and just adds file management overhead) — SQL Server automatically round-robins allocation across all tempdb files using proportional fill, so more files directly means less latch contention per file.
- Size every tempdb data file identically and set identical autogrowth increments on each — SQL Server’s proportional fill algorithm favors whichever file has the most free space, so mismatched file sizes undo the contention benefit by funneling new allocations disproportionately into whichever file happens to be largest.
- Pre-size tempdb files to their expected steady-state size at startup rather than relying on autogrowth events during production traffic, since an autogrow event briefly stalls activity on that file while SQL Server extends it, and zero-initialization of the new space (unless instant file initialization is enabled) can make that pause noticeably worse under load.
🚨 Why This Is Dangerous
- PAGELATCH contention on tempdb allocation pages shows up in monitoring as generic CPU pressure or vague “query is slow” symptoms that don’t obviously point at tempdb configuration at all, so teams often spend time tuning indexes or rewriting queries that were never the actual bottleneck, while the one-time tempdb file configuration that would fix it across the entire workload goes unexamined.
Tempdb’s allocation pages don’t know which query is asking — they just know how many sessions are all trying to latch the same page at once, and a single data file on a sixteen-core server guarantees that number stays high no matter how well-written the queries are.




