Best practice with mysql innodb to rename huge table when table with same name already exist
-
06-07-2021 - |
Question
I Use Mysql 5.5.. + INNODB and windows server.
The case(make it simple then real case):
I have 2 tables 1GB with name new_car
and car
table 1GB.
I to replace car
table with new_car
table every 10 hours not manually(auto by code) - important to do it fast(real website data).
I read(say that drop its problem in innodb) :http://www.mysqlperformanceblog.com/2011/02/03/performance-problem-with-innodb-and-drop-table/
solution1:
DROP TABLE car;
RENAME TABLE new_car TO car;
Solution2(making drop in the end -maybe it not block the table to access that happen during drop):
RENAME TABLE car TO temp_car;
RENAME TABLE new_car TO car;
DROP TABLE temp_car;
Solution3(Truncate delete fast the table and create empty table then maybe drop action after should be very fast):
TRUNCATE TABLE car;
DROP TABLE car;
RENAME TABLE new_car TO car;
Solution4:
RENAME TABLE car TO temp_car;
RENAME TABLE new_car TO car;
TRUNCATE TABLE temp_car;
DROP TABLE temp_car;
Which solution is the best and why or please write other better solution?
Thanks
Solution
I don't understand why you would DROP the table. You always want those 2 tables and you'd (I'd assume be refilling up the New_Car table...
RENAME TABLE car TO temp_car;
RENAME TABLE new_car TO car;
TRUNCATE TABLE temp_car;
RENAME TABLE temp_car TO new_car;
OTHER TIPS
RENAME TABLE car TO temp_car, new_car TO car;
TRUNCATE TABLE temp_car;
DROP TABLE temp_car;
As described in RENAME TABLE documentations:
RENAME TABLE renames one or more tables. Renaming operations are performed left to right.
Instead of running multiple queries for rename, you can do multiple table renames in a single query like this:
RENAME TABLE car TO temp_car, new_car TO car
DROP TABLE temp_car
Although please be aware that such a table rename might cause locking/latency issues for several seconds. It depends on your application if that is acceptable.
Use replace into It takes like 5 min or Use Load infile to bulk it