Two different operations that are often confused.
Repair
Fixes a table reporting itself as corrupt – usually after an interrupted write. The symptom is an error naming a specific table and telling you to repair it. In phpMyAdmin, select the table, then choose Repair table from the operations dropdown.
If tables corrupt repeatedly, that is not something to keep repairing. Tell us, because the cause is usually something else – a process being killed for exceeding limits, or a disk problem.
Optimise
Reclaims space left behind by deleted rows. Deleting a hundred thousand rows does not necessarily shrink the file; optimising rebuilds it. Worth doing after a large cleanup, and largely pointless as a routine.
When it actually helps
- After clearing a large volume of expired sessions or scheduled actions
- After deleting years of post revisions or logs
- When a table's overhead is a large proportion of its size – phpMyAdmin shows this
What optimising will not fix
It will not make a slow query fast. Slowness is nearly always a missing index or a query doing too much work, not fragmentation. Optimising a table and finding no difference is the normal outcome – look at the query instead.
Take a backup first
Both operations lock the table while they run, so do them at a quiet time, and export the database first. On a busy shop, a locked table means errors for customers.