Skip to content

Instantly share code, notes, and snippets.

@urbels
Created July 17, 2013 08:02
Show Gist options
  • Save urbels/6018646 to your computer and use it in GitHub Desktop.
Save urbels/6018646 to your computer and use it in GitHub Desktop.
Table optimize
#!/bin/sh
echo -n "MySQL username: " ; read username
echo -n "MySQL password: " ; stty -echo ; read password ; stty echo ; echo
mysql -u $username -p"$password" -NBe "SHOW DATABASES;" | grep -v 'lost+found' | while read database ; do
mysql -u $username -p"$password" -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 -u "$username" -p"$password" -NBe "OPTIMIZE TABLE $name;" "$database"
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment