SQL Server DELETE is slower with indexes

后端 未结 5 1668
忘掉有多难
忘掉有多难 2021-01-02 20:21

I have an SQL Server 2005 database, and I tried putting indexes on the appropriate fields in order to speed up the DELETE of records from a table with millions

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 21:03

    Try something like this to avoid bulk delete (and thereby avoid log file growth)

    declare @continue bit = 1
    
    -- delete all ids not between starting and ending ids
    while @continue = 1
    begin
    
        set @continue = 0
    
        delete top (10000) u
        from     u WITH (READPAST)
        where   
    
        if @@ROWCOUNT > 0
            set @continue = 1 
    
    end
    

提交回复
热议问题