MySQL Tip: How To Check, Repair & Optimize All Tables in All Databases

By Angsuman Chakraborty, Gaea News Network
Sunday, April 20, 2008

Here is a simple command to auto repair, check and optimize all the tables in all databases running on a MySQL server:

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

mysqlcheck is available in MySQL 3.23.38 and later.

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. The exact operations are determined by the underlying storage engine used.

You must execute it on running database. It is recommended that you kill other incoming queries (like from your website) before running this which makes it a lot faster. I simply stop my httpd server before running it. You may want to use a Site Unavailable message instead so that the MySQL server is left alone.

Discussion
May 7, 2010: 1:17 am

You may also want to include –check-upgrade to upgrade any tables to new formats if necessary.

February 23, 2010: 12:55 am

thank you so much, i was struggling with table-by-table procedure for the last few years.

January 30, 2010: 10:26 pm

A couple of caveats:
1) running any type of table check, repair command will take time, especially if the table is very large in space.
Check using SHOW TABLE STATUS like ‘tablename’ first.
2) Auto repair is an option to use with care. Sometimes you need to review the output of the CHECK TABLE command first.

Have Fun


Bjoern
May 3, 2008: 4:10 pm

Hi,

great command! But what about this:

Don’t forget to FLUSH TABLES after execution of any of the following - REPAIR TABLE, TRUNCATE TABLE, OPTIMIZE TABLE, or ANALYZE TABLE on tables that are mapped into MERGE table.

And shouldn´t we also run a ’sort-index’ ?

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :