• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • Last Modified:

Delete and truncate comparison

Please add your take on the following statements:

1. Some drop a table and recreate it because it is faster than deleting it.

2. Delete, deletes the data based on where clause from data pages (?) thus lowers the performance because disk fragmentation that occurs during the delete operation. SQL Server rebuilds the indexes and defrags disk later but not right-away.

3. Truncate deletes data the data and deals with data fragmentation right away (slows SQL Server engine at that time)
0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 2
4 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:

1.

Correct, although TRUNCATE will be faster than DELETE ALL, and you may blow away privs on an object by doing a DROP and CREATE vs. keeping it alive with a TRUNCATE.

2.

Decrease speed as it has to process a WHERE clause, yes, everything else I'll defer to the DBA experts here.

3.

TRUNCATE blows away all pages, plus it's only minimally logged (if it wasn't it wouldn't be able to particiate in a transaction), so it's much faster than DELETE.
0
 
Scott PletcherSenior DBACommented:
1.  Not logical.  It would be faster to just truncate the table.

2.  Delete would not have a WHERE clause if you're trying to mimic a truncate.  The big difference is that Delete logs each row, while Truncate only logs each page.

3.  Truncate does the remove the data right away and eliminate fragmentation.  But it does not "slow" the SQL engine.  The pages to be deleted must be logged regardless of the method used to delete them.
0
 
Scott PletcherSenior DBACommented:
A WHERE clause on a DELETE could speed up the DELETEs considerably, since it could reduce, possibly drastically, the number of rows that needed DELETEd.

I'm not sure that TRUNCATE is technically "minimally logged".  At any rate, if you are in full recovery model, it can't be anyway.
0
 
Dung DinhDBA and Business Intelligence DeveloperCommented:
Some more information about comparison between DELETE and TRUNCATE
https://databaseskills.wordpress.com/2014/05/20/compare-truncate-and-delete-statement/
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now