MySQL: OPTIMIZE TABLE vs. myisamchk
They do the same thing, but there is a difference and a grave danger.

Here is the difference:


OPTIMIZE TABLE is done in the mysql client or in a mysql session.
myisamchk is a utility, not a client. Thus, there are no table locking safeguards. Therefore, you should never run myisamchk -r -a -S in a live system. Otherwise, you can quickly (in fact, instantaneously) corrupt a MyISAM table.


You could move the MyISAM table to another folder and run myisamchk against the table files without interference from mysqld or any client connections thereof.

Other difference


mysqld will create temp tables
myisamchk can figure out if it needs to make a temp table or do things in place.


More info

Doing OPTIMIZE TABLE will does a complete copy of the table. It does this under the hood

ALTER TABLE mytable ENGINE=MyISAM;
ANALYZE TABLE mytable;


Doing OPTIMIZE TABLE could make the temp table in /tmp and not in place like myisamchk

Please remember that tmpdir is mapped somewhere. That's where space may give out.