Delete SQL Server 2005 records without logging

前端 未结 7 1056
北恋
北恋 2020-12-28 08:15

How to delete records from SQL Server 2005 tables without logging them into transaction logs.

I do not wish to log because once deleted, those records will never be

7条回答
  •  时光说笑
    2020-12-28 08:36

    Forget the transaction log as a source of the speed or anything other than something used internally by the DB to keep consistency. Instead, you should consider finding a way to do the delete in batches. Instead of a single delete statement, and if you had an integer PK on the table, try deleting ranges of values and do this in a loop. So something like

    Declare @RecordsLeft int
    Declare @StartRange int
    Declare @EndRange int
    Declare @BatchSize int
    
    Set @BatchSize = 10000
    Set @RecordsLeft = ( Select Count(*) From ... )
    Set @StartRange = 0
    Set @EndRange = @StartRange + @BatchSize
    
    While @RecordsLeft > 0
    Begin
        Delete ...
        Where ...
          And PK Between @StartRange And @EndRange
    
        Set @RecordsLeft = ( Select Count(*) From ... )
        Set @StartRange = @EndRange + 1
        Set @EndRange = @StartRange + @BatchSize
    End
    

提交回复
热议问题