使用drving_site处理DBLINK数据的无数据的问题
下面的存储过程是用来模拟错误的,通过begin p_test; end;来执行过程,过程执行后显示i的值不正常,多数返回为错误的0,实际上应该是4条记录,如果单独执行p_testb的程序能正常返回为4。
如果取消/*+ driving_site(b)*/的提示也能每次正常返回数据,但是这样执行的时间太长了,也就没有这样用的必要了。
下对于这种需要访问DBLINK的优化,是什么原因导致在存储过程中调用无数据的呢?还有其它好的办法优化调用DBLINK数据吗?
已经找到问题的原因,就是第一块语句执行完成后,第二块语句读取才插入数据的表有延时,在第一块和第块块之间加入延迟就可以解决,由于没有DBMS_LOCK的执行权限,自己写了个延迟过程在2个块之间运行延迟1秒就可以解决了。
create or replace procedure p_test is
i number;
procedure p_testa is
CURSOR cur_order IS
select a.ol_id, a.so_date
from so.order_list@crmqry2 a WHERE a.status_cd not in ('S','D','P','PR')
AND a.ol_type_cd <>6
AND a.channel_id not in (-10001, -10002, -10000, -10006, -10007)
and a.so_date > TRUNC(SYSDATE-1)
and a.ol_id=100582681691
AND a.so_date < TRUNC(SYSDATE);
TYPE t_order IS TABLE OF cur_order%ROWTYPE;
rec_order t_order;
BEGIN
OPEN cur_order;
LOOP
FETCH cur_order BULK COLLECT INTO rec_order LIMIT 40000;
EXIT WHEN rec_order.count =0;
FORALL i IN 1..rec_order.count
INSERT /*+ append */ INTO ygl_zc07_order_b VALUES rec_order(i);
COMMIT;
END LOOP;
CLOSE cur_order;
END p_testa;
--第二块取出退订实例
procedure p_testb is
begin
select /*+ driving_site(b)*/ count(*) into i
from ygl_zc07_order_b a, so.busi_order@crmqry2 b, so.oo_role@crmqry2 c
WHERE a.ol_id=b.ol_id
AND b.bo_id=c.bo_id
and b.ol_id=100582681691
AND c.obj_id IN (2, 9, 379)
AND c.state='DEL'
AND c.offer_role_id IN (select x.offer_role_id from spec.offer_roles@crmqry2 x WHERE x.offer_spec_id IN (SELECT y.offer_spec_id FROM spec.offer_spec@crmqry2 y WHERE y.offer_type_cd=1));
dbms_output.put_line(i);
END p_testb;
begin
execute immediate 'truncate table ygl_zc07_order_b';
execute immediate 'truncate table ygl_zc07_prod_b';
commit;
p_testa;
p_testb;
end p_test;
附:执行存储和单独执行匿名块的结果图
CREATE OR REPLACE PROCEDURE ygl_time(mins NUMBER) IS
t_a DATE := SYSDATE;
t_b DATE;
BEGIN
t_b := t_a + mins / (24 * 60 * 60);
WHILE SYSDATE < t_b LOOP
NULL;
END LOOP;
RETURN;
EXCEPTION
WHEN OTHERS THEN
NULL;
RETURN;
END;