Over time, MySQL databases can become fragmented or develop errors, particularly after frequent inserts, updates, and deletions. Optimising and repairing your databases keeps them running efficiently.
Please note: Screens and options may vary slightly depending on your cPanel version and hosting plan.
Optimising via phpMyAdmin
- Open phpMyAdmin in cPanel.
- Select the database.
- Click Check All to select all tables.
- From the With selected dropdown, choose Optimize table.
- phpMyAdmin will defragment the tables and reclaim unused space.
Repairing via phpMyAdmin
If tables have errors (e.g. after a server crash):
- Open phpMyAdmin.
- Select the database.
- Select the affected tables (or all tables).
- From the With selected dropdown, choose Repair table.
Checking Table Health
- In phpMyAdmin, select all tables.
- From the With selected dropdown, choose Check table.
- The status will show whether each table is OK or needs repair.
Via SSH (Command Line)
# Check all tables in a database
mysqlcheck -u username_dbuser -p username_database
# Optimise all tables
mysqlcheck -u username_dbuser -p --optimize username_database
# Repair all tables
mysqlcheck -u username_dbuser -p --repair username_database
# Check, optimise, and auto-repair
mysqlcheck -u username_dbuser -p --auto-repair --optimize username_database
When to Optimise
- After deleting a large amount of data.
- After importing large datasets.
- If your website or application feels sluggish and database queries are slow.
- As routine maintenance — monthly optimisation is good practice.
Tips
- Optimising locks tables temporarily. Run optimisation during low-traffic periods.
- For WordPress sites, plugins like WP-Optimize can automate database cleanup and optimisation.
- If repair fails, you may need to restore from a backup. Always maintain recent backups.
- Large InnoDB tables (the default storage engine in modern MySQL) are largely self-optimising, but occasional
OPTIMIZE TABLEcommands can still help reclaim disk space.
What Next?
- Managing Databases with phpMyAdmin — Full database management.
- Backing Up and Restoring a Database — Ensure you have backups before maintenance.