database

How do I check and repair MySQL tables?

To check a certain table for consistency with MySQL after you select the database, you can run:

mysql$ CHECK TABLE table_name;

If the tables in your databases say OK then there is not an issue.  However if instead of OK the CHECK TABLE reports Corrupt, then you have a broken table and you have to fix it as soon as possible.

Here is an example of a broken table after a CHECK REPAIR searchindex; :

+------------------+-------+----------+------------------------------------+
| Table | Op | Msg_type | Msg_text |
+------------------+-------+----------+------------------------------------+
| test.searchindex | check | error | Key in wrong position at page 4096 |
| test.searchindex | check | error | Corrupt |
+------------------+-------+----------+------------------------------------+

To fix the CORRUPTED or BROKEN table as also known you have to issue the command:

mysql$ REPAIR TABLE yourtable_name;

Depending on your table size after a while you will get an OK message like the one below.

+------------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------+--------+----------+----------+
| test.searchindex | repair | status | OK |
+------------------+--------+----------+----------+
1 row in set (0.08 sec)


Be aware that sometimes in order to fix a broken table you have to use the MySQL repair extended function.

Expect The EXTENDED REPAIR function option to take a much more time, even sometimes with large databases with millions of records it could take hours, especially if the MySQL server is serving other client requests as well.

This terrible siutation sometimes occurs because of MySQL locks.

ANALYZE TABLE does synchronization of the information concerning the variables within tables that has a INDEX key settled according to the database to which they belong.  In other words, executing ANALYZE TABLE to your database tables every now and then and that would probably help in speeding up the code executed in the SQL that has JOINS involved.

The second one OPTIMIZE TABLE is natively supported with MyISAM SQL database types, and secondary supported with Innodb, where the Optimize with Innodb is done in a non-traditional way.

When invoked to process an Innodb table OPTIMIZE TABLE does use ALTER TABLE to achieve an Innodb table optimization.

In practice what the optimize table does is defragmentation of the table unto which it’s executed.

An example of the optimize table is for instance:

OPTIMIZE TABLE your_table_name;

In order to find out which tables need to be defragmented or in other words needs optimize table you have to issue the command:

show table status where Data_free!=0;

NOTE: You have to issue this command on each of your databases.

8 Comments

8 Comments

  1. Linux Forum

    December 1, 2010 at 6:59 pm

    Thankyou for helping out, great information.

  2. Keneth Honaker

    December 3, 2010 at 10:19 am

    I realy like this angle that you have on the subject. I wasnt thinking on this at the time I started browsing for tips. Your ideas were totally simple to understand. Im glad to find that there’s an individual here that obviously understands precise what its is talking about.

  3. Daniell Tomaino

    December 5, 2010 at 6:02 pm

    Major thankies for the blog.Thanks Again.

  4. Chula Vista Plumber

    December 8, 2010 at 3:02 am

    I appreciate you sharing this post.Thanks Again. Awesome.

  5. linux forum

    December 12, 2010 at 3:12 pm

    Great post, I conceive website owners should learn a lot from this web site its very user friendly .

  6. Kimberley Pucillo

    December 20, 2010 at 2:27 am

    Most what i read online is trash and copy paste but i think you offer something different. Keep it like this.

  7. crazyupload.net

    December 23, 2010 at 1:48 pm

    I enjoyed to find this article . I like your point of view. Thanks a lot. Cheers

  8. halı saha yapımı

    August 13, 2012 at 3:39 am

    Hi there! I just wish to give a huge thumbs way up for the great info you have here on this post. I will be coming back to your site for more soon.

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top