Skip to content

Instantly share code, notes, and snippets.

@dgitman
Created January 23, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgitman/051369274e99e3577a63 to your computer and use it in GitHub Desktop.
Save dgitman/051369274e99e3577a63 to your computer and use it in GitHub Desktop.
#!/bin/sh
mysql -NBe "SHOW DATABASES;" | grep -v 'lost+found' \
| while read database ; do
#skip system-db
if [ "$database" = "mysql" ] ; then
continue
fi
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
#skip views
if [ "$datafree" = "NULL" ] ; then
continue
fi
if [ "$datafree" -gt 0 ] ; then
echo "$database.$name is fragmented"
mysql -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