SQL Batched Delete

后端 未结 9 1893
星月不相逢
星月不相逢 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:56

    You can 'nibble' the delete's which also means that you don't cause a massive load on the database. If your t-log backups run every 10 mins, then you should be ok to run this once or twice over the same interval. You can schedule it as a SQL Agent job

    try something like this:

    DECLARE @count int
    SET @count = 10000
    
        DELETE  FROM table1 
        WHERE table1id IN (
            SELECT TOP (@count) tableid
            FROM table1
            WHERE x='y'
        )
    

提交回复
热议问题