MarcosBL
1/20/2014 - 5:01 PM

Simple MyISAM tables optimization - Just put it on a cron

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