Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running optimize on table copy?

I have an InnoDB table in MySQL which used to contain about 600k rows. After deleting 400k+ rows, my guess is that I need to run an OPTIMIZE.

However, since the table will be locked during this operation, the site will not be usable at that time. So, my question is: should I run the optimize on the live database table (with a little under 200k rows)? Or is it possible to create a copy of that table, run the OPTIMIZE on that copy and after that rename both tables so the copy the data back to the live table?

like image 864
libeco Avatar asked Jan 23 '26 23:01

libeco


1 Answers

If you create a copy, then it should be optimised already if you do CREATE TABLE..AS SELECT... No need to run it separately

However, I'd consider copy the 200k rows to keep into a new table, then renaming the tables. This way is less steps and less work all round.

CREATE TABLE MyTableCopy AS
SELECT *
FROM myTable
WHERE (insert Keep condition here);

RENAME TABLE
    myTable TO myTable_DeleteMelater, 
    MyTableCopy TO myTable;
like image 100
gbn Avatar answered Jan 25 '26 18:01

gbn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!