Article ID: 307487 - Last Review: October 20, 2009 - Revision: 6.0 How to shrink the tempdb database in SQL ServerThis article was previously published under Q307487 On This PageSUMMARY
This article discusses three methods you can use to shrink the tempdb database to a size smaller than its last configured size. The first method gives you complete control of the size of the tempdb files but it requires you to restart SQL Server. The second method shrinks the tempdb as a whole, with some limitations, which may include restarting SQL Server. The third method allows you to shrink individual files in the tempdb. The last two methods require that no activity occur in the tempdb database during the shrink operation. Note If you are using SQL Server 2005, these methods also apply. However, you should use SQL Server Management Studio instead of Enterprise Manager and Query Analyzer to perform these operations. Further note that the SQL Server management studio in 2005 does not show the correct size of tempdb files after a shrink operation. The 'Currently allocated space' value is always pulled from sys.master_files DMV and this value is not updated after a shrink operation happens for tempdb database. To find the correct size of tempdb files after a shrink operation execute the following statement in management studio: Note SQL Server 2008 is not affected by this problem (GUI showing incorrect size.) Tempdb InformationThe tempdb is a temporary workspace. Among other uses, SQL Server uses the tempdb for:
By default, the tempdb database is configured to autogrow as needed; therefore, this database may grow in time to a size larger than desired. A simple restart of SQL Server resets the size of tempdb to its last configured size. The configured size is the last explicit size set with a file size changing operation such as ALTER DATABASE with the MODIFY FILE option or the DBCC SHRINKFILE statement. This article presents three methods you can use to shrink tempdb to a size smaller than its configured size. Method 1 to Shrink TempdbThis method requires you to restart SQL Server.
Method 2 to Shrink TempdbUse the DBCC SHRINKDATABASE command to shrink the tempdb database as a whole. DBCC SHRINKDATABASE receives the parameter target_percent, which is the desired percentage of free space left in the database file after the database is shrunk. If you use DBCC SHRINKDATABASE, you may have to restart SQL Server.IMPORTANT: If you run DBCC SHRINKDATABASE, no other activity can be occurring with the tempdb database. To make sure that other processes cannot use tempdb while DBCC SHRINKDATABASE is run, you must start SQL Server in single user mode. For more information refer to the Effects of Execution of DBCC SHRINKDATABASE or DBCCSHRINKFILE While Tempdb Is In Use section of this article.
Method 3 to Shrink TempdbUse the command DBCC SHRINKFILE to shrink the individual tempdb files. DBCC SHRINKFILE provides more flexibility than DBCC SHRINKDATABASE because you can use it on a single database file without affecting other files that belong to the same database. DBCC SHRINKFILE receives the target size parameter, which is the desired final size for the database file.IMPORTANT: You must run DBCC SHRINKFILE command while no other activity occurs in the tempdb database. To make sure that other processes cannot use tempdb while DBCC SHRINKFILE executes, you must restart SQL Server in the single user mode. For more information about DBCC SHRINKFILE, see the Effects of Execution of DBCC SHRINKDATABASE or DBCCSHRINKFILE While Tempdb Is In Use section of this article.
In SQL Server 7.0 a transaction log shrink is a deferred operation and you must issue a log truncation and backup to help the shrink operation in a database. However, by default, tempdb has the trunc log on chkpt option set ON; therefore, there you do not need to issue a log truncation for that database. For additional information how to shrink a database transaction log in SQL Server 7.0, click the article number below to view the article in the Microsoft Knowledge Base: 256650
(http://support.microsoft.com/kb/256650/EN-US/
)
INF: How to Shrink the SQL Server 7.0 Transaction Log
Effects of Execution of DBCC SHRINKDATABASE or DBCCSHRINKFILE While Tempdb Is In UseIf tempdb is in use and you attempt to shrink it by using the DBCC SHRINKDATABASE or DBCC SHRINKFILE commands, you may receive multiple consistency errors similar to the following type and the shrink operation may fail:
Server: Msg 2501, Level 16, State 1, Line 1
Could not find table named '1525580473'. Check sysobjects.
-or-
Server: Msg 8909, Level 16, State 1, Line 0
Table Corrupt: Object ID 1, index ID 0, page ID %S_PGID. The PageId in the page header = %S_PGID.
REFERENCESSQL Server Books Online; topics: "DBCC SHRINKFILE"; "DBCC SHRINKDATABASE"APPLIES TO
| Article Translations
|
Back to the top
