When I was working DML operation on the database, It is noted that the tempdb has grown significantly larger and I didn’t have much space on the drive of that server. I tried shrinking the mdf file of tempdb. It executes successfully but no space is released to OS. Is restarting the SQL Server is the only way to release the space. what are other steps available to troubleshoot this issue? Is there any way I can do it without restarting the SQL service?.
Let’s explore the options.
Here are quick steps to analyze the tempdb resource contention issue.
DBCC SHRINKFILE (‘tempdev’, 1024)
The query executed successfully but the size of the database did not change.
SELECT * FROM sys.dm_exec_requests WHERE database_id = 2
No open transactions! Alright, any process holding locks on tempdb?
select * from sys.dm_tran_locks where resource_database_id= 2
No locks! There was a mention of sys.dm_db_session_space_usage DMV which helps to track the number of page allocation and deallocation by each session on the instance. Hence tried my luck with this query.
select * from sys.dm_db_session_space_usage where user_objects_alloc_page_count<> 0
Any session would use tempdb for creating some temporary objects. Since the database was not shrinking, obviously some user-defined tables would be there
SELECT * FROM tempdb..sys.all_objectswhere is_ms_shipped = 0
Check for user tables on tempdb
After performing all the above steps, Space is big constraint than execute the below command to free up the cache.
Note: It is not a recommended step.
This should free up tempdb
7)DBCC SHRINKFILE (‘tempdev’, 1024)
If this doesn’t help then the last step would restart the sql server.