TempDB: SQL Server's Playground
TempDB is SQL Server’s laboratory. Temporary tables, sorts, worktables, spools, hash operations, and row versions used by snapshot isolation may all rely on it. If the database is the mind, TempDB is its workshop.
An overloaded TempDB slows down the whole system, like a workbench that is too small for the job.
⚙️ What goes into TempDB?
- Temporary tables (
#local,##global). - Table variables (
@table). - Sort and hash work for
ORDER BY,GROUP BY, and joins. - Row versions for snapshot isolation / RCSI when the version store is in TempDB.
- Work performed by
DBCC CHECKDB. - Spools, worktables, and workfiles.
🧠 TempDB architecture
TempDB is shared by the entire SQL Server instance and is recreated when the instance starts.
- Multiple data files and a transaction log file.
- Multiple equally sized data files are often recommended to address allocation contention.
- Use fixed-size autogrowth increments rather than percentages, and pre-size files appropriately.
🔍 Inspect the DMVs
| |
| |
🧪 Quick test: pressure on TempDB
| |
Monitor DMVs and performance counters while the test runs.
⚖️ Good practices
- Consider multiple data files; start with established guidance and adjust based on measured contention rather than applying a rigid CPU-to-file formula.
- Keep data-file sizes and growth increments consistent (for example, 512 MB).
- Use fast SSD/NVMe storage and appropriate initial sizes.
- Monitor
PAGELATCH_%waits intempdb:
| |
📂 Repository: TempDB
“There is no creativity without controlled chaos.” — inspired by Feynman