Created
February 24, 2015 00:56
-
-
Save michabbb/e64ffaf2de973e4ef7f1 to your computer and use it in GitHub Desktop.
Defrag all Tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#echo -n "MySQL username: " ; read username | |
#echo -n "MySQL password: " ; stty -echo ; read password ; stty echo ; echo | |
mysql -NBe "SHOW DATABASES;" | grep -v 'lost+found' | while read database ; do | |
mysql -NBe "SHOW TABLE STATUS;" $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do | |
if [ "$datafree" -gt 0 ] ; then | |
fragmentation=$(($datafree * 100 / $datalength)) | |
echo "$database.$name is $fragmentation% fragmented." | |
mysql -NBe "OPTIMIZE TABLE $name;" "$database" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment