Fastest way to copy a table in mysql?

I want to copy a table to MySQL. What is the fastest way? Like this?

CREATE TABLE copy LIKE original; INSERT INTO copy SELECT * FROM original; 

or

 CREATE TABLE copy SELECT * FROM original; ALTER TABLE copy ADD PRIMARY KEY (id); 

or is there another way?

EDIT: I'm worried about re-creating indexes, how does mysql continue to execute these statements?

PS. cannot use command line tools like mysqldump should be on the fly.

+73
sql mysql
May 31 '10 at 12:09 a.m.
source share
10 answers

This immediately copies the table structure, but not the data:

 CREATE TABLE copy LIKE original; 

This creates all the indexes on the original table.

It works this way in mysql 5.1.39 .

+44
May 31 '10 at 13:03
source share

The fastest way to use MyISAM tables when storing indexes), and possibly other storage mechanisms:

 CREATE TABLE copy LIKE original; ALTER TABLE copy DISABLE KEYS; INSERT INTO copy SELECT * FROM original; ALTER TABLE copy ENABLE KEYS; 

You want to disconnect your keys to load the database, and then recreate the keys at the end.

Similarly, for InnoDB :

 SET unique_checks=0; SET foreign_key_checks=0; ..insert sql code here.. SET unique_checks=1; SET foreign_key_checks=1; 

(As stated in the comments.)

+33
Mar 17 '14 at 23:09
source share

From the manual:

"CREATE TABLE ... SELECT does not automatically create any indexes for you. This is done intentionally to make the statement as flexible as possible. If you want to have indexes in the created table, you must indicate them before the SELECT statement:"

 CREATE TABLE bar (UNIQUE (n)) SELECT n FROM foo; 

You can specify indexes and data types (to avoid data type conversion) using both CREATE TABLE LIKE and CREATE TABLE SELECT . Which one will be faster will depend on your setup.

+9
May 31 '10 at 12:45
source share

Does create table mynewtable (select * from myoldtable) in mysql? If so, you can try.

+5
May 31 '10 at 12:19
source share

The best way to copy the structure and all records from one table to another table (by creating a new table) is this query ...

CREATE TABLE new_table LIKE old_table; INSERT new_table SELECT * FROM old_table;

+4
Oct 10 '14 at 12:14
source share

To copy using indexes and triggers, run the following 2 queries:

 CREATE TABLE newtable LIKE oldtable; INSERT newtable SELECT * FROM oldtable; 

To copy only structure and data, use the following command:

 CREATE TABLE tbl_new AS SELECT * FROM tbl_old; 
+4
Dec 14 '16 at 5:02
source share

Try SELECT INTO and use variable as an intermediate.

You will have to create a receive table, first, have the same structure as the original table.

Best of all, it's internal, so it's fast. However, you will lose your indexes.

+2
May 31 '10 at 12:15
source share

if you use MyISAM, you can also copy and rename induvidual files. .MYD, .MYI, .FRM files in the backend

+2
Jun 06 '13 at 14:13
source share

Perhaps you can take a look at SHOW CREATE TABLE .

Actions:

  • Go to phpmyadmin

  • Go to SQL

Run this request

 SHOW CREATE TABLE `the_old_table`; 
  • Click Options Check Full Texts and click Go.

The result is a complete CREATE TABLE statement. Edit the request until you are happy.

Resource: http://dev.mysql.com/doc/refman/5.7/en/show-create-table.html

+1
Feb 08 '16 at 14:16
source share
 CREATE TABLE copy SELECT * FROM original; 

This is a quick way, but perhaps not the fastest reason for indexes.

0
Jun 18 '14 at 15:48
source share



All Articles