Solved

Delete and truncate comparison

Posted on 2015-01-19
4
130 Views
Last Modified: 2015-01-20
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
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 40558714

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
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 150 total points
ID: 40558715
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
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 150 total points
ID: 40558729
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
 
LVL 8

Assisted Solution

by:Dung Dinh
Dung Dinh earned 100 total points
ID: 40559032
Some more information about comparison between DELETE and TRUNCATE
https://databaseskills.wordpress.com/2014/05/20/compare-truncate-and-delete-statement/
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question