Truncate table

Discussion in 'Databases' started by QiSoftware, Mar 29, 2011.

  1. #1
    I am thinking of using a TRUNCATE TABLE SQL command to quickly clear a database table-- daily. I have never used this command and I plan on executing it within a Java Servlet.

    I want to use this command versus -- delete table-- which would require that I also recreate the table on the fly [daily]-- because I think it will be less invasive.

    Anyone have any experience with this?

    Q...
     
    QiSoftware, Mar 29, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Yes. It sounds exactly like what you need.

    However--why do you want to clear out the table? Why not just put a date field on it and then make sure the query you use to access your data uses that field in its WHERE clause?
     
    plog, Mar 30, 2011 IP
  3. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #3
    Because I like keeping my web host happy and in this case-- I do not need the data. It is of a daily nature and no use after the day it is provided for. Also-- it should make the database faster-- when useless data is cleared out.

    Q...
     
    QiSoftware, Mar 30, 2011 IP
  4. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #4
    TRUNCATE TABLE is good - it's fast and doesn't write any data to the log file.

    DELETE FROM TABLE is OK - but it writes to the log file and can take a long time.

    DROP TABLE is OK if you can afford not to have a table in existence whilst it is being dropped and recreated.
     
    rayqsl, Mar 30, 2011 IP
  5. randheer

    randheer Greenhorn

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Truncate table directly de allocates the data from page while delete work on row basis. Because of this in truncate data entry doesn’t exist at log label while in delete data present at log label.
    You can use truncate if you don’t want to rollback your data at any condition.

    Didn’t get much clarity on your last statement that you need to recreate the table every time.

    Truncate also doesn’t delete the schema of the table. It will be there only data will truncate.
     
    randheer, Apr 4, 2011 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Any time you need to flush everything from a table but still keep the table, use truncate. If you need to keep the primary key count, use delete.
     
    jestep, Apr 5, 2011 IP