views:

547

answers:

2

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?

+3  A: 

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.

Alex Martelli
+1: Metadata changes are easily made atomic.
S.Lott
+1  A: 

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 */

=)

great_llama
mysql 5.0.45, sql_rename.cpp, line 81.
great_llama
Failure and atomicity are different things. The fact that failure leads to a complex cleanup -- while holding a lock -- can still be atomic. Atomic can also mean slow.
S.Lott
"Atomicity refers to the ability of the DBMS to guarantee that either all of the tasks of a transaction are performed or none of them are." They took care of "failure" by trying to undo the changes. From their code comment, it looks like they're just *hoping* for "atomicity".
great_llama