« Great ColdFusion 8 article; how about a comparison to PHP/RoR? | Main | What a great idea- switch oil subsidies to become renewable energy subsidies »

Shrinking/optimizing databases to save filespace

The other day, our company's managed hosting provider alerted me to the fact that the hard drive on our SQL 2000 database server was running out of filespace. So I logged on to the server to check out what was going on. What I found was that besides accumulating a few extra testing databases over the years, certain of our high-traffic databases were taking up quite a lot of space on the hard drive. And while looking around, I remembered a technique that I haven't had to use for years that helped me to regain a lot of the space on the drive: "shrinking" SQL Server databases.

As a database goes through time, with more and more inserts and deletes written to it, two things can happen to the way it uses filespace: first, as it needs more space, it will allocate more filespace for itself in large chunks; second, it will probably have more and more free space contained in gaps between records because earlier records have been deleted. So databases can grow to take up more space over time, but they may not need all of it. That's where the maintenance of shrinking, and optimizing a database should come in.

SQL Server
"Shrinking" a database refers to the process of rewriting it on the filesystem so that you removed the extra space where deleted records used to be so that all of the remaining records are contiguous. That takes up a lot more space. But databases will still take up the same amount on your hard drive even after you've shrunk them because, as mentioned above, they've allocated specific amounts of space for themselves. So, you then want to remove that extra free space by telling the database to let go of it; in SQL Server parlance, that's called "truncating". Here's how you do shrinking and truncating of your databases in Enterprise Manager:

First, right-click on your database in the left-hand pane of Enterprise Manager. From the contextual menu that appears, choose "All Tasks -> Shrink Database...":
mssql-shrink-menu.gif Next you'll see the Shrink Database dialog:
mssql-shrink.gif You may be tempted to just set a smaller percentage of free space and click on the OK, but for some reason, across many years and SQL Server installations, I've found that never to do anything-- not even to release any free space. What does work is to click on the "Shrink files" button in the lower portion of the dialog, which will bring you to a new dialog box:
mssql-shrink-files.gif Here, just choose the first option and then click on the "OK" button at the bottom. Depending on the size of your database and the number of records, this operation may take a few seconds or several minutes (I've seen it run over 20 minutes).

MySQL
You can perform the same functions as shrinking and truncating in MySQL, although it's given a different name: "optimizing". Here's what the MySQL help docs have to say about optimizing:

This ...should be used if you have deleted a large part of a table or if you have made many changes to a table with variable-length rows (tables that have VARCHAR, BLOB, or TEXT columns). Deleted records are maintained in a linked list and subsequent INSERT operations reuse old record positions. You can use this command to reclaim the unused space and to defragment the datafile.
You can optimize your tables via the SQL command OPTIMIZE TABLE [tablename], or you can use the MySQL Administrator. Just click on your database in the catalogs list, and click on the maintenance button at the bottom. Check off the "optimize" radio button, then click on the "Next" button at the bottom. Click on "Optimize Tables" on the next dialog, at you're done!

mysql-optimize.gif

Comments (1)

I saw a bin asking for batteries at the moment to be recycled, how precisely are they recycled and what in to?

Post a comment