create procedure salesAdd()
begin
declare i int default 11;
while i <= 4000000 do
insert into blog.t_sales
(`username`,`password`,`register_time`,type) values
(concat("jack",i),MD5(concat("psswe",i)),from_unixtime(unix_timestamp(now()) - floor(rand() * 800000)),floor(1 + rand() * 4));
set i = i + 1;
end while;
end
然后调用存储过程
call salesAdd()
改进版
虽然使用存储过程添加数据相对一个个添加更加便捷,快速,但是添加几百万数据要花几个小时时间也是很久的,后面在网上找到不少资料,发现mysql每次执行一条语句都默认自动提交,这个操作非常耗时,所以在在添加去掉自动提交。设置 SET AUTOCOMMIT = 0;
create procedure salesAdd()
begin
declare i int default 1;
set autocommit = 0;
while i <= 4000000 do
insert into blog.t_sales
(`username`,`password`,`register_time`,type) values
(concat("jack",i),MD5(concat("psswe",i)),from_unixtime(unix_timestamp(now()) - floor(rand() * 800000)),floor(1 + rand() * 4));
set i = i + 1;
end while;
set autocommit = 1;
end