I need to delete 10 000 records from a table containing 9 million records. The IDs which are to be deleted will be fetched from a complex query and stored in a Java collection.
I have 3 approaches to implement this
1) Create a prepared statement and add 10000 statements to the batch and execute it.
statement will look like this
Delete from <table_name> where id=?;
2) Write a 'in' query rather than using '=' in a batch. Like
Under this, the 10 000 IDs can be created as comma separated values in Java code and added to the query. Or, 10000 IDs are inserted in to a temporary table and make a select from that table in the sub query.
Delete from <table_name> where id in (<CSV>);
or
Delete from <table_name> where id in (select id from <temp_table>);
There are no constraints and indexes in the table. And I cannot add one, because I'm working on a existing table.
First option is taking ages to complete. It was running for 15hrs and still not completed.