I am facing issue deleting set of records from a table named links_data because of huge set of data to be deleted and the Oracle DB ran out of temp space.
For the same with little knowledge of mine on Stored Procedure I wrote the below code.
But the Stored Procedure looks to be running slow as well.
The scenario is that I need to delete the rows from links_data table where the nm_folder column in links_data table has foreign key relation with link_folders table. nm_folder in link_folders table is the primary key and I intend to delete 10000 records at a time and commit and then again delete 10000 records and commit.
Kindly anyone please help me optimize this query. Thanks & Regards,
Declare
Type ty_link_floder is table of number;
tble_id_folder ty_link_floder;
Cursor c_data is
select id_folder from link_folders where nm_folder='User Hotlist';
Begin
OPEN c_data;
LOOP
FETCH c_data
BULK COLLECT INTO tble_id_folder LIMIT 10000;
EXIT WHEN tble_id_folder.count = 0;
FORALL i IN tble_id_folder.first .. tble_id_folder.last
DELETE FROM links_data
WHERE id_folder = tble_id_folder(i);
COMMIT;
-- Process contents of collection here.
DBMS_OUTPUT.put_line(tble_id_folder.count || ' rows deleted from links_data table so far');
tble_id_folder.delete;
END LOOP;
CLOSE c_data;
Exception
When others Then
Commit;
Raise;
End;
I’d try it first in one SQL statement:
If you run out of temp space and the DBAs cannot set up additional temp space, the best way is to recreate the table without the rows that should be deleted:
After that, you need to rebuild all indexes and constraints on
links_data_tmpthat have been defined onlinks_data. Then you can swap the temporary table with the real one:Don’t forget to reapply all the privileges, too.