Delete large amount of data in sql server

后端 未结 7 1370
误落风尘
误落风尘 2021-02-15 15:30

Suppose that I have a table with 10000000 record. What is difference between this two solution?

  1. delete data like :

    DELETE FROM MyTable
             
    
    
            
7条回答
  •  不知归路
    2021-02-15 15:50

    The first has clearly better performance.

    When you specify DELETE [MyTable] it will simply erase everything without doing checks for ID. The second one will waste time and disk operation to locate a respective record each time before deleting it.

    It also gets worse because every time a record disappears from the middle of the table, the engine may want to condense data on disk, thus wasting time and work again.

    Maybe a better idea would be to delete data based on clustered index columns in descending order. Then the table will basically be truncated from the end at every delete operation.

提交回复
热议问题