• Skip to main content
  • Skip to primary sidebar

DBA Diaries

Thoughts and experiences of a DBA working with SQL Server and MySQL

Differences Between TRUNCATE TABLE and DELETE

May 19, 2013 by Andy Hayes 3 Comments

If you want to remove data from a table, you can do this in a couple of ways using TRUNCATE TABLE or DELETE. A common approach is to use DELETE to remove all rows in the table but TRUNCATE TABLE offers an alternative and I will list what the differences between the two are.

DELETE

[sourcecode language=’sql’]
–Delete rows with filter
DELETE FROM SchemaName.Table WHERE…
[/sourcecode]

[sourcecode language=’sql’]
–Delete all rows
DELETE SchemaName.Table
[/sourcecode]

[sourcecode language=’sql’]
–Delete all rows
DELETE FROM SchemaName.Table
[/sourcecode]

[sourcecode language=’sql’]
–Delete using JOINS
DELETE T1
FROM SchemaName.Table1 AS T1
JOIN SchemaName.Table2 AS T2 ON T1.Column1 = T2.Column1
[/sourcecode]

[sourcecode language=’sql’]
–Delete using IN
DELETE FROM SchemaName.Table1
WHERE Column1 IN (SELECT Column1 FROM SchemaName.Table2)
[/sourcecode]

[sourcecode language=’sql’]
–Delete using EXISTS
DELETE T1
FROM SchemaName.Table1 AS T1
WHERE EXISTS
(SELECT *
FROM SchemaName.Table2 AS T2
WHERE T1.Column1 = T2.Column1)
[/sourcecode]

  • Provides more control over what data is and can be deleted.
  • Will not reseed the identity value on an identity column
  • Can be used where the DELETE operation does not violate foreign key constraint
  • Can be a slow operation if the table is large
  • Locking can be an issue if the operation is removing many rows
  • Will fire triggers
  • Transactions can be rolled back as LSN (log sequence number) is maintained in the transaction log
  • Data pages consumed may need to be reclaimed using a DBCC SHRINKDATABASE operation
  • DELETE will typically consume more transaction log space because of extra logging

TRUNCATE TABLE

[sourcecode language=’sql’]
TRUNCATE TABLE Schema.TableName;
[/sourcecode]

  • WHERE clause is not optional – it will remove all data from your table
  • Reseeds the identify value of an identity column
  • Cannot be used on a table which is referenced by a foreign key constraint
  • Can be considerably quicker than DELETE due to deallocation of data pages and minimal logging
  • Cannot be used in replication or log shipping
  • Will not fire triggers
  • Cannot be used on tables which are part of an indexed view
  • Who has permission: table owner, sysadmin, db_owner, db_ddladmin or any user with ALTER permission on the table

I wanted to expand on the differences in speed between the two operations.

When TRUNCATE TABLE is issued against a table, all the data pages are deallocated which are used to store the tables data. Compared with a DELETE, this is significantly faster as each DELETE operation is logged in the transaction log but only the deallocations are logged in the transaction log for a TRUNCATE operation.

The DELETE operation will consume database resource and locks and can therefore be considerably slower and have greater impact on your system in comparison to TRUNCATE TABLE. See this post on reducing overhead for large delete operations.

As each DELETE operation is logged inside of the transaction log, it is possible to rollback to a point in time but with TRUNCATE TABLE it is not possible to have this level of precision. In a rollback scenario, only a complete rollback could be performed.

As with a DELETE it is also possible to rollback a TRUNCATE if it has not yet been committed. You can do this by using transactions like in this very simple example:

[sourcecode language=’sql’]
BEGIN TRAN
TRUNCATE TABLE SchemaName.Table
–test success here
ROLLBACK TRAN
ELSE
COMMIT TRAN
[/sourcecode]

Time to wrap this up..

So there are a number of differences between the two. Both operations have their pros and cons and provide you with a choice over how you want to remove your data.

 

Related Posts:

  • sql grouping sets
    Using SQL GROUPING SETS for Multiple GROUP BY…
  • How to fix "conversion failed when converting date and/or time from character string"
    How to fix "conversion failed when converting date…

Filed Under: All Articles, SQL Tips and Tricks Tagged With: sql server, t-sql

About Andy Hayes

Andy Hayes is a DBA working with SQL Server since version 7.0. He has a wonderful wife and two beautiful children. He loves database technology, playing cricket, and blogging. He is passionate about sharing his experiences as a DBA and learning more to further his understanding and knowledge. You can follow me on Twitter, check out my Facebook page or follow me on Google+

Reader Interactions

Comments

  1. java67 says

    July 31, 2013 at 5:33 am

    Wonderful comparison of two, I have also shared couple of tips on this topic as http://java67.blogspot.hk/2013/07/difference-between-truncate-vs-delete-SQL-Command-database-interview-question.html. When to use truncate vs delete in database, programmer may find it useful.

    Reply
    • Andy Hayes says

      August 3, 2013 at 9:16 am

      Glad you like the article. Thanks for the comment.

      Reply

Trackbacks

  1. http://dbadiaries.com/differences-between-truncate-table-and-delete › Benedikt Schackenberg says:
    May 19, 2013 at 12:25 pm

    […] Kommentar hinterlassen […]

    Reply

Leave a Reply to Andy Hayes Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Categories

  • All Articles (84)
  • Career Development (8)
  • MySQL Administration (18)
  • MySQL Performance (2)
  • SQL Server Administration (24)
  • SQL Server News (3)
  • SQL Server Performance (14)
  • SQL Server Security (3)
  • SQL Tips and Tricks (21)

Top 10 Popular Posts

  • Using sp_change_users_login to fix SQL Server orphaned users
  • MySQL SHOW USERS? – How to List All MySQL Users and Privileges
  • How to shrink tempdb
  • How to Transfer Logins to Another SQL Server or Instance
  • How to Delete Millions of Rows using T-SQL with Reduced Impact
  • T-SQL – How to Select Top N Rows for Each Group Using ROW_NUMBER()
  • New T-SQL features in SQL Server 2012 – OFFSET and FETCH
  • How to Kill All MySQL Processes For a Specific User
  • Using exec sp_who2 to help with SQL Server troubleshooting
  • How to fix “conversion failed when converting date and/or time from character string”

Recent Posts

  • How to fix “conversion failed when converting date and/or time from character string”
  • Using SQL GROUPING SETS for Multiple GROUP BY Queries in a Single Query
  • How to Setup MySQL Master Master Replication
  • How To Use SQL to Convert a STRING to an INT
  • How to set up MySQL Replication Tutorial

Search

Connect

  • Twitter
  • Facebook
  • RSS

About

  • Cookie Policy
  • Disclaimer
  • About
Copyright ©