views:

68

answers:

2
+1  Q: 

delete on batch

I have a db table that has > 14M rows.

If I attempt to perform:

 delete from table

The connection hangs out.

How can I delete all the rows, in "batchs" ( I guess ) so they are deleted eventually?

+2  A: 

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.

paxdiablo
Nope.. what it does? Does it delete everything without a transaction or something?
OscarRyz
Well ehem I already knew it, you know, I just wanted to do a "jeopardy type of question" from the FAQ *It's also perfectly fine to ask and answer your own question, but pretend you're on Jeopardy: phrase it in the form of a question.* je je je Thank paxdiablo
OscarRyz
Huh? Are you talking to yourself?
ceejayoz
@Oscar, you are indeed right that these sorts of questions are okay. @ceejayoz, that sort of discourse normally comes about when one party to a conversation deletes their comments, making what's left seem disjointed. It may be that someone had a comment in between Oscar's two along the lines of "Oscar, you seem knowledgeable about SQL in all your other questions - surely you knew this" and then deleted it. That would make the current state understandable.
paxdiablo
+2  A: 

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
brianegge