PDA

View Full Version : Ideas how to check, repaid, optimize all mysql databases, tables - ONLY non optimized



Fli
02-21-2015, 10:53 AM
How to optimize only non optimized mysql tables of all mysql databases?
I found several mysql linux commands, here:


mysqlcheck -u root -p --auto-repair -c -o --all-databases
this may not work to optimize tables


mysqlcheck -Aos -uuser -p
not tested

This script i ran and it checked all databases and its tables and optimized those that was "fragmented":

#!/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