emptying large tables

Discussion in 'Databases' started by Jamie18, Sep 24, 2009.

  1. #1
    is there a better way to empty a large table than

    DELETE FROM atable;
    COMMIT;
    Code (markup):
    ??

    i've been working on migration scripts and so i've been populating tables, testing their accuracy, emptying them, making any necessary changes and rerunning my scripts..

    one of the tables has 2000000+ records.. and when i use the delete from method it seems to take forever.. is there a better way?

    do i need to empty it in chunks? like
    delete from atable where id between 0-100;
    commit;
    delete from atable where id between 101-200;
    commit;
    etc.
    Code (markup):
    ??

    my problem is coming from mssql i never had to do commit's.. now working in oracle i don't know all the rules surrounding them.. like memory management
     
    Jamie18, Sep 24, 2009 IP
  2. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    post script..

    the reason i've got to ask this is because it's beginning to take up to an hour to empty out all my tables..
     
    Jamie18, Sep 24, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Save the table structure in a sql file somewhere. DROP the table and recreate it. Should only take a few seconds.
     
    jestep, Sep 24, 2009 IP
  4. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    TRUNCATE TABLE table_name
    Code (markup):
    The above code will delete all entries from the table - and leave it's structure intact.

    http://beginner-sql-tutorial.com/sql-delete-statement.htm
     
    ThomasTwen, Sep 26, 2009 IP
  5. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thank the lord.. i haven't tried it out yet to see the time difference but i'm certainly optimistic that i can finally pull off the deletes in a reasonable amount of time
     
    Jamie18, Oct 13, 2009 IP
  6. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #6
    Definitely use Truncate (Truncate table table_name).

    DELETEs get written to log file butr truncate doesn't.

    Safer than dropping and recreating because at some time in the future, somebody (maybe even you) is going to change the structure of the table (maybe adding an extra column) and won't know (or will forget) to change the table structure code that you've got saved.
     
    rayqsl, Oct 13, 2009 IP
  7. phones2me

    phones2me Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    sounds like you want to drop and then recreate the table - easy
     
    phones2me, Oct 14, 2009 IP
  8. urstop

    urstop Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Use Truncate table, as it does not log infomation on a record level, it will be faster than the normal delete statement.
     
    urstop, Oct 15, 2009 IP
  9. Brandon_R

    Brandon_R Peon

    Messages:
    330
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hello,

    Use TRUNCATE as DELETE was made for smaller queries such as removing a 'row' with a 'WHERE' condition.
     
    Brandon_R, Oct 16, 2009 IP
  10. liverpools

    liverpools Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Export Db structure as sql file and remove it and rebuild your table or import sql file again.
     
    liverpools, Oct 16, 2009 IP
  11. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    i've been trying to use the truncate command to no avail..

    truncate keeps claiming their are foreign key records in another table.. however it will allow me to delete those records..

    say i have these tables
    TableA
    col1 number pk
    col2 string

    TableB
    col3 number fk references TableA.col1
    col4 char

    I should be able to run a script that would first
    Truncate table TableB

    And second
    Truncate table TableA

    shouldn't i?
     
    Jamie18, Oct 22, 2009 IP