开发者

What is Difference between truncate table A and Delete from A [duplicate]

开发者 https://www.devze.com 2023-01-11 09:14 出处:网络
This question already has answers here: 开发者_JAVA技巧 Closed 12 years ago. Possible Duplicates: SQL Server - bulk delete (truncate vs delete)
This question already has answers here: 开发者_JAVA技巧 Closed 12 years ago.

Possible Duplicates:

SQL Server - bulk delete (truncate vs delete)

What’s the difference between TRUNCATE and DELETE in SQL

What is Difference between truncate table A and Delete from A? which one better to use for deleting the records from table.


This answer can be easily found in Microsoft documentation for TRUNCATE table:

Compared to the DELETE statement, TRUNCATE TABLE has the following advantages:

  • Less transaction log space is used. The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log.
  • Fewer locks are typically used. When the DELETE statement is executed using a row lock, each row in the table is locked for deletion. TRUNCATE TABLE always locks the table and page but not each row.
  • Without exception, zero pages are left in the table. After a DELETE statement is executed, the table can still contain empty pages. For example, empty pages in a heap cannot be deallocated without at least an exclusive (LCK_M_X) table lock. If the delete operation does not use a table lock, the table (heap) will contain many empty pages. For indexes, the delete operation can leave empty pages behind, although these pages will be deallocated quickly by a background cleanup process.
0

精彩评论

暂无评论...
验证码 换一张
取 消