Mysql 存储过程 插入100W条数据
drop procedure insert_rand_tkt_num_01;
-- 插入100W条数据
delimiter $$
create procedure insert_rand_tkt_num_01(vv_num INT ) -- vv_num 需生成的随机数条数
begin
declare i int default 0;
declare count int default 0;
/insert ignore into 有就不插入,没有就插入/
set @exesql = concat("insert ignore into rand_tkt_num(number_id) values ");
set @exedata = "";
set i =0, count =0;
WHILE count< vv_num DO
set @exedata = concat(@exedata, ",(CEILING(RAND()*9000000 + FLOOR(1 + (RAND() * 5)) * 10000000))");
set count=count+1;
set i=i+1;
/1000条数据组合在一起/
if i%1000=0
then
set @exedata = SUBSTRING(@exedata, 2);
set @exesql = concat("insert ignore into rand_tkt_num(number_id) values ", @exedata);
prepare stmt from @exesql;
execute stmt;
DEALLOCATE prepare stmt;
set @exedata = "";
end if;
end WHILE;
if length(@exedata)>0 then
set @exedata = SUBSTRING(@exedata, 2);
set @exesql = concat("insert ignore into rand_tkt_num(number_id) values ", @exedata);
/* set @exesql = concat("select "", @exesql, "" from dual"); */
prepare stmt from @exesql;
execute stmt;
DEALLOCATE prepare stmt;
end if;
commit;
end $$