I am working on a script that will be a cronjob that will look at the tables in database and repair any that need it. They are myisam tables in a mysql database. My question, is there any reason to use myisamchk
syntax on the command line vs REPAIR TABLE
query syntax?
On a myisam documentation page, http://dev.mysql.com/doc/refman/5.0/en/myisamchk.html, they provide this as a way to repair all tables that need it
myisamchk --silent --force --fast --update-state \
--key_buffer_size=64M --sort_buffer_size=64M \
--read_buffer_size=1M --write_buffer_size=1M \
/path/to/datadir/*/*.MYI
would that have a different result than using
REPAIR TABLE tablename
on all of my tables? I would do it programatically with php and end up with something like (pseudocode)
$tables=query("SELECT table from information_schema.tables where table_schema='myDBName'");
foreach($tables as $table)
{
$result=query("REPAIR TABLE ".$table);
}
Notwithstanding any opinions on the practicality of doing it one way or the other, but would the actual end result be different? Thanks.
I can provide working (non-pseudo) php code if needed but I didn't think it was necessary. The reason I am using pseudocode is that at work we use our own wrapper for db access.
You shouldn't have to be repairing tables so often that automation is needed. You probably shouldn't be using MyISAM at all. But if you insist, the answers are mostly from the page you linked to:
Caution
It is best to make a backup of a table before performing a table repair operation; under some circumstances the operation might cause data loss. Possible causes include but are not limited to file system errors.
...which, if course, could also be the original cause of the problem... this sounds to me like another vote against automating this process.
Important
You must ensure that no other program is using the tables while you are running myisamchk. The most effective means of doing so is to shut down the MySQL server while running myisamchk, or to lock all tables that myisamchk is being used on.
So while the two ways of repairing tables are from a common base of code, it seems like eou should be using REPAIR TABLE
unless the server is offline. Note ask that this command also has options.
http://dev.mysql.com/doc/refman/5.6/en/repair-table.html
Update: I almost hate to add this, because it is entirely anecdotal, but I have long suspected that repairing a MyISAM table may not always find every possible thing that could be wrong with a table, leading you to think it has been repaired because it's usable again. Anything short of using the full-blown extended repair, to me, seems like you're taking a risk, and I would be most inclined to actually force a full rebuild of the entire table, with something like this, if you have a problematic table:
ALTER TABLE t1 ENGINE=InnoDB;
ALTER TABLE t1 ENGINE=MyISAM;
This would completely remove all traces of the former table and leave you with a shiny new one. Be sure you have innodb_file_per_table
set to 1 first, or you will allocate disk space to the ibdata1 system namespace, which can't easily be reclaimed.