First, I have to tell you, that the “fragmentation” is not the best word what I should use, but that was the closest to that 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 that results what I needed.
Some background: a few weeks ago we got some free space related problems (well, they weren’t a real problem, but they could lead onto one, and we had to act fast.) The InnoDB is working on a bit strange way, it is not like to free up space what is deleted before, and because of this, the datafiles will grow to the end of the world. The first thing what we could do to reclaim free space is to use
configuration option, what will split up the database to 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, what will compact the datafiles (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 did this on master be aware that it will happen on slaves too. So, when you take some maintenance time, you have to calculate with this. 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 is that table on disk. If they differ, then you can reclaim the space what is the difference between the two sizes.
So I’ve checked the table sizes, and I’ve seen there are about 45 gigs of data what could be freed up, so I run 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) what can help you to 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 to display, you could select the mysql data files directory, and you can remove the header if you wanna to run it on a lot of tables, and you wanna to 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.)