SQL Batched Delete

后端 未结 9 1905
星月不相逢
星月不相逢 2021-02-20 04:29

I have a table in SQL Server 2005 which has approx 4 billion rows in it. I need to delete approximately 2 billion of these rows. If I try and do it in a single transaction, th

9条回答
  •  离开以前
    2021-02-20 04:51

    Well, if you were using SQL Server Partitioning, say based on the date column, you would have possibly switched out the partitions that are no longer required. A consideration for a future implementation perhaps.

    I think the best option may be as you say, to delete the data in smaller batches, rather than in one hit, so as to avoid any potential blocking issues.

    You could also consider the following method:

    1. Copy the data to keep into a temporary table
    2. Truncate the original table to purge all data
    3. Move everything from the temporary table back into the original table

    Your indexes would also be rebuilt as the data was added back to the original table.

提交回复
热议问题