3.how To Shrink TempDB Without SQL Server Restart
3.how To Shrink TempDB Without SQL Server Restart
com
Contact: +91 9966246368 (WhatsApp) YouTube Channel: https://bit.ly/35QYIiG
1) Usually, tempdb fills up when you are low on disk space, or when you have set an
unreasonably low maximum size for database growth.
Many people think that tempdb is only used for #temp tables. When in fact, you can easily fill
up tempdb without ever creating a single temp table. Some other scenarios that can cause
tempdb to fill up:
1) any sorting that requires more memory than has been allocated to SQL Server will be forced
to do its work in tempdb;
2) if the sorting requires more space than you have allocated to tempdb, one of the above
errors will occur;
3) DBCC CheckDB('any database') will perform its work in tempdb -- on larger databases, this
can consume quite a bit of space;
4) DBCC DBREINDEX or similar DBCC commands with 'Sort in tempdb' option set will also
potentially fill up tempdb;
5) large result sets involving unions, order by / group by, cartesian joins, outer joins, cursors,
temp tables, table variables, and hashing can often require help from tempdb;
6) any transactions left uncommitted and not rolled back can leave objects orphaned in
tempdb;
USE tempdb
GO
EXEC sp_spaceused
Rajasekhar Reddy Bolla Email Id: [email protected]
Contact: +91 9966246368 (WhatsApp) YouTube Channel: https://bit.ly/35QYIiG
The following should give you some clues as to which table(s) consume most of the space in the
data file(s) --this will help you narrow down any transactions that are either taking a long time
or repeatedly being left in limbo:
USE tempdb
GO
USE [TEMPDB];
GO
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
DBCC FREEPROCCACHE;
GO
DBCC FREESYSTEMCACHE ('ALL');
GO
DBCC FREESESSIONCACHE;
GO
DBCC SHRINKFILE (TEMPDEV, 1024) –use script mention at the last
GO
DBCC DROPCLEANBUFFERS
Clears the clean buffers. This will flush cached indexes and data pages. You may want to run a
CHECKPOINT command first, to flush everything to disk.
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO
DBCC FREEPROCCACHE
Rajasekhar Reddy Bolla Email Id: [email protected]
Contact: +91 9966246368 (WhatsApp) YouTube Channel: https://bit.ly/35QYIiG
Clears the procedure cache, which may free up some space in tempdb, although at the expense
of your cached execution plans, which will need to be rebuilt the next time. This means that ad-
hoc queries and stored procedures will have to recompile the next time you run them.
Although this happens automatically, you may notice a significant performance decrease the
first few times you run your procedures.
DBCC FREEPROCCACHE;
GO
DBCC FREESYSTEMCACHE
Flushes the distributed query connection cache. This has to do with distributed queries (queries
between servers), but I’m really not sure how much space they actually take up in tempdb.
DBCC FREESESSIONCACHE;
GO
How to shrink?
DBCC SHRINKFILE is the same tool used to shrink any database file, in tempdb or other
databases. This is the step that actually frees the unallocated space from the database file.
Don’t set the new size too low! Make a realistic estimate of the largest “normal” size that
tempdb will assume during normal day-to-day operation.
That’s it. If everything works the way it should, you should now be able to verify the new size of
tempdb.
Warning: Make sure you don’t have any open transactions when running DBCC SHRINKFILE.
Open transactions may cause the DBCC operation to fail, and possibly corrupt your tempdb!