Skip to content

Instantly share code, notes, and snippets.

@MarcosBL
Created January 20, 2014 17:01
Show Gist options
  • Save MarcosBL/8524039 to your computer and use it in GitHub Desktop.
Save MarcosBL/8524039 to your computer and use it in GitHub Desktop.
Simple MyISAM tables optimization - Just put it on a cron
#!/bin/bash
# Get a list of all fragmented tables
FRAGMENTED_TABLES="$( mysql -e ';use information_schema; SELECT TABLE_SCHEMA,TABLE_NAME FROM TABLES WHERE ENGINE="MyISAM" AND TABLE_SCHEMA NOT IN ("information_schema","mysql") AND Data_free > 0' | grep -v "^+" | sed 's,\t,.,' )"
for fragment in $FRAGMENTED_TABLES; do
database="$( echo $fragment | cut -d. -f1 )"
table="$( echo $fragment | cut -d. -f2 )"
[ $fragment != "TABLE_SCHEMA.TABLE_NAME" ] && echo "Optimizando $fragment" && mysql -e "USE $database; OPTIMIZE TABLE $table;" > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment