Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does MySQL's RENAME TABLE statment work/perform?

MySQL has a RENAME TABLE statemnt that will allow you to change the name of a table.

The manual mentions

The rename operation is done atomically, which means that no other session can access any of the tables while the rename is running

The manual does not (to my knowedge) state how this renaming is accomplished. Is an entire copy of the table created, given a new name, and then the old table deleted? Or does MySQL do some magic behind the scenes to quickly rename the table?

In other words, does the size of the table have an effect on how long the RENAME table statement will take to run. Are there other things that might cause the renaming of a block to significantly block?

like image 427
Alan Storm Avatar asked Sep 15 '25 00:09

Alan Storm


2 Answers

I believe MySQL only needs to alter metadata and references to the table's old name in stored procedures -- the number of records in the table should be irrelevant.

like image 198
Alex Martelli Avatar answered Sep 16 '25 13:09

Alex Martelli


In addition to altering the metadata, it also renames the associated .FRM file. While they can claim it being an "atomic" operation, this is an actual comment in the code for the mysql_rename_tables function...

/* Lets hope this doesn't fail as the result will be messy */

=)

like image 30
great_llama Avatar answered Sep 16 '25 15:09

great_llama