(Oracle) Truncate or Delete which one to use...??

AID: 8891
  • Status: Published

1830 points

  • Bywasimibm
  • TypeTips/Tricks
  • Posted on2011-12-16 at 11:30:05
Truncate is a DDL Command where as Delete is a DML Command.

Both will delete data from table, but what is the difference between these below statements

truncate table <table_name> ??
delete from <table_name> ??

The first command cannot be rolled back, the second command can be (ie rollback can work with delete). But in terms of performance which one is better? Well it depends on the scenario where we use the statements...

For all the temporary tables (intermediate tables) to have a better performance we should always use truncate command. Because, apart from deleting the whole data from the table, truncate command also releases the space(extents) used by the table and reset the high and low water marks of the table (high water mark is just a note to the oracle writer so as to write from the next extent). When you use delete command, high water mark doesnot get reset, hence the storage space doesnt get released.

To test the scenario better, we do need to perform an exercise (for this you need atleast 50 mb of storage).

1) create a test table like this
 
create table truncatedelete
as select * from emp;
                                    
1:
2:

Select allOpen in new window



2) check the number of rows in emp table(recommended rows are to be less than 50)

3) now to just to populate the data in our temp(truncatedelete table) use the below script
/* Formatted on 2011/12/17 00:36 (Formatter Plus v4.8.8) */
SET SERVEROUTPUT ON;

DECLARE
   v_count   NUMBER := 1;
BEGIN
   INSERT INTO truncatedelete
      SELECT *
        FROM scott.emp;

   FOR i IN 1 .. 31
   LOOP
      INSERT INTO truncatedelete
         SELECT *
           FROM truncatedelete;

      COMMIT;
      DBMS_OUTPUT.put_line ('v_count' || v_count);
      v_count := v_count + 1;

      IF MOD (v_count, 10) = 0
      THEN
         DELETE FROM truncatedelete
               WHERE ROWNUM < 1000;

         DBMS_OUTPUT.put_line ('Deleted 9 rows');
         COMMIT;
      END IF;

      IF v_count > 15
      THEN
         EXIT;
         DELETE FROM truncatedelete
               WHERE ROWNUM < 10000;
                  INSERT INTO truncatedelete
         SELECT *
           FROM truncatedelete;

      END IF;
   END LOOP;
END;
                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:

Select allOpen in new window



4) now that the data has  been populated in our table check out the size of the table using the below query
 
select sum(bytes/1024/1024) from dba_extents where segment_name='TRUNCATEDELETE';
                                    
1:

Select allOpen in new window


   You need to have access to the dba_views (use system schema for this purpose)
   In my db the size is shown as 40 mb

5) after this execute the below query
 
delete from truncatedelete;
                                    
1:

Select allOpen in new window



6) and check the size using below query
 
select sum(bytes/1024/1024) from dba_extents where segment_name='TRUNCATEDELETE';
                                    
1:

Select allOpen in new window



7) you will not see any difference in size , even though there is no data in the table - hence you can conclude that delete does not release the storage space..

8) and now execute the below query
truncate table truncatedelete;
                                    
1:

Select allOpen in new window



   now check the size again using the above query from dba_extents
 
select sum(bytes/1024/1024) from dba_extents where segment_name='TRUNCATEDELETE';
                                    
1:

Select allOpen in new window



   you will see that table unused space has shrunk and that space has been released.

Conclusion:

So in the case of temporary/interface tables where data needs are only for a temporary basis, and you need to keep the table, it would be better to use truncate rather than delete. Truncate also saves the effort of having to drop and recreate - but that is a different story.

Considerations:

Few things to be considered while using both the statements are, while using delete and truncate if there are any referential constraints, the operation gets aborted because of the child/foreign records.

Also in case of delete, as the delete is a dml command, until and unless a commit happens all the transactional information gets recorded in database buffer caches and redo log space ie., only when a commit happens the information gets written in archive logs. Hence Truncate is faster than Delete. As Truncate statement does not need a commit as it is a DDL Command.

Also incase of delete, if there are any triggers enabled in the table(row level or statement level) a delete statement will fire the trigger . Where in a truncate statement the trigger doesn't get fired.

Further information about these statements can be read from the below sites:

http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10007.htm

http://docs.oracle.com/cd/B28359_01/server.111/b28310/general003.htm

Incase some one is unable to find the employee table in their database schemas. you can get it created from the scripts stored in the server at the following location

 $ORACLE_HOME/sqlplus/demo/demobld.sql

Depending upon the scenario and requirement, the different statements can make a big difference and should always remove data with caution - always check twice before you do - especially with the truncate statement (remember - no rollback - have to go to backup).
Asked On
2011-12-16 at 11:30:05ID8891
Tags

Oracle Truncate or Delete

Topic

Oracle Database

Views
1182

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Oracle Database Experts

  1. slightwv

    811,605

    Sage

    12,800 points yesterday

    Profile
    Rank: Genius
  2. sdstuber

    578,296

    Sage

    3,540 points yesterday

    Profile
    Rank: Genius
  3. wasimibm

    159,939

    Guru

    2,100 points yesterday

    Profile
    Rank: Guru
  4. tosse

    83,962

    Master

    1,510 points yesterday

    Profile
    Rank: Master
  5. dvz

    77,992

    Master

    900 points yesterday

    Profile
    Rank: Sage
  6. flow01

    70,666

    Master

    0 points yesterday

    Profile
    Rank: Sage
  7. OP_Zaharin

    66,395

    Master

    0 points yesterday

    Profile
    Rank: Sage
  8. Geert_Gruwez

    66,198

    Master

    800 points yesterday

    Profile
    Rank: Genius
  9. awking00

    63,850

    Master

    0 points yesterday

    Profile
    Rank: Genius
  10. MikeOM_DBA

    55,954

    Master

    10 points yesterday

    Profile
    Rank: Genius
  11. johnsone

    46,104

    0 points yesterday

    Profile
    Rank: Genius
  12. schwertner

    43,375

    0 points yesterday

    Profile
    Rank: Genius
  13. slobaray

    42,921

    20 points yesterday

    Profile
    Rank: Master
  14. Bajwa

    35,334

    1,000 points yesterday

    Profile
  15. gatorvip

    33,868

    0 points yesterday

    Profile
    Rank: Sage
  16. praveencpk

    33,355

    0 points yesterday

    Profile
    Rank: Master
  17. sujith80

    30,896

    0 points yesterday

    Profile
    Rank: Genius
  18. sventhan

    30,548

    0 points yesterday

    Profile
    Rank: Sage
  19. markgeer

    29,292

    0 points yesterday

    Profile
    Rank: Genius
  20. paquicuba

    26,860

    0 points yesterday

    Profile
    Rank: Genius
  21. HainKurt

    24,922

    0 points yesterday

    Profile
    Rank: Genius
  22. Milleniumaire

    20,300

    0 points yesterday

    Profile
    Rank: Sage
  23. ytarkan

    17,352

    0 points yesterday

    Profile
  24. mwvisa1

    15,600

    0 points yesterday

    Profile
    Rank: Genius
  25. angelIII

    14,509

    0 points yesterday

    Profile
    Rank: Elite

Hall Of Fame