Mysql – Reducing the size of MyISAM table

MySQL

I have a MyISAM table that currently contains about 54 million records and is 20 GB in size. Poor choices were made when this database was designed. I'm no DB pro but since this table is constantly being written to and infrequently being queried, it seems InnoDB would have been a better choice. The problem is this table has to be available for writing to almost always. One of the fields is a timestamped field. Records go back 5 years–I only need to keep the last 6 months. I've tried some deletes, it takes about 20 minutes to delete 200,000 rows during which the table is locked and cannot be written to. Does anyone have suggestions on how I can reduce the size of this table without it being locked for several hours? Is there a faster way to delete the unneeded rows? I have never converted a MyISAM table to an InnoDB table. Is this a lengthy process?

Best Answer

I finally had time to revisit this project and for completeness sake wanted to post what I ended up doing. The original problem with the DELETE FROM taking so long, I believe, is related to how I was specifying rows to be deleted. I was using a statement similar to

DELETE FROM table WHERE starttime < 20150101 limit 200000;

The starttime field is actually a DATETIME. I ended up creating a new table using

CREATE TABLE new_table LIKE existing_table;

and then changing the new table's engine

ALTER TABLE new_table ENGINE=InnoDB;

and then renaming

RENAME TABLE existing_table TO old_table, new_table TO existing_table;

After this I needed to get 6 months worth of data from the old table into the new table

INSERT INTO existing_table SELECT * FROM old_table WHERE starttime BETWEEN DATE_SUB(NOW(), INTERVAL 6 MONTH) AND NOW();

Thank you Martin for the suggestion. I'm new on serverfault so I'm not sure how I would mark his comment as an answer.

Related Topic