我寫了個 Shell Script 可以將「所有資料庫」的「所有表格」最佳化,如下:
#!/bin/sh
DBs=`echo "SHOW DATABASES" | mysql -u root`
db_row=0
for db in $DBs ; do
let db_row++
if [ $db_row -gt 1 ]; then
echo ""
echo "Optimizing database \`$db\` "
echo ""
TBs=`echo "SHOW TABLES" | mysql -u root $db`
tb_row=0
for tb in $TBs ; do
let tb_row++
if [ $tb_row -gt 1 ]; then
echo -n "Optimizing $db.$tb ... "
echo "OPTIMIZE TABLE \`$tb\`" | mysql -u root $db
echo "Done"
fi
done
fi
done
exit 0