更新查询在oracle中需要很长时间

时间:2016-05-31 16:08:46

标签: sql oracle rdbms

我有一个更新表行的功能,更新完成选择最低创建日期并比较各个开票日期,在将创建日期增加到最高创建日期后,我试图更新发起日期根据这些选择。

CREATE OR REPLACE FUNCTION OriginatedDate_update( total in Number )
  RETURN VARCHAR2
IS
  intialCount Number(10);
  temp Number(10);
  count1 Number(10);
BEGIN
  SELECT COUNT(DISTINCT(p2.created_date)) INTO temp FROM discrepancy_grid_info p2 where billing_date is not null;
  intialCount :=1;
  temp :=temp+1;
  WHILE intialCount < temp loop 
     update (SELECT * FROM discrepancy_grid_info p1 WHERE (intialCount-1) = (SELECT COUNT(DISTINCT(p2.created_date)) FROM discrepancy_grid_info p2 WHERE p2.created_date < p1.created_date ) and billing_date is not null) f 
     set f.ORIGINATED_DATE=(SELECT MAX(p.ORIGINATED_DATE) FROM DISCREPANCY_GRID_INFO p where  p.PRIM_ID=f.PRIM_ID AND p.PLAN_NAME=f.PLAN_NAME and   p.PROCESS_INSTANCE_ID IN (select process_instance_id from audit_process where client_vendor_id in (select id from client_vendor where vendor_id=(select vendor_id from client_vendor where id=(select client_vendor_id from audit_process where process_instance_id=f.process_instance_id)) and client_id=( select client_id  from client_vendor where id=(select client_vendor_id from audit_process where process_instance_id=f.process_instance_id)))) 
            and p.billing_date between add_months(trunc(f.billing_date,'mm'),-1) and last_day(add_months(trunc(f.billing_date,'mm'),-1)) and p.billing_date is not null and p.created_date < f.created_date  group by p.PRIM_ID,p.PLAN_NAME) 
    where f.billing_date is not null;
    DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);
    count1 :=count1+SQL%ROWCOUNT;    
    intialCount :=intialCount+1;
    end loop;
  RETURN count1;
END;

1 个答案:

答案 0 :(得分:3)

只运行 ONE 查询:

 update 
   discrepancy_grid_info p1 
  -- WHERE (intialCount-1) = 
  --  (
  -- SELECT * FROM discrepancy_grid_info p1 
  --     WHERE (intialCount-1) = (
  --         SELECT COUNT(DISTINCT(p2.created_date)) 
  --         FROM discrepancy_grid_info p2 
  --         WHERE p2.created_date < p1.created_date 
  --   ) and */

 set ORIGINATED_DATE=(
   .... rest of your query goes here
)
WHERE p1.billing_date is not null ;

而不是在循环中重复相同的查询数十,数百甚至数千次 也删除循环,不需要。
查询可以进行优化,但这个非常简单的改变应该会给人一种改善。