Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
SQL

SQL Server: Fix Tempdb Contention From Too Few Data Files

- 02.12.26 - ErcanOPAK

🗄️ 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.

— Database Engineer

Related posts:

Database stuck in “Restoring” state

Database stuck in “Restoring” state

Covering Indexes Prevent Lookups

Covering Indexes Prevent Lookups

SQL Server: Fix a Computed Column Index That Silently Stops Being Used After a Session Changes ANSI ...

SQL Server: Fix a Computed Column Index That Silently Stops Being Used After a Session Changes ANSI ...

Views: 0
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Windows 11: Fix Phone Link Dropping Notifications Post-Update

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com