views:

2036

answers:

5

(I've tried this in MySql)

I believe they're semantically equivalent. Why not identify this trivial case and speed it up?

+23  A: 

truncate table cannot be rolled back, it is like dropping and recreating the table.

Otávio Décio
truncate doesn't have to add the log entries
Traingamer
that's right, thus the almost zero time.
Otávio Décio
+18  A: 

...just to add some detail.

Calling the DELETE statement tells the database engine to generate a transaction log of all the records deleted. In the event the delete was done in error, you can restore your records.

Calling the TRUNCATE statement is a blanket "all or nothing" that removes all the records with no transaction log to restore from. It is definitely faster, but should only be done when you're sure you don't need any of the records you're going to remove.

Dillie-O
Doesn't this mean that if other processes/threads are acting on that table, they might get an inconsistent view? Or that it would otherwise be blocked from happening until other queries on the table were done?
Paul Tomblin
@Paul Tomblin: whether other processes are blocked or have an inconsistent read is going to depend on the table engine being used and any locking semantics that have been configured.
Toby Hede
+6  A: 

Delete from table deletes each row from the one at a time and adds a record into the transaction log so that the operation can be rolled back. The time taken to delete is also proportional to the number of indexes on the table, and if there are any foreign key constraints (for innodb).

Truncate effectively drops the table and recreates it and can not be performed within a transaction. It therefore required fewer operations and executes quickly. Truncate also does not make use of any on delete triggers.

Exact details about why this is quicker in MySql can be found in the MySql documentation: http://dev.mysql.com/doc/refman/5.0/en/truncate.html

Pervez Choudhury
+1 for mentioning indexes ... while it's true that there are "smart" ways of shrinking indexes during a delete, using a naive one-by-one method could be very expensive because of how you shrink a B+ tree.
Ian Varley
+5  A: 

Your question was about MySQL and I know little to nothing about MySQL as a product but I thought I'd add that in SQL Server a TRUNCATE statement can be rolled back. Try it for yourself

create table test1 (col1 int)
go
insert test1 values(3)
begin tran
truncate table test1
select * from test1
rollback tran
select * from test1

In SQL Server TRUNCATE is logged, it's just not logged in such a verbose way as DELETE is logged. I believe it's referred to as a minimally logged operation. Effectively the data pages still contain the data but their extents have been marked for deletion. As long as the data pages still exist you can roll back the truncate. Hope this is helpful. I'd be interested to know the results if somebody tries it on MySQL.

esabine
Correct, and there is a good article at http://weblogs.sqlteam.com/mladenp/archive/2007/10/03/SQL-Server-Why-is-TRUNCATE-TABLE-a-DDL-and-not.aspx which explains the differences - in SQL Server.
Cade Roux
A: 

For MySql 5 using InnoDb as the storage engine, TRUNCATE acts just like DELETE without a WHERE clause: i.e. for large tables it takes ages because it deletes rows one-by-one. This is changing in version 6.x.

see

http://dev.mysql.com/doc/refman/5.1/en/truncate.html

for 5.1 info (row-by-row with InnoDB) and

http://blogs.mysql.com/peterg/category/personal-opinion/

for changes in 6.x

davek