Duplicating table in MYSQL without copying one row at a time

╄→гoц情女王★ 提交于 2019-12-12 11:09:22

问题


I want to duplicate a very large table, but I do not want to copy it row by row. Is there a way to duplicate it?

For example, you can TRUNCATE w/o deleting row/row, so i was wondering if there is something similar for copying entire tables

UPDATE: row by row insert is very painful (because of 120M rows). Anyway to avoid that?


回答1:


MySQL no longer has a reliable "copy table" functionality - many reasons for this related to how data is stored. However, the below does row-by-row insertion but is pretty simple:

CREATE TABLE `new_table` LIKE `old_table`;
INSERT INTO `new_table` (SELECT * FROM `old_table`);



回答2:


You could use INSERT INTO ... SELECT.




回答3:


If you're using MyISAM you can copy the physical files on disk. Restart the service and you'll have the new table with indexes and everything the same.




回答4:


INSERT INTO TABLE2 SELECT * FROM TABLE1



回答5:


It's nontrivial to copy a large table; ultimately the database is likely to need to rebuild it.

In InnoDB the only way is really to rebuild it, which means insert ... select or such like, however, with 120M rows as it's going to happen in a single transaction, you will probably exceed the size of the rollback area, which will cause the insert to fail.

mysqldump followed by renaming the original table then restoring the dump should work, as mysqldump may cause a commit every lots of rows. However it will be slow.




回答6:


oracle:

Create table t as select * from original_table



来源:https://stackoverflow.com/questions/2867675/duplicating-table-in-mysql-without-copying-one-row-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!