Demo scripts and slides from my session TempDB Contention: How to Identify and Resolve It.
TempDB is shared by every database on a SQL Server instance, so heavy use of temp tables and table variables can cause page latch contention. This session walks through the two main kinds of contention, how to tell them apart, and how SQL Server 2019 and 2022 change the fix.
- How to detect TempDB contention from live requests and identify the page type involved
- Allocation contention (PFS, GAM, SGAM pages) and why adding equally sized data files helps
- Metadata contention (system object pages) and why adding data files does not help
- Enabling the memory-optimized TempDB metadata feature in SQL Server 2019 and 2022
- How temp table caching works, and how creating indexes inside the table definition keeps temp tables cacheable
- SQL Server 2019 and/or SQL Server 2022 test instances (Developer or Enterprise edition for the memory-optimized metadata feature)
- A test database named
DBATEST - ostress (part of the free RML Utilities) to generate concurrent workload
- Permission to restart the SQL Server service (needed when you toggle the memory-optimized metadata feature)
| Step | Script | What it does |
|---|---|---|
| 0 | 0_RESET_2019.sql, 0_RESET_2022.sql |
Resets TempDB files and turns the metadata feature off |
| 1 | 1_TEMPDB_2019_DEMO.sql |
Checks version, CPUs and TempDB files, and creates a stored procedure that uses a temp table |
| 2 | 2_CHECK_CONTENTION.sql |
Shows current page latch waits and classifies them as allocation or metadata contention |
| 3 | 3_ALLOCATION CONTENTION_2019.SQL |
Adds TempDB data files to relieve allocation contention |
| 4 | 4_METADATA CONTENTION_2019.SQL |
Enables memory-optimized TempDB metadata on SQL Server 2019 |
| 5 | 5_TEMPDB_2022_DEMO.sql |
Repeats the workload on SQL Server 2022 |
| 6 | 6_CHECK_CONTENTION.sql |
Checks contention again, with sample ostress commands for load |
| 7 | 7_METADATA_CONTENTION_2022.sql |
Enables memory-optimized TempDB metadata on SQL Server 2022 |
| 8 | 8_CACHE_NONCACHE.sql |
Compares cacheable and non-cacheable temp table patterns |
The isolation level file has helper queries that show the isolation level of each session and database.
*.sql: demo scripts, run in numbered orderTEMPDB CONTENTION.pptx: session slides
Some scripts clear caches, change server configuration, and require a service restart. Run them only on a test instance.