🗄️ TempDB at 100% = Whole Server at Risk
TempDB filling its drive doesn’t just fail the one query causing it — every other database on that SQL Server instance shares the same TempDB, so a single runaway sort, hash join, or long-open transaction can bring your entire server to a halt.
🔍 Find What’s Actually Consuming TempDB Right Now
-- Which sessions are using the most TempDB space right now
SELECT
s.session_id,
r.status,
(t.internal_objects_alloc_page_count + t.user_objects_alloc_page_count) * 8 / 1024 AS mb_used,
r.command,
t2.text AS running_query
FROM sys.dm_db_task_space_usage t
JOIN sys.dm_exec_sessions s ON s.session_id = t.session_id
JOIN sys.dm_exec_requests r ON r.session_id = t.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t2
ORDER BY mb_used DESC;
🐞 The Usual Culprits
1. A query doing a huge sort or hash join without a helpful index - the intermediate results spill to TempDB. 2. A long-running or forgotten-open transaction under snapshot/ RCSI isolation - SQL Server keeps a version store in TempDB for as long as that old transaction stays open, even if it's not touching the affected rows anymore. 3. Excessive use of table variables or temp tables (#temp) with very large row counts inside a loop.
✅ Immediate Relief
- Identify the worst offending session from the query above, and if it’s safe to do so, KILL it — check with the team first if this is shared production.
- Check for old open transactions specifically: SELECT * FROM sys.dm_tran_active_snapshot_database_transactions ORDER BY elapsed_time_seconds DESC.
🛑 Kill the Offending Session
KILL 62; -- replace 62 with the actual session_id from the query above -- This rolls back that session's transaction, which may take a -- while itself if it had done a lot of work - be patient, don't -- kill -9 the whole SQL Server process instead.
🛡️ Prevent the Next One
- Size TempDB proportionally to your workload up front, and split it across multiple equally-sized data files (one per CPU core, up to about 8) to reduce contention.
- Set TempDB’s autogrowth to a fixed MB amount, not a percentage — percentage growth gets slower and slower as the file grows.
- Add monitoring/alerting on TempDB free space specifically, separate from your general disk-space alerts, since it fills far faster than a normal data file.
TempDB is shared infrastructure for the whole instance — a runaway query in one database can starve every other database on the box, which is exactly why it deserves its own dedicated monitoring.
