3 Replies Latest reply: Oct 3, 2011 9:25 PM by 525979 RSS

    need suggestion in deletion for five tables at a time

    577162
      Hi,

      I need some suggestion regarding a deletion and i have the following scenario.

      tab1 contains 100 items.
      for one item tab2..6 tables contain 4000 rows.So the loop will run for each item and will delete 20,000 lines and will do a commit.
      Currently for 5,00,000 deletion it is taking 1 hr.All the tables and indexes are analysied.

      CURSOR C_CHECK_DELETE_IND
      IS
      SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y';

      type p_item IS TABLE OF tab1.item%type;
      act_p_item p_item;

      BEGIN

      OPEN C_CHECK_DELETE_IND;
      LOOP

      FETCH C_CHECK_DELETE_IND bulk collect INTO act_p_item limit 5000;
      FOR i IN 1..act_p_item.count
      LOOP

      DELETE FROM tab2 WHERE item = act_p_item(i);
      DELETE FROM tab3 WHERE item = act_p_item(i);
      DELETE FROM tab4 WHERE item = act_p_item(i);
      DELETE FROM tab5 WHERE item = act_p_item(i);
      DELETE FROM tab6 WHERE item = act_p_item(i);

      COMMIT;
      END IF;
      END LOOP;

      exit when C_CHECK_DELETE_IND%notfound;

      END LOOP;

      Hope i have explained the scenario.Can you please suggest me the right approach.

      Thanks in advance.
        • 1. Re: need suggestion in deletion for five tables at a time
          577162
          Hi All,

          Can anyone please suggest me on the same .I want suggestion regarding the best approach in this situation.
          • 2. Re: need suggestion in deletion for five tables at a time
            319958
            drop the loop
            DELETE FROM tab2 WHERE item in (SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y' );
            DELETE FROM tab3 WHERE item in (SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y' );
            DELETE FROM tab4 WHERE item in (SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y' );
            DELETE FROM tab5 WHERE item in (SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y' );
            DELETE FROM tab6 WHERE item in (SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y' );
            if you can accomplish the task without looping, by all means do so!


            you could also do a bulk delete.
            forall j in indices of act_p_item save exceptions
              DELETE tab2 
               WHERE item = act_p_item(j);
            etc for tab3 to 6

            but unless the query
            SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y' 
            is horrible, I'd stick to the
            DELETE FROM tab2 WHERE item in (SELECT api.item FROM tab1 api WHERE api.delete_item_ind = 'Y' );
            path

            Edited by: tanging on Dec 21, 2009 10:19 AM
            • 3. Re: need suggestion in deletion for five tables at a time
              525979
              I assume the following: 1) tabs 2..6 have a foreign key referencing tab1 with "on delete cascade" option;
              2) tabs 2..6 have index defined on the top of the foreign key field;
              3) if assumption (1) and (2) are valid or you can make them valid then you can execute just two statements:
              delete from tab1 where delete_item_ind = 'Y';
              commit;

              foreign key with on delete cascade option will take care of child records when parent record is being deleted.

              Edited by: Dmitriy S on Oct 3, 2011 7:25 PM