Results 1 to 4 of 4

Thread: Truncate or Rebuild Tables?

Hybrid View

  1. #1
    Tommy Guest

    Truncate or Rebuild Tables?

    I frequently have tables that I want to empty and refill. I thought I read somewhere that trucating was a bad idea, example:

    Truncate table tblFruits

    A bad idea because of something to do with the log? I'm a beginner so I don't understand transaction processing very well, or logs.

    Is it better to delete the table and rebuild it in code before you add the values?

  2. #2
    Sundar Guest

    Truncate or Rebuild Tables? (reply)

    We truncate a couple of our tables at the end of each night and have never experienced problems so far. Our transaction logs haven't grown to a huge size but then we do dump the transaction log every night as well.


    ------------
    Tommy at 3/8/01 5:32:57 PM

    I frequently have tables that I want to empty and refill. I thought I read somewhere that trucating was a bad idea, example:

    Truncate table tblFruits

    A bad idea because of something to do with the log? I'm a beginner so I don't understand transaction processing very well, or logs.

    Is it better to delete the table and rebuild it in code before you add the values?

  3. #3
    Join Date
    Aug 2022
    Location
    Krakow, Poland
    Posts
    1
    some differences between truncate and delete sql commands:


    sql delete command:


    a) Deletes all or some records from the table, you can limit the records to be deleted by using the WHERE clause
    b) Does not free the space occupied by the data in the table (in the TABLESPACE - on the disk)
    c) Does not reset the SEQUENCE value assigned to the table
    d) DELETE works much slower than TRUNCATE
    e) DELETE can be applied to tables and tables in a cluster (can be Oracle specific)
    f) Oracle - For DELETE, you can use the GRANT command
    g) The DELETE operation does not make unusable indexes usable again
    h) DELETE requires a shared table lock
    i) Triggers fire
    j) DELETE can be used in the case of: database link
    k) DELETE returns the number of records deleted
    l) Transaction log - for each deleted record (deletes rows one at a time and records an entry in the transaction log for each deleted row) - slower execution than TRUNCATE. The table may still contain blank pages after executing the DELETE statement. DELETE needs to read records, check constraints, update block, update indexes, and generate redo / undo. All of this takes time, hence it takes time much longer than with TRUNCATE
    m) DELETE uses more transaction space than the TRUNCATE statement
    n) DELETE can be used with indexed views
    o) It is a DML (Data Manipulation Language) command, therefore the following commands are used for this command: COMMIT and ROLLBACK
    p) You can undo the operation of removing records by using the ROLLBACK command



    sql truncate command:


    a) Deletes all records from the table, records cannot be limited to deletion. For Oracle, when the table is split per partition, individual partitions can be truncated (TRUNCATE) in isolation, making it possible to partially remove all data from the table
    b) Frees up the space occupied by the data in the table (in the TABLESPACE - on disk). For Oracle - if you use the REUSE STORAGE clause, the data segments will not be rolled back, i.e. you will keep space from the deleted rows allocated to the table, which can be a bit more efficient if the table is to be reloaded with data. The high mark will be reset
    c) Resets the SEQUENCE value assigned to the table to zero. However, the following options can be used: RESTART IDENTITY or CONTINUE IDENTITY
    d) TRUNCATE works much faster than DELETE
    e) TRUNCATE only affects tables or the entire cluster (may be Oracle specific)
    f) Oracle - TRUNCATE cannot be granted (GRANT) without using DROP ANY TABLE
    g) The TRUNCATE operation makes unusable indexes usable again
    h) TRUNCATE requires an exclusive table lock, therefore, turning off exclusive table lock is a way to prevent TRUNCATE operation on the table
    i) DML triggers do not fire after executing TRUNCATE (so be very careful in this case, you should not use TRUNCATE, if a delete trigger is defined in the table to perform an automatic table cleanup or a logon action after row deletion). On Oracle, DDL triggers are fired
    j) Oracle - TRUNCATE cannot be used in the case of: database link
    k) TRUNCATE does not return the number of records deleted
    l) Transaction log - one log indicating page deallocation (removes data, releasing allocation of data pages used for storing table data and writes only page deallocations to the transaction log) - faster execution than DELETE. TRUNCATE only needs to adjust the pointer in the database to the table (High Water Mark) and the data is immediately deleted, therefore it uses less system resources and transaction logs
    m) TRUNCATE takes up less transaction space than the DELETE statement
    n) TRUNCATE cannot be used with indexed views
    o) It is a DDL (Data Definition Language) command, therefore commands such as COMMIT and ROLLBACK do not apply to this command (the exceptions here are PostgreSQL and MSSQL, whose implementation of the TRUNCATE command allows the command to be used in a transaction)
    p) You cannot undo the operation of deleting records, it occurs automatically and is irreversible (except for the above exceptions - provided, however, that the operation is included in the TRANSACTION block and the session is not closed). In case of Oracle - Includes two implicit commits, one before and one after the statement is executed. Therefore, the command cannot be withdrawn while a runtime error will result in commit anyway

  4. #4
    Join Date
    Feb 2014
    Location
    Riviera Beach, Maryland, USA
    Posts
    84

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •