Skip to content

Instantly share code, notes, and snippets.

@romanodesouza
Last active January 1, 2016 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romanodesouza/8102221 to your computer and use it in GitHub Desktop.
Save romanodesouza/8102221 to your computer and use it in GitHub Desktop.
#!/bin/bash
for elm in $@; do
database=$(echo $elm | cut -f1 -d '.')
tables=$(echo $elm | cut -f2 -d '.')
if [ $tables == "{*}" ]; then
tables=$(mysql -uroot -e "SHOW TABLES FROM $database")
tables=$(echo $tables | sed s",^Tables_in_$database,,")
fi
for table in $tables; do
echo -ne "Optimizing table \e[0;33m$database\e[0m.\e[0;34m$table\e[0m... "
output=$(mysql -uroot -e "OPTIMIZE TABLE $database.$table")
error=$(echo $output | awk '{print $7}')
if [ "$error" == "Error" ]; then
echo -e "\e[1;31m** ERROR!! **"
echo
echo $output
echo
echo -e "\e[0m"
else
echo -e "\e[1;32mOK\e[0m"
fi
done
done
@romanodesouza
Copy link
Author

./mysql_optimize_tables.sh db.{*} db2.{table1,table2} db3.table

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment