delete rows

  • hi

    i need to move data to another table and then remove from old one ,which i needs to delete 30 millions of rows,

    i know its taking time but any idea i can do it faster like in a batch

  • You should do it in several batches. If you give us the code you're using, we might be able to help more. A simple example could be this.

    SELECT 1

    WHILE @@ROWCOUNT > 0

    BEGIN

    DELETE TOP (1000000)

    FROM MyTable

    WHERE MyCondition = 'Delete this row'

    END

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • If you can, instead just truncate the old table, then re-insert only the kept rows.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • copy the table to another table and then truncate the old table

  • Before modifying data it is always a good idea to create a temp table as a backup. Once you delete it all, there's no going back.

  • Hi Riya, its better if you keep of your old data in some temp table and then insert the new data....

    first insert the old data into a temp table then truncate old table and insert new ones into that....

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • riya_dave (7/16/2013)


    hi

    i need to move data to another table and then remove from old one ,which i needs to delete 30 millions of rows,

    i know its taking time but any idea i can do it faster like in a batch

    After deleting 30 million rows from the old table, how many rows are left?

    How similar are the two tables?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • total i have 60 millions rows from there i need to insert 30 millions to some another table.

    once it is insert i need to delete it from original table

  • riya_dave (7/17/2013)


    total i have 60 millions rows from there i need to insert 30 millions to some another table.

    once it is insert i need to delete it from original table

    You can do the delete/insert in the same statement like so:

    DELETE TOP (100000) FROM LiveTable

    OUTPUT deleted.* INTO ArchiveTable

    WHERE ...

    --batched up so it doesn't choke up your resources.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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