![]() |
Why you want to be restrictive with shrink of database files | |||||||||||||||||||||||||||||||||||
|
Home About me and Nucleus Partners Links Articles, tips etc. My blog Microsoft articles Newsgroups Course demo files Sync logins SMTP Alerter ![]() |
Overview Introduced in SQL Server 7.0 was the ability automatically grow and to shrink the physical size of database data and transaction log files. Auto grow of files doesn't happen before the file is full, it happens when new space is needed (like when an insert is performed), so the user will wait the time it takes to grow until the modification is completed. Auto grow and shrink can be very useful under special circumstances, for example after archiving data to some other location. However, we often see DBA's doing shrink on a regular basis and the purpose of this article is to explain some of the downsides of shrink and what actually happens when you shrink a database file. Also, it is worth noticing that the auto grow functionality was mainly added so the dba wouldn't be paged in the middle of the night because the database is full. It was never intended to be a high performance feature or to replace the need to manage space usage, especially at the high end Acknowledgements I like to thank SQL Server MVP Mark Allison, http://www.markallison.co.uk, who has provided valuable suggestions and input for this article. Versions of SQL Server This article applies to SQL Server 7.0, 2000, 2005 and 2008, where not noted otherwise. More information You can shrink a database file using either DBCC SHRINKDATABASE (which targets all files for the database) or DBCC SHRINKFILE (which targets a particular database file). I prefer SHRINKFILE. I will not go through the details of the commands here; they are documented in SQL Server Books Online. Let us first determine what actually happens when you shrink a database file: Shrinking of data file When you shrink a data file, SQL Server will first move pages towards the beginning of the file. This frees up space at the end of the file and the file can then be shrunk (or as I like to view it: "cut off at the end"). Management of transaction log file This section is only to serve as a brief introduction to the topic of transaction log file management. See below for reference. Each modification performed in the database is reflected in the transaction log file. Needless to say, these log records need to be removed (or actually: overwritten) sooner or later - or else we would end up with a huge log file or a full log file. One way is to set the database to simple recovery model. This mean that SQL Server will automatically truncate the log (or, as I like to call it: "empty the log"). Another option is to have the database in full or bulk_logged recovery model and perform regular transaction log backups (BACKUP LOG). The transaction log is emptied when you backup the log. Note that the log is *not* emptied for other backup types (like BACKUP DATABASE). See the SQL Server 2005 Books Online reference http://msdn.microsoft.com/en-us/library/ms345583.aspx for details. Make sure you read all the subsections. It is worth the 30 minutes of your time to understand how to manage the transaction log. Trust me on this. Shrinking of transaction log file SQL Server cannot move log records from the end of the log file toward the beginning of the log file. This means that SQL Server can only cut down the file size if the file is empty at the end of the file. The end-most log record sets the limit of how much the transaction log can be shrunk. A transaction log file is shrunk in units of Virtual Log Files (VLF). You can see the VLF layout using the undocumented DBCC LOGINFO command, which returns one row per virtual log file for the database: DBCC LOGINFO('myDatabase')
The interesting column is "Status". 0 means that the VLF is not in use and 2 means that it is in use.
In my example, I have 2 at the end of the file (read result from top to bottom) and this means that the file cannot currently be shrunk.In 7.0, you have to generate dummy transactions so that the usage of the log file wraps toward the beginning of the file. You can then empty the log file using BACKUP LOG and then shrink the file. As of SQL Server 2000, the generation of dummy log records is done for you when you execute the DBCC SHRINKFILE command. What you end up doing is BACKUP LOG, DBCC SHRINKFILE several times. Investigate the layout of the log file using DBCC LOGINFO in between. If you have loads of VLF (many rows returned from DBCC LOGINFO), you probably had a small file size for the log initially and then had lots of small autogrow. Having lots of VLF is a bit of a performance hit. If this is your case, consider shrinking the log file to a very small size and then expand the file size to something comfortable (a bigger size). Here are some articles specifically about management of log file size: How to Shrink the SQL Server 7.0 Transaction Log Shrinking the Transaction Log in SQL Server 2000 with DBCC SHRINKFILE Log File Grows too big Log file filling up Considerations for Autogrow and AutoShrink So what is the problem? Why shouldn't I shrink database files on a regular basis? Have a look at below list and then you can determine for yourself whether or not you want to shrink database files regularly:
In the end, only you can determine whether you want to shrink or not. With above information, you hopefully have enough to go on when making that decision. You probably know by now what I think of regular shrinking. Not to talk about autoshrink. :-) Transact-SQL code Below you find the T-SQL code I ran to prove that shrinking of a database will generate the same amount of transaction log records as the amount of data which was moved: --Script to show that shrink produces a lot of log record.
|