• Skip to main content
  • Skip to primary sidebar

DBA Diaries

Thoughts and experiences of a DBA working with SQL Server and MySQL

How to shrink tempdb

December 27, 2011 by Andy Hayes 15 Comments

There may come a time when you might want to shrink tempdb because it has become too large.

There are a few ways you can do this and I have listed them below but please read to the end of the post before making a decision on which way you want to approach this. There is an important note at the end of the post.

So first, we’ll look at the configuration on my server.

[sourcecode language=’sql’]SELECT name, size
FROM sys.master_files
WHERE database_id = DB_ID(N’tempdb’);[/sourcecode]

name                 size
-------------------- -----------
tempdev              1280
templog              640

(2 row(s) affected)

Note that the size column is listing the size of the file in 8Kb pages. In this instance my “tempdev” file is 10Mb (( 1280 * 8 ) = 10240 kb)

How to shrink tempdb using DBCC SHRINKFILE

The syntax is as follows and the operation does not require a restart of the sql server service.

DBCC SHRINKFILE(logical_filename, size_in_MB)

So I will shrink the file to 5Mb

Here is the T-SQL:

[sourcecode language=’sql’]DBCC SHRINKFILE(tempdev, 5);[/sourcecode]
Which produces the following output and I can see that CurrentSize is now smaller than previously

DbId   FileId      CurrentSize MinimumSize UsedPages   EstimatedPages
------ ----------- ----------- ----------- ----------- --------------
2      1           640         288         176         176

(1 row(s) affected)

DBCC execution completed. If DBCC printed error messages, contact your system administrator.

name                 size
-------------------- -----------
tempdev              640
templog              640

(2 row(s) affected)

Don’t try and increase filesizes in tempdb using this command because you will see an error. In this example, the attempt was to increase to 50Mb.

Cannot shrink file ‘1’ in database ‘tempdb’ to 6400 pages as it only contains 640 pages.

How to shrink tempdb using DBCC SHRINKDATABASE

The syntax is as follows and does not require a restart of the SQL Server service:

DBCC SHRINKDATABASE(tempdb, ‘target_percentage_of_free_space’);

So if the data files in tempdb had enough free space, you could shrink tempdb by running this command to leave 10Mb of free space at the end of the files:
[sourcecode language=’sql’]DBCC SHRINKDATABASE(tempdb, 10);[/sourcecode]

How to shrink tempdb using ALTER DATABASE

As in my post about moving tempdb, you can use the ALTER DATABASE command to perform a tempdb resize.

The following script will resize both the log and data file to be 100Mb.
[sourcecode language=’sql’]USE master;
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, SIZE=100Mb);
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, SIZE=100Mb);
GO[/sourcecode]

This operation requires a SQL Server service restart once the tempdb configuration has been set by ALTER DATABASE.

How to shrink tempdb using Management Studio

You can also use Management Studio to resize Tempdb and perform shrink operations by right mouse clicking the tempdb database and choosing Tasks->Shrink.

To resize tempdb, you can set the file sizes by right mouse clicking the tempdb and choosing Properties->Files and setting the sizes there.

Note that with both the ALTER DATABASE and management studio methods, you cannot resize a tempdb file to a particular size if the data contained in the file exceed the size that you are trying to size to.

Should you shrink TempDB?

It is documented in this Microsoft article that it can cause consistency errors in the TempDB database if you perform a shrink operation while the database is in use so please read this carefully and consider whether you can shrink the database by other means, i.e restarting the SQL Server instance which will create a brand new copy of TempDB releasing the disk space.

Tempdb won’t shrink?

I sometimes get asked about why tempdb won’t shrink when using one of the methods which does not involve a SQL Server service restart. Microsoft recommends that if possible, shrinking of tempdb should be done either in single user mode or when there is no current tempdb activity. I have extracted the quote below from the article that is linked to in the previous section.

It is safe to run shrink in tempdb while tempdb activity is ongoing. However, you may encounter other errors such as blocking, deadlocks, and so on that can prevent shrink from completing. Therefore, in order to make sure that a shrink of tempdb will succeed, we recommend that you do this while the server is in single-user mode or when you have stopped all tempdb activity.

Related Posts:

  • sql grouping sets
    Using SQL GROUPING SETS for Multiple GROUP BY…
  • How to fix "conversion failed when converting date and/or time from character string"
    How to fix "conversion failed when converting date…

Filed Under: All Articles, SQL Server Administration Tagged With: sql server, tempdb

About Andy Hayes

Andy Hayes is a DBA working with SQL Server since version 7.0. He has a wonderful wife and two beautiful children. He loves database technology, playing cricket, and blogging. He is passionate about sharing his experiences as a DBA and learning more to further his understanding and knowledge. You can follow me on Twitter, check out my Facebook page or follow me on Google+

Reader Interactions

Comments

  1. Gary says

    April 5, 2013 at 9:33 pm

    This was very clear and informative. Thanks!

    Reply
    • admin says

      April 6, 2013 at 10:18 am

      Hi Gary

      Glad you found the article useful. Thanks for your comment.

      All the best

      Andy

      Reply
  2. Umer Tahir says

    June 26, 2013 at 10:13 am

    Perfect…very nicely put through which helped me getting the information out in no time.

    Reply
  3. Junice says

    June 24, 2014 at 7:14 am

    Thanks for your suggestion!

    You saved me from the fires of hell! LOL

    Reply
    • Andy Hayes says

      June 27, 2014 at 5:17 pm

      Great! I’m glad it helped 🙂

      Reply
  4. radiy says

    August 26, 2014 at 9:10 am

    Very, informative article.

    Reply
  5. Velu says

    November 27, 2014 at 3:52 pm

    Very Nice

    Reply
  6. Fahad Malik says

    February 23, 2015 at 5:44 am

    Dear All
    This article is not working for me, my tmpdb is taking 60{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de} of memory. please help if you can share anything.

    Reply
  7. John Bala says

    August 23, 2015 at 7:03 am

    Hi, I tried ALTER command to reduce the data file size and it was perfectly worked-out. But the problem is when I check the data file size it is showing 5MB & log file also 5MB. But when I checked the database size in DB properties it is showing as 3 GB also same in physical data file size. Please shed some light on this.

    Reply
  8. Vamshi Krishna says

    November 7, 2016 at 7:27 am

    Your Command was very simple and it really helped me to reduce the tempdb size.

    Reply
  9. Johnson says

    December 7, 2016 at 6:33 am

    Great Article, there i found very informative article why to prevent shrinking of data files

    Reply
  10. Johnson says

    December 7, 2016 at 6:34 am

    Great Article, there i found very informative article why to prevent shrinking of data files:
    http://www.sqlmvp.org/do-not-shrink-your-data-files/

    Reply
  11. Awanish mishra says

    January 9, 2017 at 12:17 pm

    nice……….

    Reply
  12. Ravi Theja Madisetty says

    February 2, 2017 at 10:06 am

    Yesterday I was in a critical situation due to Temp DB drive was full and somewhere I read it is not recommended to do shrink the temp.
    Finally, this post helped to shrink temp DB.

    Reply
  13. Hermine says

    April 19, 2017 at 4:13 pm

    Thanks for sharing your thoughts about tempdb. Regards

    Reply

Leave a Reply to Junice Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Categories

  • All Articles (84)
  • Career Development (8)
  • MySQL Administration (18)
  • MySQL Performance (2)
  • SQL Server Administration (24)
  • SQL Server News (3)
  • SQL Server Performance (14)
  • SQL Server Security (3)
  • SQL Tips and Tricks (21)

Top 10 Popular Posts

  • Using sp_change_users_login to fix SQL Server orphaned users
  • MySQL SHOW USERS? – How to List All MySQL Users and Privileges
  • How to shrink tempdb
  • How to Transfer Logins to Another SQL Server or Instance
  • How to Delete Millions of Rows using T-SQL with Reduced Impact
  • T-SQL – How to Select Top N Rows for Each Group Using ROW_NUMBER()
  • New T-SQL features in SQL Server 2012 – OFFSET and FETCH
  • How to Kill All MySQL Processes For a Specific User
  • Using exec sp_who2 to help with SQL Server troubleshooting
  • How to fix “conversion failed when converting date and/or time from character string”

Recent Posts

  • How to fix “conversion failed when converting date and/or time from character string”
  • Using SQL GROUPING SETS for Multiple GROUP BY Queries in a Single Query
  • How to Setup MySQL Master Master Replication
  • How To Use SQL to Convert a STRING to an INT
  • How to set up MySQL Replication Tutorial

Search

Connect

  • Twitter
  • Facebook
  • RSS

About

  • Cookie Policy
  • Disclaimer
  • About
Copyright ©