The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

如何最佳化 MySQL 所有資料庫與表格的狀態

我寫了個 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