I have been able to get the delete range from about 3 minutes to 3-7 seconds. However I'm not sure if I'm following the ideal route for deleting information. From what I've been reading the ideal way to delete is the following:
- Remove all foreign key restraints.
- Delete the Data.
- Re Add the foreign key restraints.
However this does get a little messy since we also want to delete the information inside of the foreign keys. So I have come to the following solution.
- Remove all foreign key restraints.
- Get the list of all the id's that are being deleted.
- Foreach id in list
- Delete from table1 where ID matches
- Delete from table2 where ID matches
- Delete from table3 where ID matches
- Delete from table4 where ID matches
- Delete from table5 where ID matches
- Re Add the foreign key restraints.
It seems that the time lockdown is doing a foreach loop and deleting from each table. In postgres is there a way to delete from the tables using parallelism? Or should I try deleting with groups of id's instead of single id's?
I am also creating an index and clustering on the ID before the delete statements are ran.
I am open to any suggestions.