Skip to content

Instantly share code, notes, and snippets.

@vitalbh
Forked from romanodesouza/mysql_optimize_tables.sh
Created November 13, 2015 17:50
Show Gist options
  • Save vitalbh/69ee047c4e7a5ddb6fac to your computer and use it in GitHub Desktop.
Save vitalbh/69ee047c4e7a5ddb6fac 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment