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.
So I checked the table sizes and saw that there were about 45 gigs of data that could be freed up, so I ran the optimize commands, and we got enough free space. Hooray!
After this, I made a small Perl script (checkfrag.pl– check it out on GitHub) that can help you determine the table sizes and differences. It’s easy to use. If you run it without any arguments, it will ask you for the root password of the local database and after this, it will show you a neat table with the table sizes and the “fragmentation” ratio. Like this (and believe me, it looks much better on a terminal):
[root@sql-server bin]# /usr/local/bin/checkfragmentation.pl
Please add the mysql root password:
Table name Number of rows (M) Size of data (G) Size of Index (G) Total Size (G) Data/Index ratio Size on disk (G) Fragmentation ratio
db1.AAAAAAA 93.79 80.84 0.93 81.77 0.01 82.93 1.01
db1.BBBBBBB 95.79 5.93 12.74 18.67 2.15 18.94 1.01
db2.CCCCCCC 42.40 3.93 8.14 12.06 2.07 12.24 1.02
db1.DDDDDDD 52.09 3.88 7.89 11.77 2.03 11.95 1.01
db1.EEEEEEE 45.66 3.49 7.40 10.89 2.12 11.05 1.01
db2.FFFFFFF 33.07 4.28 5.83 10.10 1.36 10.48 1.04
db3.GGGGGGG 41.27 3.77 5.41 9.18 1.44 9.32 1.02
db1.HHHHHHH 8.73 6.90 0.19 7.08 0.03 7.19 1.02
db1.IIIIIII 31.89 2.69 3.30 6.00 1.23 6.09 1.01
db3.JJJJJJJ 48.25 2.26 2.87 5.13 1.27 5.21 1.02
I’ve made a few command-line options for the script. You could select how many rows you wanna display, you could select the directory for the mysql data files, and you can remove the header if you wanna run it on a lot of tables, and you wanna sort it with an old-fashioned Unix sort utility (remember -n and -k).
[root@sql-server bin]# /usr/local/bin/checkfragmentation.pl -h
Usage: ./checkfragmentation.pl [ -h ] [ -e ] [ -u user ] [ -p password ] [ -d mysqldatadir ] [ -n NUM ]
-h this help screen
-e no print header - useful to sort the results
-u username username with connect to mysql. Defaults 'root'
-p password password with connect to mysql. If not provided, you'll be asked for it.
-d mysqldatadir full path of mysql data files. Defaults '/var/lib/mysql'
-n NUM number of rows to display. Defaults 10
(Why Perl? I like Perl. And Perl is not Dead.)
