Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Code Optimization using cursor loop, insert into and commit for each row

3918247Mar 19 2019 — edited Mar 22 2019

Hi,

I writed something like this below (it works), but in my case for 1,5 milions rows it is not so effective as I need (it will run maybe 2 days) I saw something like BULK COLLECT FETCH FORALL etc. but I am not managing to rewrite my code to this without errors. Can you help me with it? Or do you have some better idea for optimization my simple code?

Thank you

--It is my code for rewriting
  DECLARE
  cnt NUMBER;
  d_min NUMBER;
  d_max NUMBER;
  i NUMBER := 0;
  CURSOR ts_metatata_cur IS select * from (select rownum as rn, id_profile from ts_metadata where typ=7 and per=3600 order by id_profile) where rn between 1 and 100000;
  BEGIN
  for metadata_rec in ts_metatata_cur
  LOOP
  XTS.GET_PROFILE_AGGR(metadata_rec.id_profile, cnt, d_min, d_max); --procedure with one IN parameter and three OUT parameter cnt, d_min, d_max
  Execute immediate 'insert into TMP_PROFILES_OVERVIEW (id_profile, cnt, d_min, d_max) values (' || metadata_rec.id_profile || ', ' || cnt || ', ' || d_min || ', ' || d_max ||')';
  i := i+1;
  if (i > 10000) then
  commit;
  i := 0;
  end if;
  END LOOP;
  commit;
  END;

If it is necessary I give here procedure which I call:

--this is procedure, which I call in my script
CREATE OR REPLACE PROCEDURE XTS.GET_PROFILE_AGGR(id_prof IN NUMBER, cnt OUT NUMBER, d_min OUT NUMBER, d_max OUT NUMBER)
AS
  res varchar2(61);
BEGIN
  select cluster_table_name into res FROM XTS.TIME_SERIES TS where TS.id=id_prof;
  Execute immediate 'select nvl(count(*),0), nvl(min(time),0), nvl(max(time),0) from '|| res || ' where time_series_id=' || id_prof || ' ' into cnt, d_min, d_max;

EXCEPTION

  when others then
  null;

END;

This post has been answered by Cookiemonster76 on Mar 19 2019
Jump to Answer
Comments
Post Details
Added on Mar 19 2019
15 comments
5,264 views