A Defragmentation Script to Optimize Fragmented Tables in MySQL

by Phil Dufault on August 7, 2008 · 9 comments

in Featured,Linux,MySQL

I wrote a BASH script that will defragment / defrag any fragmented tables.  It’s pretty tidy little script in BASH;  you can supply usernames and passwords, and even run it against remote MySQL databases.

Here’s an example of it’s output:
root@lit [~]# mysqlDefragmenter
[x] MySQL defragmenter v0.7.4
[x] Written by Phil Dufault ( http://www.dufault.info/ )
[x] Found 76 databases
[x] Checking eximstats ... found 2 fragmented tables.
[x] Optimizing sends ... done
[x] Optimizing smtp ... done
[x] Checking random_forum ... found 9 fragmented tables.
[x] Optimizing datastore ... done
[x] Optimizing forum ... done
[x] Optimizing humanverify ... done
[x] Optimizing posthash ... done
[x] Optimizing postparsed ... done
[x] Optimizing search ... done
[x] Optimizing stopbotsregistry ... done
[x] Optimizing strikes ... done
[x] Optimizing thread ... done
[x] Checking settiphs_blog ... found 1 fragmented table.
[x] Optimizing wp_options ... done
[x] 12 tables were fragmented, and were optimized.

I find this a great script — maybe a feature to add later is a way to prevent it from optimizing really large tables.

Here’s a link to the source:
mysqlDefragmenter

Any other features you’d like to see in it?   Comment!

{ 6 comments… read them below or add one }

Rubén Ortiz November 27, 2008 at 12:11 pm

You did a great job, is a very good script :) Thanks!

Phil Dufault November 30, 2008 at 10:48 pm

Thanks for the feedback! Let me know if you see any improvements I can make in the script.

Banned December 18, 2008 at 12:39 pm

Thanks dude, nice job!

tom3k May 1, 2009 at 1:13 am

i use this on a ton of my servers.

cheers!

fsckr October 24, 2009 at 9:18 am

The process of optimizing/fixing fragmented tables has always been kinda painful especially with 100′s of relation tables.

Hundreds of thousands of users thank you…

Dave August 6, 2010 at 6:03 am

Great script! How about a parameter to only operate on a specific database (and it’s tables). I need to work on just one table as I am not responsible for all of the tables on this server.

Leave a Comment

{ 3 trackbacks }

Previous post:

Next post: