Sunday, 9 February 2014

Difference Between Delete and Truncate

Delete

Truncate

Delete is an DML (Data Manipulation Language) command Truncate is DDL (Data Definition Language) command
Delete is a row level operation. Only a specific row is effected Truncate is Table level operation, whole table data is effected.
Delete statements gets executed using Row Lock meaning, each row on a table is locked for deletion Truncate Table command locks the complete table and page but not row.
We can filter the row and delete using WHERE clause Truncate cannot use any filters, Whole table is Truncated.
If a table is having Identity Column, then executing Delete statement does not reset identity value If a table is having identity column, then executing Truncate reset identity value.
Delete statement deletes the specific rows which are mentioned in where condition Truncate removes all the data of the table.
Delete activates a trigger as it maintains log for each file Truncate table cannot activate trigger as the operation doesn’t log individual row deletions.
Slower in execution compared to Truncate as it maintains the logs for every record. Faster than Delete statement as it is logged minimum is transaction log.
Delete statements keeps object’s statistics and all allocated space. After a DELETE statement is executed, the table can still contain empty pages.  Drop all object’s statistics and marks like High Water Mark free extents and leave the object really empty with the first extent. zero pages are left in the table 
Delete statement removes rows one at a tme 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 
Delete works at row level thus, row level restrictions apply. Restrictions on Using Truncate are:

  1. Column Values should not be referenced by Foreign Key
  2. Participate in Indexed view
  3.  Are Published by using transactional replication or merge replication.

No comments:

Post a Comment