Skip to Main Content
  • Questions
  • Taking nearly four days to delete the records

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, bb.

Asked: July 03, 2019 - 5:46 pm UTC

Last updated: July 05, 2019 - 10:22 am UTC

Version: 12.2

Viewed 1000+ times

You Asked

Following Query is taking nearly four days to delete the records from C_HDR_PARENT_TB table, we need to tune this query.

Can you please suggest and modify this

declare
  rowcount         number := 0;
  type rowid_typ is
    table of rowid;
  rowid_rec        rowid_typ;
  var_aud_add_id   varchar2 (40) := 'B-IF-OLTP-PURG';
  cursor cur_c_hdr_parent_tb is
  select rowid
  from c_hdr_parent_tb a1
  where a1.c_tcn_num in (
    select b1.c_tcn_num
    from c_arcvl_cntl_oltp_tb b1
    where c_incld_excld_ind = 'I'
  );

begin
  open cur_c_hdr_parent_tb;
  loop
    fetch cur_c_hdr_parent_tb bulk collect into rowid_rec limit 10000;
    forall i in rowid_rec.first..rowid_rec.last
      delete c_hdr_parent_tb
      where rowid = rowid_rec (i);

    rowcount := rowcount + sql%rowcount;
    commit;
    exit when cur_c_hdr_parent_tb%notfound;

  end loop;

  insert into c_iface_purge_cnt_oltp_tb values (
    'C_HDR_PARENT_TB',
    nvl (rowcount,0),
    var_aud_add_id,
    systimestamp
  );

  commit;
end;
/

and Chris said...

Step 1

Trace your process to see what it's doing:

exec dbms_monitor.session_trace_enable ( <sid>, <serial#>, true, true );


Step 2

Get the trace file this generates. Format it with tkprof or similar:

tkprof <trace file name> <output file>


Step 3

Post your findings back to us.

For more on this process read:

https://blogs.oracle.com/sql/how-to-create-an-execution-plan#tkprof

Without this analysis of what the process is doing, anything we suggest is just a guess.

Step 1a

While you're collecting this information, read one of the many questions we already have about making deletes faster:

https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:5033906925164
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2345591157689
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:30909275913631
https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:9531646000346504729

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Regarding the delete..

A reader, July 05, 2019 - 6:45 am UTC

Any reason a direct delete cannot be run?
Why are you deleting based on rowid? Although may not be a performance bottlenck but just curious.
Why are you using SQL%ROWCOUNT instead of CURSOR_NAME%ROWCOUT as the final output?

Cheers.
Chris Saxon
July 05, 2019 - 10:22 am UTC

Indeed, good questions.

Lot of things

Gh, July 06, 2019 - 7:49 am UTC

Although this could be done in more efficient sql I would ask to check relationship btw tables eg fk pk and indexes on fks.

A reader, July 24, 2019 - 5:44 pm UTC


More to Explore

Performance

Get all the information about database performance in the Database Performance guide.