Compressing MySQL Tables For Fun and Profit (or just Save Space)
Occasionally, you have a database table that holds a lot of text data and it’s not accessed regularly (log tables for example). InnoDB provides an easy way to compress your tables on disk so they use less space.
Prerequisites
The most important things are that innodb_file_per_table must be set in your /etc/my.cnf file. If you don’t have this enabled already you’ll need to perform a mysqldump on the database, add the setting, and then restore the database. I haven’t been able to find a way around this so it’s of limited use on production servers. The innodb_file_per_table is a good setting to have turned on anyways so I would suggest you enable it.
The second most important think is the Barracuda file format must be enabled when MySQL was built. I think the majority of the packaged versions of MySQL do (I know Ubuntu and CentOS do).
Process
To compress your table you need to run the following for each table you want to compress:
Depending on the size of your table this may take a long time as it has to rewrite the whole table. During this process your table will be inaccessible to others so it’s best to do this off hours.
Downside
The downside to this process is that because the table is compressed accessing the data may take more time. In our tests we didn’t see an large difference (~2%) searching 2 million rows of email log data but your mileage may vary.
Even if you don’t do this on your production server I’ve done it on development machines to save space when that was a factor.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows