How to quickly copy a table?

In the previous article, the question I left for you is how to copy data in the two tables. If it is possible to control the number of scanned rows of the source table and the locking range to be small, we can simply use the insert ... select statement.

Of course, in order to avoid adding read locks to the source table, a more secure solution is to write the data to an external text file first, and then write it back to the target table. At this time, there are two commonly used methods. In the following content, I will expand these two methods with you in detail.

For the sake of illustration, I first create a table db1.t, and insert 1000 rows of data, and create a table db2.t with the same structure.

create database db1;
use db1;

create table t(id int primary key, a int, b int, index(a))engine=innodb;
delimiter ;;
create procedure idata()
begin
declare i int;
set i=1;
while(i<=1000)do
insert into t values(i,i,i);
set i=i+1;
end while;
end;;
delimiter ;
call idata();

create database db2;
create table db2.t like db1.t

Suppose we want to export the data rows with a>900 in db1.t and insert them into db2.t.

mysqldump method

One way is to use the mysqldump command to export the data as a set of INSERT statements. You can use the following command:

mysqldump -h

Guess you like

Origin blog.csdn.net/yzh_2017/article/details/128748839