Mysql 如何批量插入数据

比如你需要造一些压测数据,150万条,怎么快速做到呢?

下面使用存储函数和存储过程来批量插入数据。

# 1.创建数据库:
create database bigData;
use bigData;

# 2.创建表:
# 部门表
drop table if exists dept;
create table dept(
id int unsigned primary key auto_increment,
deptno mediumint unsigned not null default 0,
dname varchar(20) not null default "",
loc varchar(13) not null default ""
);
# 员工表
drop table if exists emp;
create table emp(
id int unsigned primary key auto_increment,
empno mediumint unsigned not null default 0 comment "编号",
ename varchar(20) not null default "" comment "名字",
job varchar(9) not null default "" comment "工作",
mgr mediumint unsigned not null default 0 comment "上级编号",
hiredate Date not null comment "入职时间",
sal decimal(7,2) not null comment "薪水",
comm decimal(7,2) not null comment "红利",
deptno MEDIUMINT UNSIGNED not null DEFAULT 0 comment "部门编号"
);

# 3.创建存储函数用来生成随机字符串和随机号码
/*随机字符生成函数*/
create function rand_string(n int) returns varchar(255)
begin
declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
declare return_str varchar(255) default '';
declare i int default 0;
while i < n do
set return_str = CONCAT(return_str, SUBSTRING(chars_str, floor(1+RAND()*52),1));
set i = i + 1;
end while;
return return_str;
end;
# drop FUNCTION rand_string;
/*随机号码生成函数*/
create function rand_num()
returns int(5)
begin
declare i int default 0;
set i = floor(100+rand()*10);
return i;
end;
# drop function rand_num;

# 4.创建存储过程用来批量插入数据
/*批量插入员工存储过程*/
# drop procedure insert_emp;
create procedure insert_emp(in start_num int(10), in max_num int(10))
begin
declare i int default 0;
# 手动提交
set autocommit = 0;
REPEAT
	set i = i + 1;
	insert into emp(empno, ename,job,mgr,hiredate,sal,comm,deptno) values((start_num+i), rand_string(6), 'salesman', 1, curdate(), 2000, 400, rand_num());
UNTIL i = max_num END REPEAT;
commit;
end;
/*批量插入部门存储过程*/
# drop procedure if exists insert_dept;
create procedure insert_dept(in start_num int(10), in max_num int(10))
begin
declare i int default 0;
# 手动提交
set autocommit = 0;
REPEAT
	set i = i + 1;
	insert into dept(deptno, dname, loc) values((start_num+i), rand_string(10), rand_string(8));
UNTIL i = max_num END REPEAT;
commit;
end;

# 5.调用存储过程
# 添加30个部门,从101开始
call insert_dept(101, 30);
# 添加150万个员工,从1001开始
call insert_emp(1001, 1500000);

执行了好长时间。

# 5.调用存储过程
# 添加30个部门,从101开始
call insert_dept(101, 30)
> OK
> 时间: 0.073s


# 添加150万个员工,从1001开始
call insert_emp(1001, 1500000)
> OK
> 时间: 273.681s

其他(不用关注):

# 返回 0 到 1 的随机数 0.7290583464587651
select rand() from dual;
# 7.411780747037176
select 1+RAND()*52 from dual;
# 返回小于或等于 x 的最大整数 12
select floor(1+RAND()*52) from dual;
# 如果因为必须为存储函数指定一个参数报错,执行一下命令
show variables like 'log_bin_trust_function_creators';
set global log_bin_trust_function_creators=1;

当使用命令行时,语句结束符时;
若不想遇到分号结束,使用下面语句修改:

# 声明语句结束符$$
DELIMITER $$
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值