21+ years in hosting · Jaipur, India · INR billing with 18% GST · 24/7 email support
Blog · WebJi® India

Repair and Optimize a MySQL Database

Quick answer

Run CHECK TABLE first: OPTIMIZE TABLE rebuilds a table and reclaims space, while REPAIR TABLE only works on MyISAM, ARCHIVE and CSV tables. In phpMyAdmin, select the database, tick the tables, then choose Check or Optimize. Back up with mysqldump before either, and restore when CHECK keeps reporting corruption.

For WordPress admins facing a crashed table or a bloated database: the four maintenance statements, the CLI batch pass, and when a restore wins.

✓ In business since 2005
✓ INR + GST billing
✓ 24/7 Email Support — avg reply < 60 min
GuideWebJi®
knowledge
Read time
5 min
Sections
6
Updated
14 Nov 2026
Written by
WebJi® team

Repair or optimize: which one does the table need?

Run CHECK TABLE first: OPTIMIZE TABLE rebuilds a healthy table and reclaims space, REPAIR TABLE fixes a damaged one and only works on MyISAM, ARCHIVE and CSV, and ANALYZE TABLE merely refreshes index statistics. In phpMyAdmin, select the database, tick the tables, then pick Check or Optimize from the "With selected" menu.

The four table-maintenance statements and where each runs
CommandWhat it doesRuns on
CHECK TABLEReads the table and reports errors without changing itInnoDB, MyISAM, ARCHIVE
REPAIR TABLERebuilds a damaged index or data file (equivalent to myisamchk --recover)MyISAM, ARCHIVE, CSV; refused by InnoDB
OPTIMIZE TABLERebuilds the table and returns free space to the diskInnoDB (runs as ALTER TABLE … FORCE), MyISAM, ARCHIVE
ANALYZE TABLEUpdates index statistics for the query plannerInnoDB, MyISAM

A slow dashboard is not corruption. If pages load slowly but the data reads correctly, optimize or add an index; repairing a table that already passes the check changes nothing.

Why back up the database before either command?

Because both commands rewrite files on disk, and MySQL's own manual warns that a repair can lose rows — take a dump first, then read the file back before you trust it.

mysqldump -u user -p --single-transaction --routines dbname | gzip > dbname-2026-10-29.sql.gz

--single-transaction gives InnoDB a consistent snapshot without holding a lock for the whole dump. In cPanel, Backup → Download a Full Account Backup does the same job from the panel, and the Export tab in phpMyAdmin works for a single database.

  • Size check: gzip -l dbname-2026-10-29.sql.gz should show a size close to the database size.
  • Content check: zcat dbname-2026-10-29.sql.gz | head -20 should show CREATE TABLE lines.
  • Off-server copy: a dump stored in the same account leaves with the account.

How do you repair or optimize from phpMyAdmin?

phpMyAdmin is the shortest route when the database is small and SSH is not open: click the database, tick the tables, and choose Check table — then Repair table only for a MyISAM table that failed. Read the Msg_text column of the result; a green OK is a pass, anything else needs the full message copied out.

phpMyAdmin clears the selection after each action, so re-tick the tables every time. A repair on a 1 GB MyISAM table can run for minutes; leave the tab open until the result table appears.

How do you run the same pass from the command line?

mysqlcheck runs the same statements across a whole database in one command, with one flag per operation. Each table is locked while it is processed, so a server-wide run belongs in a maintenance window, not at noon.

mysqlcheck -c dbname -u user -p     # check every table
mysqlcheck -r dbname -u user -p     # repair the tables that failed
mysqlcheck -o dbname -u user -p     # optimize — the rebuild
mysqlcheck -a dbname -u user -p     # refresh statistics after a big import
mysqlcheck -o --all-databases -u root -p

Add --auto-repair to a check run to fix what it finds in the same pass. On WordPress, WP-CLI wraps the same tools and reads the credentials from wp-config.php: wp db check and wp db optimize call mysqlcheck without the flags.

InnoDB or MyISAM: what happens after a crash?

InnoDB — the default engine since MySQL 5.5 — recovers itself from its redo log after a crash and refuses REPAIR TABLE, while MyISAM has no crash recovery and often ends up marked as crashed until you repair it. That difference is why "table is marked as crashed" messages almost always mean MyISAM.

Crash behaviour by storage engine
BehaviourInnoDBMyISAM
After a crashRecovers from the redo log on startNeeds a check and often a repair
REPAIR TABLERefusedAccepted; may lose rows it cannot read
OPTIMIZE TABLEOnline rebuild, brief exclusive lock at start and commitLocks the table for the whole job
Files on diskOne .ibd per table.MYD, .MYI and .frm per table
Convert withALTER TABLE wp_posts ENGINE=InnoDB;

Converting a repeat offender to InnoDB is the durable fix. Run the ALTER in a quiet hour: it rebuilds the table and needs free disk space roughly the size of the table.

How do you tell a table is really corrupted?

Only specific messages mean damage — a slow admin page, a timeout or a failed plugin does not. These are the ones that justify a repair or a restore.

  • "Table is marked as crashed and should be repaired" — a MyISAM table; run the repair once.
  • Error 145 or 1194 — table marked as crashed, or corrupt.
  • "Incorrect key file for table" — index damage, usually MyISAM.
  • "Database page corruption on disk or a failed file read" — InnoDB; go to restore.
  • CHECK TABLE returns Error or Corrupt in the Msg_text column.

If CHECK returns OK and the site still fails, the fault is elsewhere: a full disk, a dropped connection or a missing table. Compare SHOW TABLES; against the table prefix in wp-config.php before you repair anything.

When should you restore instead of repairing?

Restore when a second repair fails, when the message names an InnoDB page or checksum, or when the table opens but rows are missing — a restore replaces the table with the last dump, so posts and orders written after that dump disappear.

  1. Copy the damaged table out: mysqldump -u user -p dbname wp_posts > wp_posts-broken.sql.
  2. Rename the live table so the site fails cleanly instead of half-reading it: RENAME TABLE wp_posts TO wp_posts_old;
  3. Import the dump: mysql -u user -p dbname < dbname-2026-10-29.sql, or wp db import on WordPress.
  4. Compare row counts with the old table, then drop the copy once the site has run a full day.

A single-table restore is possible: pull that table's CREATE and INSERT lines from the dump, import only those, and leave the rest of the database untouched. The WordPress restore walkthrough covers the surrounding steps.

Keeping tables small on WebJi® hosting

Optimize returns space after deletes but does not stop growth — on WordPress, revisions, expired transients and autoloaded options are the usual weight in wp_posts and wp_options. The wp_options cleanup guide covers that job, which is different from a repair.

WebJi® Linux hosting plans include phpMyAdmin, SSH and the MySQL users these commands need. If a check output is not clear, email support@webji.services with the database name, the error text and the time it started — support runs 24/7 in Hindi and English and the first reply usually lands within the hour. The office phone is not a support line.

FAQ

Frequently asked questions

Should I repair or optimize a MySQL table?

Run CHECK TABLE first. Repair is for a table that fails the check and uses MyISAM, ARCHIVE or CSV. Optimize is for a healthy table that has lost space to deletes or replaced rows — it rebuilds rather than fixes. Analyze only refreshes index statistics.

How do I repair a table in phpMyAdmin?

Open phpMyAdmin, click the database in the left column, tick the tables, then choose Check table from the "With selected" menu. If the output reports an error on a MyISAM table, repeat with Repair table. Read the Msg_text column: a green OK is a pass.

Why does REPAIR TABLE say the storage engine does not support repair?

InnoDB does not accept REPAIR TABLE — the statement works only on MyISAM, ARCHIVE and CSV, so the server refuses instead of rebuilding. For InnoDB, dump what still reads and restore, or run OPTIMIZE TABLE if the table is intact but bloated.

Does OPTIMIZE TABLE lock the table?

On InnoDB it runs as an online rebuild and takes an exclusive lock only briefly, at the start and the commit. On MyISAM the table is locked for the whole operation, so a large table keeps readers waiting. Run it in a low-traffic window.

Can a repair lose data?

Yes. MySQL's manual warns to back up a table before repairing it, because the operation can lose rows — a MyISAM repair rebuilds by dropping what it cannot read. Always take a mysqldump copy first, and restore rather than repair twice.

Last editorial update: 15 November 2026. Engine behaviour and REPAIR TABLE scope checked against the MySQL 8.0 reference manual.

WebJi® since 2005

Ready to get started with WebJi®?

Join 12,000+ businesses across India and overseas. INR pricing, 18% GST shown before payment and 24/7 email support in Hindi and English — average first reply under 60 minutes.