MySQL如何快速批量插入1000w条数据

来自:互联网
时间:2021-03-20
阅读:

听说有个面试题是: 如何快速向mysql中插入1000w条数据?

我私下试了一下, 发现插入10000条数据用了0.9s, 插入10w条数据用了4.7s, 插入100w条数据用了58s左右,1000w条数据,我的笔记本吭哧了5分钟,自己停了, 心中1000w只草泥马呼啸而过,我用的是下面的代码:

-- 进入数据库
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);
$

生成10w条数据,用了4.44秒

MySQL如何快速批量插入1000w条数据

生成100w条数据用了58.62秒,差不多1分钟

MySQL如何快速批量插入1000w条数据

生成1000w条数据, 屏幕前的大佬可以去试一下, 哈哈, 我 Ctrl+C把进程kill了!

MySQL如何快速批量插入1000w条数据

返回顶部
顶部