有些时候对于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=sw+1;
-
set var=var+1;
-
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=sw+1;
-
set k=k+1;
-
end while;
-
if (sw%2000=0) then
-
commit;
-
start transaction;
-
end if;
-
end LOOP loop_lable01;
-
commit;
-
close wa01;
-
close pk01;
-
-
END
-
//
-
delimiter ;
要是有更多更复杂的数据关系建议考虑nosql了,毕竟关系型数据库处理的关系能力有限。
阅读(3513) | 评论(0) | 转发(1) |