Delete on batch
I have a开发者_StackOverflow中文版 database table that has >14 million rows.
If I attempt to perform:
delete from table
The connection hangs out.
How can I delete all the rows, in "batches" so they are deleted eventually?
Try out:
truncate table tblname
This has the advantage of not being rollback-able so will not have to log its actions, and also does not fire triggers. That makes it substantially faster than the equivalent delete from tblname
.
Otherwise, you can work out subsets to delete, based on your table data. For example, if there's a field (hopefully indexed) containing last names:
delete from tblname where lastname like 'A%'
Note that this is just an example. You can also use things like
- salary (<20K, 20K-40K, 40K-60K, ...).
- SSN.
- current balance.
and so on. What you choose depends entirely on the table you're trying to delete.
I prefer the truncate
command myself due to its simplicity.
deletes are logged operation which can be rolled back, hence they can be quite slow. If you can truncate or drop and recreate the table, that's your best bet. Alternatively, you can execute this sql.
BEGIN
loop
delete from table where rownum < 500
exit when SQL%rowcount < 499
end loop;
END
精彩评论