Home » SQL Server » TempDB Performance Tuning in SQL Server 2022 vs 2016 vs 2019

TempDB Performance Tuning in SQL Server 2022 vs 2016 vs 2019

📘 What Is TempDB and Why It Matters

TempDB is a shared system database in SQL Server used for:

  • Temporary tables and table variables
  • Sorting and hashing operations
  • Version stores for snapshot isolation
  • Internal operations like index rebuilds and DBCC checks

Because it’s used by every session and process, TempDB is often a performance bottleneck—especially in high-concurrency environments.


🧭 Evolution of TempDB: SQL Server 2016 vs 2019 vs 2022

FeatureSQL Server 2016SQL Server 2019SQL Server 2022
Multiple TempDB Files by Default✅ (via setup)
Trace Flag 1117/1118 BehaviorManualDefaultDefault
Memory-Optimized Metadata
Latch-Free Allocation (GAM/SGAM)
Enhanced Temp Table CachingBasicImprovedOptimized
Persistent Version Store
TempDB Space Governance❌ (Coming in 2025)

🚀 What’s New in SQL Server 2022 TempDB?

1. Memory-Optimized TempDB Metadata

Problem: High concurrency workloads cause contention on system tables like sysobjects and syscolumns.

Solution: SQL Server 2022 introduces in-memory metadata for TempDB, reducing latch contention and improving throughput.

How to Enable:

✅ Use Case: Ideal for OLTP systems with frequent temp table creation and destruction.


2. Latch-Free Allocation for GAM/SGAM Pages

Problem: Allocation pages (GAM/SGAM) are hotspots under parallel workloads.

Solution: SQL Server 2022 introduces shared latches for these pages, reducing contention.

📈 Performance Boost: Up to 40% improvement in temp table-heavy workloads.


3. Enhanced Temp Table Caching

SQL Server 2022 improves temp table reuse across sessions, reducing overhead from frequent creation/destruction.

Best Practice:

  • Avoid using sp_executesql with temp tables to benefit from caching.

🧪 Real-World Examples

Example 1: Simulating TempDB Contention

CREATE OR ALTER PROCEDURE dbo.SimulateTempDBContention AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @i INT = 0;
    WHILE @i < 1000
    BEGIN
        CREATE TABLE #Temp (ID INT);
        DROP TABLE #Temp;
        SET @i += 1;
    END
END;
GO

Run this procedure in multiple sessions to simulate contention. On SQL Server 2022, you’ll notice significantly reduced waits compared to 2016 or 2019.

Example 2: Monitoring TempDB Waits

SELECT session_id, wait_type, wait_time_ms, wait_resource
FROM sys.dm_os_waiting_tasks
WHERE wait_type LIKE 'PAGE%';

Use this to identify latch contention on allocation pages.

Example 3: Checking TempDB File Usage

SELECT file_id, name, size * 8 / 1024 AS SizeMB
FROM tempdb.sys.database_files;

Ensure all files are equal in size and growth settings.


🛠️ Design Best Practices for TempDB in SQL Server 2022

✅ File Configuration

  • Use 1 TempDB file per logical CPU (up to 8, then scale in multiples of 4).
  • Ensure equal size and growth for all files.
  • Place TempDB on fast SSD/NVMe storage.

✅ Enable Memory-Optimized Metadata

ALTER SERVER CONFIGURATION 
SET MEMORY_OPTIMIZED TEMPDB_METADATA = ON;

✅ Avoid Common Pitfalls

  • Don’t use % for autogrowth—use fixed MB.
  • Avoid sp_executesql with temp tables to enable caching.
  • Don’t place TempDB on the same drive as user databases.

🔮 What’s Coming in SQL Server 2025?

  • TempDB Space Governance: Limit TempDB usage per workload group.
  • Persistent Version Store (PVS): For Accelerated Database Recovery (ADR).
  • Improved Monitoring: New DMVs for TempDB usage and contention.

📌 Final Thoughts

SQL Server 2022 brings game-changing improvements to TempDB, especially for high-concurrency environments. By enabling memory-optimized metadata and leveraging latch-free allocation, you can dramatically reduce contention and boost performance.

If you’re still on SQL Server 2016 or 2019, upgrading to 2022 can offer immediate performance gains—especially for workloads that rely heavily on TempDB.


#SQLServer, #TempDB, #SQLServer2022, #TempDBPerformance, #SQLPerformanceTuning, #TempDBOptimization, #SQLServerBestPractices, #TempDBDesign, #SQLServerInternals, #SQLServerTips, #SQLServerMonitoring, #SQLServerAdmin, #SQLServerScripts, #SQLServerDMVs, #SQLServerContendedPages, #SQLServerMetadata, #SQLServerCaching, #SQLServerMemory, #SQLServerStorage, #SQLServerArchitecture, #SQLServerSetup, #SQLServerConfiguration, #SQLServerParallelism, #SQLServerETL, #SQLServerOLTP, #SQLServerReporting, #SQLServerAnalytics, #SQLServerBatchJobs, #SQLServerSnapshotIsolation, #SQLServerVersionStore, #SQLServerGovernance, #SQLServerHealthCheck, #SQLServerFileGrowth, #SQLServerFileSizing, #SQLServerGAMSGAM, #SQLServerWaitStats, #SQLServer2025Preview, #SQLServerEvolution, #SQLServerComparison, #SQLServer2016, #SQLServer2019, #SQLServerUpgrade, #SQLServerPerformanceBoost, #SQLServerContentionFix, #SQLServerMonitoringTools, #SQLServerSetupChecklist, #SQLServerStorageOptimization, #SQLServerTempTables, #SQLServerTableVariables, #SQLServerInMemoryMetadata, #SQLServerResourceGovernor


1 Comment

  1. Simon says:

    memory optimised tempdb metadata is a feature in SQL 2019, and we’re using it already
    https://www.mssqltips.com/sqlservertip/6230/memoryoptimized-tempdb-metadata-in-sql-server-2019/

Leave a Reply

Join us on Facebook

microsoftcommunitycontributor