How to shrink temp DB files

Issue :- 

Tempdb full is very poor situation , when tempdb database is full, application is not accessible , user may not able to connect. To reduce tempdb size there is one typical solution for this - Restart of SQL Services. But this solution we can oftenly use in our critical production server as it requires downtime. Another solution is to add logfile in other drive but it requires sufficient space on drive. So below is the best possible solution which I try to use as first option.

Solution :- 

Step 1 -

Use TEMPDB
Go
CHECKPOINT;
GO
Description :- Check point is the process that writes all dirty pages from buffer cache to Physical disk. Also writes log records from log buffer to physical disk 

Step 2 -

DBCC DROPCLEANBUFFERS;
GO
Description :- Removes all clean buffers from the buffer pool, and columnstore  objects from the columnstore object pool.


Step 3-

DBCC FREEPROCCACHE;
GO
Description :- Removes all elements from the plan cache or removes all cache entries  associated with a specified resource pool.


Step 3-

DBCC FREESYSTEMCACHE ('ALL');
GO
Description :- Releases all unused cache entries from all caches. The SQL server engine proactively cleans up unused cache entries in the background to make  memory available for current entries. However, you can use this command to manually remove unused entries from all caches


Step 4-

DBCC FREESESSIONCACHE;
GO
Description :- Flushes the distributed query connection cache used by distributed queries against an instance of Microsoft SQL Server..


step 5  -

use tempdb
DBCC SHRINKFILE('N'tempdev', 2048);--- 2048 is size in MB
GO
Description :- To Reduce size of log file


Note :- Do it twice or trice if not releasing space . Even though if it is not releasing space then need to look into open transaction which query generating high temp usage
Use Below to check 
DBCC opentran                 ----- To check open Transaction
DBCC inputbuffer(SPID)         ----- To check query






No comments:

Post a Comment