4

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?

Alana Storm
  • 164,128
  • 91
  • 395
  • 599

2 Answers2

6

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
  • 854,459
  • 170
  • 1,222
  • 1,395
1

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
  • 11,481
  • 4
  • 34
  • 29
  • 2
    mysql 5.0.45, sql_rename.cpp, line 81. – great_llama May 20 '09 at 02:04
  • 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 May 20 '09 at 02:26
  • "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 May 20 '09 at 02:34