There’s a simple command to automatically check, repair and optimize all tables in all databases when you’re running a MySQL server on Linux / Unix / BSD.
mysqlcheck -u{username} -p{password} –check –optimize –auto-repair –all-databases
Where:
{username} – user full access to the all databases
{password} – password for {user} account
mysqlcheck uses the SQL statements CHECK TABLE, REPAIR TABLE, ANALYZE TABLE, and OPTIMIZE TABLE in a convenient way for the user. It determines which statements to use for the operation you want to perform, and then sends the statements to the server to be executed.
See the mysqlcheck docs for more information.