Delete large amount of data in sql server

后端 未结 7 1380
误落风尘
误落风尘 2021-02-15 15:30

Suppose that I have a table with 10000000 record. What is difference between this two solution?

  1. delete data like :

    DELETE FROM MyTable
             
    
    
            
7条回答
  •  渐次进展
    2021-02-15 15:59

    Found this post on Microsoft TechNet.

    Basically, it recommends:

    1. By using SELECT INTO, copy the data that you want to KEEP to an intermediate table;
    2. Truncate the source table;
    3. Copy back with INSERT INTO from intermediate table, the data to the source table;

    ..

    BEGIN TRANSACTION
    
    SELECT  *
       INTO    dbo.bigtable_intermediate
       FROM    dbo.bigtable
       WHERE   Id % 2 = 0;
    
       TRUNCATE TABLE dbo.bigtable;  
    
       SET IDENTITY_INSERT dbo.bigTable ON;
       INSERT INTO dbo.bigtable WITH (TABLOCK) (Id, c1, c2, c3)
       SELECT Id, c1, c2, c3 FROM dbo.bigtable_intermediate ORDER BY Id;
       SET IDENTITY_INSERT dbo.bigtable OFF;
    ROLLBACK TRANSACTION
    

提交回复
热议问题