Regularly repairing and optimizing MySQL/MariaDB tables reclaims unused space, fixes corrupted indexes, and improves query performance. Here is how to do it at both the account and server level.
Optimize via phpMyAdmin
phpMyAdmin > select your database > check all tables > dropdown: "Optimize table." Or use SSH: mysqlcheck -u dbuser -p --optimize dbname
01. Server-Wide Optimization (Root)
# Check, repair, and optimize all databases
mysqlcheck -u root -p --all-databases --check --optimize --auto-repair
# Or separately:
mysqlcheck -u root -p --all-databases --repair
mysqlcheck -u root -p --all-databases --optimize
02. Automate With Cron
# Weekly optimization - add to root crontab
0 3 * * 0 mysqlcheck -u root --all-databases --optimize --auto-repair >/dev/null 2>&1
For InnoDB tables, OPTIMIZE TABLE actually performs a table rebuild (ALTER TABLE ... ENGINE=InnoDB). This can temporarily lock the table and use significant disk space for large tables. Run during low-traffic periods. For ibdata1 bloat, see Shrink ibdata1.
For backups before maintenance: Backup MySQL.
Database Issues?
Open a Support TicketQuick Recap
- phpMyAdmin: Check all > Optimize table
- SSH:
mysqlcheck --optimize --auto-repair - Server-wide:
--all-databasesflag (root only) - Automate weekly via cron
- InnoDB optimize rebuilds tables - Run off-peak
Database maintenance · Last updated March 2026 · Browse all Server Maintenance articles
