SQL Server Efficiently dropping a group of rows with millions and millions of rows

前端 未结 13 541
遇见更好的自我
遇见更好的自我 2021-02-04 12:26

I recently asked this question: MS SQL share identity seed amongst tables (Many people wondered why)

I have the following layout of a table:

Table: Star

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 13:20

    When you say deleting millions of rows is "too intense for SQL server", what do you mean? Do you mean that the log file grows too much during the delete?

    All you should have to do is execute the delete in batches of a fixed size:

    DECLARE @i INT
    SET @i = 1
    
    WHILE @i > 0
    BEGIN
        DELETE TOP 10000 FROM dbo.SuperBigTable
            WHERE CategoryID = 743
        SELECT @i = @@ROWCOUNT
    END
    

    If your database is in full recovery mode, you will have to run frequent transaction log backups during this process so that it can reuse the space in the log. If the database is in simple mode, you shouldn't have to do anything.

    My only other recommendation is to make sure that you have an appropriate index in CategoryId. I might even recommend that this be the clustered index.

提交回复
热议问题