Shrinks the size of the data files in the specified database.
DBCC SHRINKDATABASE
( database_name [, target_percent]
[, {NOTRUNCATE | TRUNCATEONLY}]
)
Microsoft® SQL Server™ can shrink:
DBCC SHRINKDATABASE shrinks data files on a per-file basis. However, DBCC SHRINKDATABASE shrinks log files as if all the log files existed in one contiguous log pool.
Assume a database named mydb with two data files and two log files. Both data and log files are 10 MB in size. The first data file contains 6 MB of data.
For each file, SQL Server calculates a target size, which is the size to which the file is to be shrunk. When DBCC SHRINKDATABASE is specified with target_percent, SQL Server calculates target size to be the target_percent amount of space free in the file after shrinking. For example, if you specify a target_percent of 25 for shrinking mydb. SQL Server calculates the target size for this file to be 8 MB (6 MB of data plus 2 MB of free space). Therefore, SQL Server moves any data from the last 2 MB of the data file to any free space in the first 8 MB of the data file and then shrinks the file.
Assume the first data file of mydb contains 7 MB of data. Specifying target_percent of 30 allows this data file to be shrunk to the desired free percentage of 30. However, specifying a target_percent of 40 does not shrink the data file because SQL Server will not shrink a file to a size smaller than the data currently occupies. You can also think of this issue another way: 40 percent desired free space + 70 percent full data file (7 MB out of 10 MB) is greater than 100 percent. Because the desired percentage free plus the current percentage that the data file occupies is over 100 percent (by 10 percent), any target_size greater than 30 will not shrink the data file.
For log files, SQL Server uses target_percent to calculate the target size for the entire log; therefore, target_percent is the amount of free space in the log after the shrink operation. Target size for the entire log is then translated to target size for each log file. Unlike data files, the shrinking of log files is not immediate. Each log file is marked with the target size of the shrink operation. Each subsequent log backup or log truncation attempts to shrink the file and bring its size as close to the target size as possible. Because a log file can only be shrunk to a virtual log file boundary, it may not be possible to shrink a log file to a size smaller than the size of a virtual log file, even if it is not being used. For example, a database with a log file of 1 GB can have the log file shrunk to only 128 MB. For more information about when truncation occurs, see Truncating the Transaction Log. For more information about determining virtual log file sizes, see Virtual Log Files.
The target size for data and log files as calculated by DBCC SHRINKDATABASE can never be smaller than the minimum size of a file. The minimum size of a file is the size specified when the file was originally created, or the last explicit size set with a file size changing operation such as ALTER DATABASE with the MODIFY FILE option or DBCC SHRINKFILE. For example, if all the data and log files of mydb were specified to be 10 MB at the time CREATE DATABASE was executed, the minimum size of each file is 10 MB. DBCC SHRINKDATABASE cannot shrink any of the files smaller than 10 MB. If one of the files is explicitly grown to a size of 20 MB by using ALTER DATABASE with the MODIFY FILE option, the new minimum size of the file is 20 MB. To shrink a file to a size smaller than its minimum size, use DBCC SHRINKFILE and specify the new size. Executing DBCC SHRINKFILE changes the minimum file size to the new size specified.
When using data files, DBCC SHRINKDATABASE has the NOTRUNCATE and TRUNCATEONLY options. Both options are ignored if specified for log files. DBCC SHRINKDATABASE with neither option is equivalent to a DBCC SHRINKDATABASE with the NOTRUNCATE option followed by a DBCC SHRINKDATABASE with the TRUNCATEONLY option.
The NOTRUNCATE option, with or without specifying target_percent, performs the actual data movement operations of DBCC SHRINKDATABASE including the movement of allocated pages from the end of a file to unallocated pages in the front of the file. However, the free space at the end of the file is not returned to the operating system and the physical size of the file does not change. Therefore, data files appear not to shrink when the NOTRUNCATE option is specified. For example, assume you are using the mydb database again. mydb has two data files and two log files. The second data file and second log file are both 10 MB in size. When DBCC SHRINKDATABASE mydb NOTRUNCATE is executed, Microsoft SQL Server moves the data from the later pages to the front pages of the data file. However, the file still remains 10 MB in size.
The TRUNCATEONLY option reclaims all free space at the end of the file to the operating system. However, TRUNCATEONLY does not perform any page movement inside the file or files. The specified file is shrunk only to the last allocated extent. target_percent is ignored if specified with the TRUNCATEONLY option.
The database cannot be made smaller than the size of the model database.
The database being shrunk does not have to be in single user mode, other users can be working in the database when it is shrunk. This includes system databases.
DBCC SHRINKDATABASE returns this result set when only the database name is specified (result set used pubs); values may vary:
DbId FileId CurrentSize MinimumSize UsedPages EstimatedPages
------ ------ ----------- ----------- ----------- --------------
5 1 176 96 168 168
5 2 96 63 96 56
(2 row(s) affected)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Note SQL Server does not display rows for those files not shrunk.
DBCC SHRINKDATABASE returns this result set when the database name and a target percentage (with or without the NOTRUNCATE or TRUNCATEONLY options) are specified; this result set used a target percentage of 5 (values may vary):
DbId FileId CurrentSize MinimumSize UsedPages EstimatedPages
------ ------ ----------- ----------- ----------- --------------
5 2 96 63 96 56
(1 row(s) affected)
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
This table describes the columns in the result set.
Column name | Description | |
---|---|---|
DbId | Database identification number of the file SQL Server attempted to shrink. | |
FileId | The file identification number of the file SQL Server attempted to shrink. | |
CurrentSize | The number of 8-KB pages the file currently occupies. | |
MinimumSize | The number of 8 -B pages the file could occupy, at minimum. This corresponds to the minimum size or originally created size of a file. | |
UsedPages | The number of 8 -B pages currently used by the file. | |
EstimatedPages | The number of 8-KB pages that SQL Server estimates the file could be shrunk down to. |
DBCC SHRINKDATABASE permissions default to members of the sysadmin fixed server role or the db_owner fixed database role, and are not transferable.
This example decreases the size of the files in the UserDB user database to allow 10 percent free space in the files of UserDB.
DBCC SHRINKDATABASE (UserDB, 10)
GO
ALTER DATABASE | DBCC |
Physical Database Files and Filegroups |