First, I have to tell you that “fragmentation” is not the best word I should use, but that was the closest to what I wanted to say. The base of the fragmentation checker script is Peter Zaitsev’s 2008 article about Information_schema. I used that query to get the results I needed.
Some background: a few weeks ago we got some problems related to free space (well, they weren’t real problems, but they could lead to one, and we had to act fast). InnoDB works in a bit of a strange way. It doesn’t like to free up space after data has been deleted, and because of this, the datafiles will grow to the end of the world. The first thing we could do to reclaim free space is to use the
innodb_file_per_table
configuration option, which will split up the database into a lot of .ibd datafiles, instead of one big ibdata file. If you got your data in different tables, you can run “OPTIMIZE TABLE” from mysql, which will compact the datafiles (it creates a new one, copies the table contents onto this new table, deletes the old file, and renames the new) for you. It takes a lot of time to run, and you have to know that the “optimize” command will also be replicated, so after you do this on the master, be aware that it will happen on the slaves too. So, when you take some maintenance time, you have to factor this in. It’s easy to determine what tables would be optimized. You can check the table size from the data dictionary, and you can see how big that table is on disk. If they differ, then you can reclaim the space that is the difference between the two sizes.
(more…)