If I stop a long running query, does it rollback?

后端 未结 12 1901
忘了有多久
忘了有多久 2020-12-06 04:23

A query that is used to loop through 17 millions records to remove duplicates has been running now for about 16 hours and I wanted to know if the query is sto

12条回答
  •  [愿得一人]
    2020-12-06 05:05

    Your query is not wrapped in a transaction, so it won't rollback the changes already made by the individual delete statements.

    I specifically tested this myself on my own SQL Server using the following query, and the ApplicationLog table was empty even though I cancelled the query:

    declare @count int
    select @count = 5
    WHILE @count > 0
    BEGIN
      print @count
      delete from applicationlog;
      waitfor time '20:00';
      select @count = @count -1
    END
    

    However your query is likely to take many days or weeks, much longer then 15 hours. Your estimate that you can process 2000 records every 6 seconds is wrong because each iteration in your while loop will take significantly longer with 17 million rows then it does with 2000 rows. So unless your query takes significantly less then a second for 2000 rows, it will take days for all 17 million.

    You should ask a new question on how you can delete duplicate rows efficiently.

提交回复
热议问题