有些时候对于mysql需要进行一些测试,或者项目上线前的测试,这时候就需要使用一些模拟数据。在即将上线的系统中插入接近线上的真实数据,本来使用shell也是能实现,但是发现shell确实效率太低,根本无法利用服务器资源,所以选择使用mysql的存储过程来实现。
代码贴出
一对一
t_warehouse_order.order_code -->t_warehouse_waybill.order_code 使用游标遍历单字段数据,每2000条一次事务。
-
delimiter //
-
drop procedure if exists insert_waybill;
-
create procedure insert_waybill ()
-
begin
-
declare fig int default 0;
-
declare var int default 0;
-
declare code varchar(10);
-
declare sw int default 0;
-
declare yb01 cursor for select order_code from t_warehouse_order;
-
declare continue handler for not found set fig=1;
-
open yb01;
-
start transaction;
-
loop_lable01:loop
-
fetch yb01 into code;
-
if fig=1 then
-
leave loop_lable01;
-
end if;
-
insert into t_warehouse_waybill ( id,order_code) values ( var,code);
-
set sw=sw1;
-
set var=var1;
-
if (sw%2000=0) then
-
commit;
-
start transaction;
-
end if;
-
end loop loop_lable01;
-
commit;
-
close yb01;
-
-
end
-
//
-
delimiter ;
一对多比例是1:40 使用嵌套循环 加双游标
-
delimiter //
-
drop procedure if exists packwaybill;
-
create procedure packwaybill ()
-
begin
-
declare kig int default 0;
-
declare wano varchar(20);
-
declare pkno varchar(20);
-
declare k int default 0;
-
declare sw int default 0;
-
declare wa01 cursor for select waybill_no from t_warehouse_waybill;
-
declare pk01 cursor for select package_no from t_warehouse_package;
-
declare continue handler for not found set kig=1;
-
open pk01;
-
open wa01;
-
start transaction;
-
loop_lable01:loop
-
fetch pk01 into pkno;
-
if kig=1 then
-
leave loop_lable01;
-
end if;
-
set k=0 ;
-
while k<40 do
-
fetch wa01 into wano;
-
insert into t_warehouse_package_relation_waybill (package_no,way_bill_no)
-
values ( pkno,wano);
-
set sw=sw1;
-
set k=k1;
-
end while;
-
if (sw%2000=0) then
-
commit;
-
start transaction;
-
end if;
-
end loop loop_lable01;
-
commit;
-
close wa01;
-
close pk01;
-
-
end
-
//
-
delimiter ;
要是有更多更复杂的数据关系建议考虑nosql了,毕竟关系型数据库处理的关系能力有限。
阅读(3459) | 评论(0) | 转发(1) |