Efficient way to empty tables in ORACLE

0

I have a procedure that empties an auxiliary table and reloads it every time it is invoked. The command used to delete all records is:

DELETE FROM T1;

To my surprise, removing 500.000 records with this command had a cost of approximately 8 segundos .

After several tests I added the clause WHERE in the following way:

DELETE FROM T1 WHERE 1=1;

The latter reduced the time considerably to approximately 3 segundos .

Is there any other, more efficient way to delete all the records in a table?

    
asked by Blasito 22.07.2017 в 17:22
source

2 answers

2

To delete all the records of a table you have some options, each one has its differences, in terms of performance, I think this would be the order of best to worst.

TRUNCATE

  • It is a command DDL
  • Delete all records
  • Triggers are not executed
  • It is not possible to make a ROLLBACK
  • Does not affect the dependencies of the table: indexes, permissions or triggers

DROP TABLE

  • It is a command DDL
  • Deletes all records together with the dependencies of the table: permissions, triggers, indexes, etc.
  • Triggers are not executed
  • It is not possible to make a ROLLBACK
  • Forces us to recreate the table next to its dependencies.

DELETE without WHERE

  • It's a command DML
  • Delete all records
  • It is possible to perform a ROLLBACK of the operation
  • Eventually the associated trigger is executed

More detail of the differences you will be able to find in this article.

PS: I am very impressed by your comment about WHERE 1=1 , I can not find a reason for the query optimizer to make a difference only with that filter. On the other hand the time it takes a query is not a good indicator most of the time, depending on the workload of the server the same operation can vary a lot, it is preferable to see things such as number of readings / writes.

    
answered by 24.07.2017 / 03:36
source
-1

TRUNCATE is much faster than DELETE. Only with TRUNCATE you will delete all what is in the table without any distinction.

If at any time you need to control what to eliminate you will have to use DELETE.

    
answered by 22.07.2017 в 19:59