To delete existing rows or create a proper new table, and old old ones faster, depends on many factors. 11 million rows is a lot, but it is only 0.5% of the total number of rows in the table. It is possible that recreation and rollback can be much slower than deletion, depending on how many indexes exist in the source table, and also where the rows to be deleted exist on the data pages.
The question then becomes whether the source table is live or not. If inserts and updates occur during this cleanup, the copy and rollback will not work without enough additional code to synchronize the table after the fact.
Finally, why is this operation necessary to be "quick"? Is it because the system must be shut down during the process? You can write a procedure that removes the tricks while the system is live, but does not affect the rest of the system in terms of destruction. We solved this problem in the past by first writing a query that collects the primary keys of the rows that need to be deleted in the second table, for example:
INSERT INTO RowsToDeleteTable SELECT PKColumn FROM SourceTable WHERE <conditions used to find rows to remove> CREATE UNIQUE INDEX PK_RowsToDelete ON RowsToDeleteTable (PKColumn);
Then we have a PL / SQL block that either iterates over the lines in the cursor like this:
BEGIN FOR theRow IN (SELECT PKColumn FROM RowsToDeleteTable ORDER BY 1) LOOP <delete source table for theRow.PKColumn) <optionally wait a bit> commit; END LOOP; END;
or does something like this:
BEGIN FOR theRow IN (SELECT MIN(PKColumn) FROM RowsToDeleteTable ) LOOP <delete source table for theRow.PKColumn) <optionally wait a bit> DELETE RowsToDeleteTable WHERE PKColumn = theRow.PKColumn; commit; END LOOP; END;
The loop and SELECT MAX are obviously less efficient, but it has the advantage of allowing you to monitor the progress of the delete operation. We put some waiting code in a loop so that we can control how much the extraction operation is going on.
The initial creation of a RowsToDeleteTable is very fast, and you have the advantage of allowing the process to accept as many as you want. In this case, the “holes” remaining in extents through deletions will not be too bad, since you delete such a small percentage of the total amount of data.
Steve broberg
source share