join the MSSQLTips community

Today's Site Sponsor


 

Find performance issues related to Analysis Services memory limits.
 


Deleting Data in SQL Server with TRUNCATE vs DELETE commands
Written By: Edgewood Solutions Engineers -- 10/11/2006 -- 0 comments -- printer friendly -- become a member



I generated better data in only seconds...

        Win SQL Books  -----  SharePoint Tips  -----  Live Webcast - SQL Backup Mistakes  -----  Bookmark and Share        

Problem
There are two main keywords used for deleting data from a table: TRUNCATE and DELETE. Although each achieves the same result, the methods employed for each vastly differ. There are advantages, limitations, and consequences of each that you should consider when deciding which method to use.

Solution
Deleting Data Using TRUNCATE TABLE

TRUNCATE TABLE is a statement that quickly deletes all records in a table by deallocating the data pages used by the table. This reduces the resource overhead of logging the deletions, as well as the number of locks acquired; however, it bypasses the transaction log, and the only record of the truncation in the transaction logs is the page deallocation. Records removed by the TRUNCATE TABLE statement cannot be restored. You cannot specify a WHERE clause in a TRUNCATE TABLE statement-it is all or nothing. The advantage to using TRUNCATE TABLE is that in addition to removing all rows from the table it resets the IDENTITY back to the SEED, and the deallocated pages are returned to the system for use in other areas.

In addition, TRUNCATE TABLE statements cannot be used for tables involved in replication or log shipping, since both depend on the transaction log to keep remote databases consistent.
TRUNCATE TABLE cannot used be used when a foreign key references the table to be truncated, since TRUNCATE statements do not fire triggers. This could result in inconsistent data because ON DELETE/UPDATE triggers would not fire. If all table rows need to be deleted and there is a foreign key referencing the table, you must drop the index and recreate it. If a TRUNCATE TABLE statement is issued against a table that has foreign key references, the following error is returned:

Error message when attempting TRUNCATE TABLE on table referenced by FK

Deleting Data Using DELETE FROM Statement

DELETE TABLE statements delete rows one at a time, logging each row in the transaction log, as well as maintaining log sequence number (LSN) information. Although this consumes more database resources and locks, these transactions can be rolled back if necessary. You can also specify a WHERE clause to narrow down the rows to be deleted. When you delete a large number of rows using a DELETE FROM statement, the table may hang on to the empty pages requiring manual release using DBCC SHRINKDATABASE (db_name).
When large tables require that all records be deleted and TRUNCATE TABLE cannot be used, the following statements can be used to achieve the same result as TRUNCATE TABLE:

  • DELETE from "table_name"
  • DBCC CHECKIDENT("table_name", RESEED, "reseed_value")

DBCC CHECKIDENT with RESEED

Next Steps

  • Review information on the TRUNCATE TABLE statement and the DELETE statement.
  • Review the structure of tables you need to rid of large amounts of data from
  • Stay tuned to MSSQLTIPS for more information
Readers Who Read This Tip Also Read Comment or Ask Questions About This Tip Twitter This Tip!


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try Red Gate SQL Backup Pro for smaller, more robust SQL Server backups. Download a free trial now!

Wearing too many hats? We deliver valuable SQL Server consulting services for success.

Looking for SQL Server interview questions and answers?

Learn SQL Server 2008, Performance Tuning, Development, Administration, DR, Replication and more from these web casts

All SQL Server, all the time! Sign-up for the MSSQLTips newsletter!

Do you work on SharePoint too? Check this out...

Free Whitepaper - The Seven Steps to Successful SQL Server Auditing


 

 

Red Gate Software - SQL Data Generator

Test your database until it cries… “Red Gate’s SQL Data Generator has overnight become the principal tool we use for loading test data to run our performance and load tests.” Grant Fritchey, FM Global.

Download now!



More SQL Server Tools
SQL Prompt

SQL Compare

SQL defrag manager

SQL Nitro

SQL secure




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.