fastest way to run DELETE command

  • I have to purge selected data from my db (around 70 million rows) using the delete command unfortunately. Whats the most efficient way of doing this?

  • a Move data you are keeping into a temp table

    truncate original

    restore temp into original table

    b break the data down into a reasonable count (10k? 100k? ) and loop through the deletes with a moving delete clause

    while yada

    begin

    delete from target where (your criteria) and id between @low and @high

    set low and high

    end

    those would be my starting location.

    Daryl

  • How many of the 70 million rows are you deleting?

    Thanks

  • I have similar problem in my database.

    I need to purge 10 mil record every month. My table size is 233 mil.

    I can move the data to a temp table first. But, I can't truncate my original table (due to foreign key constraint). Unless I drops all my constrains/indices and rebuild it again (I am not sure how much time it will take).

    Eventhough I can delete batch by batch, it will still take me 2 - 3 hours to complete.

    Is there any other suggestion?

    Thanks.

    Leo Leong

  • I followed Steve's instructions up top and I was getting about 2 million deletes an hour (8 way 12 GBs ram on a SAN). Looking back, I probably should have just took the data I needed and dumped it to a temp table and truncated.

     

    Thanks.

  • I have seen INSERT working faster than DELETE so, I will:

    1) copy monster_table to temp_table

    2) script the table to CREATE first then DROP and build a script that will do this:

    DROP TABLE [monster_table]

    CREATE TABLE [monster_table] (with all its constraints and indexes)

    insert into [monster_table]

    select <fields>

    from [temp_table]

    where <just the records you need>

    DROP TABLE [temp_table]

    Try this scenario and compare what takes longer.

  • You could always, create the temp table, transfer the records you want to the temp table, drop the orignal table and then rename the temp table to the orignal table name...

    Thanks

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply