deleting huge records From Table

  • I was considering locking/blocking and availability of the table

  • Paul White NZ (11/22/2010)


    Since this is in the SQL Server 2008 forum, you could also use bulk-logged INSERT...SELECT then ALTER TABLE...SWITCH.

    I thought SWITCH only worked in Enterprise Edition?

    If you have to reindex a large table then make sure you have enough space in TEMPDB.

    If you can't use switch purging then try something like

    SET ROWCOUNT 50000

    WHILE 1=1

    BEGIN

    DELETE FROM yourschema.yourtable WHERE <your criteria>

    OPTION (MAXDOP=1)

    IF @@ROWCOUNT = 0

    BREAK

    END

    SET ROWCOUNT 0

    If you are doing a significant delete then I'd plan to do it after a full back up and in a quiet time on the server if such a period exists.

    If there is any danger that you might need to get the records back in a hurry then I'd BCP them out and store them with the CREATE TABLE script so you have the archive and what the archive actually was at the time it was taken.

    If you take the approach suggested by Jeff then it may be faster but it does mean that you are temporarily going to need enough space for your original records AND the new copy prior to the purge.

    It sounds as if you need to plan a regular purge/archive job for this table so you can automate this for future use.

  • David.Poole (11/23/2010)


    I thought SWITCH only worked in Enterprise Edition?

    One can only create partitioned tables on Enterprise, it's true, but ALTER TABLE...SWITCH works regardless of any partitioning.

    All tables are partitioned (they have one partition at least).

    ALTER TABLE...SWITCH is a very fast way to switch a whole table to another.

  • Paul White NZ (11/23/2010)


    David.Poole (11/23/2010)


    I thought SWITCH only worked in Enterprise Edition?

    One can only create partitioned tables on Enterprise, it's true, but ALTER TABLE...SWITCH works regardless of any partitioning.

    All tables are partitioned (they have one partition at least).

    ALTER TABLE...SWITCH is a very fast way to switch a whole table to another.

    I swear I saw somewhere where you used SWITCH to move rows you wanted to delete from a larger table to a non-partioned table. If you have a link for that, I sure would appreciate it. Thanks, Paul.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Ah... never mind. The thing I was looking for is in the link you previously posted and it doesn't do quite what I thought. What I thought was that there was a way to use SWITCH to move only the rows we wanted to delete to a non-paritioned table and then drop that table.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

Viewing 5 posts - 16 through 19 (of 19 total)

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