In plsql i want to insert around 1 million rows from a staging table to actual table and currently I am using cursor for loop for that. But I understand that there is a way to speed up this using bulk insert for all command but i am struggling in the section of eliminating duplicates. Can u please help to convert the below code to use bulk collect
DECLARE
CURSOR c1 is
select
ps_item_code,
item_code,
cons_date,
shop_code,
dept_code,
class_code,
sub_class_code,
supl_code,
plu_price,
sal_qty,
sal_val_in_lc,
disc_val_in_lc,
tax_val_in_lc,
odept, oclass, osubclass
from
sales_stage;
BEGIN
FOR i in c1 loop
BEGIN
my_cnt := 0;
select count(1) into my_cnt from hps_ps_terr_sales_1415
where item_code=i.item_code and cons_date=i.cons_date and shop_code=i.shop_code and sal_qty=i.sal_qty and supl_code=i.supl_code;
IF my_cnt = 0 THEN
BEGIN
insert into sales_actual
(
ps_item_code,
item_code,
cons_date,
shop_code,
dept_code,
class_code,
sub_class_code,
supl_code,
plu_price,
sal_qty,
sal_val_in_lc,
disc_val_in_lc,
tax_val_in_lc, odept, oclass, osubclass,
dept, class, subclass
)
values
(
i.terr_code,
i.ps_item_code,
i.item_code,
i.cons_date,
i.shop_code,
i.dept_code,
i.class_code,
i.sub_class_code,
i.supl_code,
i.plu_price,
i.sal_qty,
i.sal_val_in_lc,
i.disc_val_in_lc,
i.tax_val_in_lc, i.odept, i.oclass, i.osubclass,
i.dept_code,
i.class_code,
i.sub_class_code
);
END;
END IF;
END;
END LOOP;
COMMIT;
END;
答案 0 :(得分:4)
据我所知,你真的没有理由使用PL / SQL。以下内容应该与您当前的代码完成相同的最终结果。
INSERT INTO sales_actual
(ps_item_code
,item_code
,cons_date
,shop_code
,dept_code
,class_code
,sub_class_code
,supl_code
,plu_price
,sal_qty
,sal_val_in_lc
,disc_val_in_lc
,tax_val_in_lc
,odept
,oclass
,osubclass
,dept
,class
,subclass)
SELECT i.terr_code
,i.ps_item_code
,i.item_code
,i.cons_date
,i.shop_code
,i.dept_code
,i.class_code
,i.sub_class_code
,i.supl_code
,i.plu_price
,i.sal_qty
,i.sal_val_in_lc
,i.disc_val_in_lc
,i.tax_val_in_lc
,i.odept
,i.oclass
,i.osubclass
,i.dept_code
,i.class_code
,i.sub_class_code
FROM sales_stage i
WHERE NOT EXISTS
(SELECT NULL
FROM hps_ps_terr_sales_1415 t
WHERE t.item_code = i.item_code
AND t.cons_date = i.cons_date
AND t.shop_code = i.shop_code
AND t.sal_qty = i.sal_qty
AND t.supl_code = i.supl_code);
您在实际代码中处理的内容是否比此更多?