MySQL batch insert 1000w data

I heard that there is an interview question: How to quickly insert 1000w pieces of data into mysql?
I tried it privately, and found that it took 0.9s to insert 10000 pieces of data, 4.7s to insert 10w pieces of data, and 58s to insert 100w pieces of data. About 1000w pieces of data, my notebook chuckled for 5 minutes, then stopped by myself, and 1000w grass and mud horses whizzed past in my heart. I used the following code:

-- 进入数据库
use test;
-- 显示所有表
show tables;
-- 创建majors表
create table majors(id int,  major varchar(255));
-- 定义结束符$
delimiter "$";
-- 创建存储过程,定义存储方法
create procedure batchInsert(in args int)
begin
declare i int default 1;
-- 开启事务(重要!不开的话,100w数据需要论天算)
start transaction;
while i <= args do
insert into majors(id,major) value(i,concat("软件工程-",i));
set i = i+ 1;
end while;
commit;
end
$

-- 调用函数,生成数据
-- 先生成10w条试试,同时输入$, 回车执行
call batchInsert(100000);
$
  1. It took 4.44 seconds to generate 10w pieces of data
    Insert picture description here
  2. It took 58.62 seconds to generate 100w pieces of data, which is almost 1 minute
    Insert picture description here
  3. Generate 1000w pieces of data, the boss in front of the screen can try it, haha, I Ctrl+C to kill the process!
    Insert picture description here

Guess you like

Origin blog.csdn.net/SoULikeMe/article/details/112787678