SQL中TRUNCATE和DELETE的区别是什么?
如果你的答案是针对特定平台的,请注明。
SQL中TRUNCATE和DELETE的区别是什么?
如果你的答案是针对特定平台的,请注明。
下面是一些不同之处。我强调了特定于oracle的特性,希望社区也能添加其他供应商的特定差异。对于大多数供应商来说常见的差异可以直接放在标题下面,并在下面突出显示差异。
总体概述
如果您想要快速删除表中的所有行,并且您确实确定要这样做,并且表上没有外键,那么TRUNCATE可能会比delete更快。
必须考虑各种特定于系统的问题,详情如下。
语句类型
删除是DML,截断是DDL(什么是DDL和DML?)
提交和回滚
供应商变量
SQL *服务器
Truncate可以回滚。
PostgreSQL
Truncate可以回滚。
甲骨文
因为TRUNCATE是DDL,它涉及两次提交,一次在语句执行之前,一次在语句执行之后。因此,Truncate不能回滚,截断过程中的失败无论如何都会发出提交。
然而,请看下面的闪回。
空间回收
Delete不能恢复空间,Truncate只能恢复空间
甲骨文
如果使用了REUSE STORAGE子句,那么数据段就不会被重新分配,如果要用数据重新加载表,这样做的效率会稍微高一些。重置高水位标志。
行范围
Delete可用于删除所有行或仅删除行的子集。Truncate删除所有行。
甲骨文
在对表进行分区时,可以隔离地截断各个分区,从而可以部分删除表的所有数据。
对象类型
Delete可以应用于集群内的表和表。Truncate仅适用于表或整个集群。(可能是Oracle特有的)
数据对象标识
甲骨文
Delete不会影响数据对象id,但truncate会分配一个新的数据对象id,除非自该表创建以来从未对该表进行过插入操作。即使是一个回滚的插入操作也会导致在截断时分配一个新的数据对象id。
闪回(Oracle)
闪回可以跨删除操作,但截断操作可以防止闪回操作之前的状态。
然而,从11gR2 FLASHBACK ARCHIVE功能允许这一点,除了在快速版
在Oracle中使用FLASHBACK http://docs.oracle.com/cd/E11882_01/appdev.112/e41502/adfns_flashback.htm#ADFNS638
特权
变量
甲骨文
可以将一个表上的Delete授权给另一个用户或角色,但是截断不能不使用DROP ANY table授权。
Redo /。
删除会产生少量的重做和大量的撤销。Truncate生成的两者数量都可以忽略不计。
索引
甲骨文
截断操作使不可用的索引重新可用。删除则不然。
外键
当启用的外键引用表时,不能应用截断。使用delete的处理取决于外键的配置。
表锁
甲骨文
Truncate需要一个排他表锁,delete需要一个共享表锁。因此,禁用表锁是防止对表进行截断操作的一种方法。
触发器
DML触发器不会在截断时触发。
甲骨文
DDL触发器是可用的。
远程执行
甲骨文
不能在数据库链接上发出Truncate命令。
标识列
SQL *服务器
Truncate重置IDENTITY列类型的序列,delete则不会。
结果集
在大多数实现中,DELETE语句可以向客户端返回已删除的行。
例如,在Oracle PL/SQL子程序中,你可以:
DELETE FROM employees_temp
WHERE employee_id = 299
RETURNING first_name,
last_name
INTO emp_first_name,
emp_last_name;
简而言之,truncate不会记录任何内容(因此更快,但不能撤消),而delete会记录日志(并且可以是更大事务的一部分,将回滚等)。如果您在开发中有不想在表中使用的数据,通常最好截断,因为您不会冒填满事务日志的风险
最大的区别是truncate是不记录日志的操作,而delete是。
简单地说,这意味着在数据库崩溃的情况下,不能通过截断恢复所操作的数据,但可以通过删除恢复。
详情请点击这里
“Truncate不会记录任何东西”是正确的。我想更进一步:
Truncate不会在事务的上下文中执行。
截断比删除的速度优势应该是明显的。根据你的情况,这种优势从微不足道到巨大不等。
然而,我看到截断无意中破坏了引用完整性,并违反了其他约束。通过在事务外部修改数据而获得的权力,必须与在没有安全网的情况下走钢丝时继承的责任相平衡。
是的,DELETE比较慢,TRUNCATE比较快。为什么?
DELETE必须读取记录,检查约束,更新块,更新索引,并生成重做/撤销。所有这些都需要时间。
TRUNCATE只是在数据库中为表(High Water Mark)调整一个指针,然后!数据不见了。
这是Oracle特有的,AFAIK。
对原始答案的一个小修正——删除也会产生大量的重做(因为undo本身是由重做保护的)。这可以从autotrace输出中看到:
SQL> delete from t1;
10918 rows deleted.
Elapsed: 00:00:00.58
Execution Plan
----------------------------------------------------------
0 DELETE STATEMENT Optimizer=FIRST_ROWS (Cost=43 Card=1)
1 0 DELETE OF 'T1'
2 1 TABLE ACCESS (FULL) OF 'T1' (TABLE) (Cost=43 Card=1)
Statistics
----------------------------------------------------------
30 recursive calls
12118 db block gets
213 consistent gets
142 physical reads
3975328 redo size
441 bytes sent via SQL*Net to client
537 bytes received via SQL*Net from client
4 SQL*Net roundtrips to/from client
2 sorts (memory)
0 sorts (disk)
10918 rows processed
TRUNCATE是DDL语句,而DELETE是DML语句。以下是两者的区别:
As TRUNCATE is a DDL (Data definition language) statement it does not require a commit to make the changes permanent. And this is the reason why rows deleted by truncate could not be rollbacked. On the other hand DELETE is a DML (Data manipulation language) statement hence requires explicit commit to make its effect permanent. TRUNCATE always removes all the rows from a table, leaving the table empty and the table structure intact whereas DELETE may remove conditionally if the where clause is used. The rows deleted by TRUNCATE TABLE statement cannot be restored and you can not specify the where clause in the TRUNCATE statement. TRUNCATE statements does not fire triggers as opposed of on delete trigger on DELETE statement
这里有一个非常好的与主题相关的链接。
truncate和delete的区别如下:
+----------------------------------------+----------------------------------------------+
| Truncate | Delete |
+----------------------------------------+----------------------------------------------+
| We can't Rollback after performing | We can Rollback after delete. |
| Truncate. | |
| | |
| Example: | Example: |
| BEGIN TRAN | BEGIN TRAN |
| TRUNCATE TABLE tranTest | DELETE FROM tranTest |
| SELECT * FROM tranTest | SELECT * FROM tranTest |
| ROLLBACK | ROLLBACK |
| SELECT * FROM tranTest | SELECT * FROM tranTest |
+----------------------------------------+----------------------------------------------+
| Truncate reset identity of table. | Delete does not reset identity of table. |
+----------------------------------------+----------------------------------------------+
| It locks the entire table. | It locks the table row. |
+----------------------------------------+----------------------------------------------+
| Its DDL(Data Definition Language) | Its DML(Data Manipulation Language) |
| command. | command. |
+----------------------------------------+----------------------------------------------+
| We can't use WHERE clause with it. | We can use WHERE to filter data to delete. |
+----------------------------------------+----------------------------------------------+
| Trigger is not fired while truncate. | Trigger is fired. |
+----------------------------------------+----------------------------------------------+
| Syntax : | Syntax : |
| 1) TRUNCATE TABLE table_name | 1) DELETE FROM table_name |
| | 2) DELETE FROM table_name WHERE |
| | example_column_id IN (1,2,3) |
+----------------------------------------+----------------------------------------------+
DELETE语句:该命令只根据where子句中给定的条件从表中删除行,如果没有指定条件,则从表中删除所有行。但是它不会释放包含表的空间。
SQL DELETE语句的语法是:
DELETE FROM table_name [WHERE condition];
TRUNCATE语句:该命令用于删除表中的所有行,并释放包含表的空间。
删除
DELETE是一个DML命令 DELETE可以回退 Delete =仅删除-因此可以回滚 在DELETE中,可以使用WHERE子句编写条件 语法- Delete from [Table] where [Condition]
截断
TRUNCATE是一个DDL命令 不能在TRUNCATE中回滚,TRUNCATE将永久删除该记录 Truncate = Delete+Commit -这样我们就不能回滚 不能在TRUNCATE中使用条件(WHERE子句) 语法- Truncate table [table]
详情请浏览
http://www.zilckh.com/what-is-the-difference-between-truncate-and-delete/
并不是说截断不记录SQL Server中的任何内容。truncate不记录任何信息,但它记录您触发truncate的表的数据页的释放。
如果在开始时定义事务,则可以回滚被截断的记录,回滚后可以恢复被截断的记录。但不能在提交截断事务后从事务日志备份中恢复截断记录。
在这个例子中,Truncate也可以被rollback
begin Tran
delete from Employee
select * from Employee
Rollback
select * from Employee
如果不小心使用Delete/Truncate从表中删除了所有数据。您可以回滚已提交的事务。恢复上次备份并运行事务日志,直到将要执行删除/截断操作。
以下相关信息来自一篇博客文章:
While working on database, we are using Delete and Truncate without knowing the differences between them. In this article we will discuss the difference between Delete and Truncate in Sql. Delete: Delete is a DML command. Delete statement is executed using a row lock,each row in the table is locked for deletion. We can specify filters in where clause. It deletes specified data if where condition exists. Delete activities a trigger because the operation are logged individually. Slower than Truncate because it Keeps logs Truncate Truncate is a DDL command. Truncate table always lock the table and page but not each row.As it removes all the data. Cannot use Where condition. It Removes all the data. Truncate table cannot activate a trigger because the operation does not log individual row deletions. Faster in performance wise, because it doesn't keep any logs. Note: Delete and Truncate both can be rolled back when used with Transaction. If Transaction is done, means committed then we can not rollback Truncate command, but we can still rollback Delete command from Log files, as delete write records them in Log file in case it is needed to rollback in future from log files. If you have a Foreign key constraint referring to the table you are trying to truncate, this won't work even if the referring table has no data in it. This is because the foreign key checking is done with DDL rather than DML. This can be got around by temporarily disabling the foreign key constraint(s) to the table. Delete table is a logged operation. So the deletion of each row gets logged in the transaction log, which makes it slow. Truncate table also deletes all the rows in a table, but it won't log the deletion of each row instead it logs the deallocation of the data pages of the table, which makes it faster. ~ If accidentally you removed all the data from table using Delete/Truncate. You can rollback committed transaction. Restore the last backup and run transaction log till the time when Delete/Truncate is about to happen.
如果将TRUNCATE包装在事务中,则可以回滚。
请参阅下面的两个参考资料并自行测试:-
http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolled-back-using-log-files-after-transaction-session-is-closed/
http://sqlblog.com/blogs/kalen_delaney/archive/2010/10/12/tsql-tuesday-11-rolling-back-truncate-table.aspx
截断与删除是SQL面试中最臭名昭著的问题之一。一定要向面试官解释清楚,否则可能会让你丢掉这份工作。问题是没有多少人意识到这一点,所以如果你告诉他们YES Truncate可以回滚,他们很可能会认为答案是错误的。
Truncate and Delete in SQL are two commands which is used to remove or delete data from table. Though quite basic in nature both Sql commands can create lot of trouble until you are familiar with details before using it. An Incorrect choice of command can result is either very slow process or can even blew up log segment, if too much data needs to be removed and log segment is not enough. That's why it's critical to know when to use truncate and delete command in SQL but before using these you should be aware of the Differences between Truncate and Delete, and based upon them, we should be able to find out when DELETE is better option for removing data or TRUNCATE should be used to purge tables.
参考资料请点击这里
DELETE The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire. TRUNCATE TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE. DROP The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back. DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.
来自:http://www.orafaq.com/faq/difference_between_truncate_delete_and_drop_commands
DROP The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back. TRUNCATE TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUNCATE is faster and doesn't use as much undo space as a DELETE. Table level lock will be added when Truncating. DELETE The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire. Row level lock will be added when deleting.
来自:http://www.orafaq.com/faq/difference_between_truncate_delete_and_drop_commands
下面是我对SQL Server中DELETE和TRUNCATE的区别的详细回答
•删除数据:首先,两者都可以用于从表中删除行。 但是,根据提供程序的功能,DELETE不仅可以用于从表中删除行,还可以用于从VIEW或OPENROWSET或OPENQUERY的结果中删除行。
•FROM子句:使用DELETE,您还可以使用另一个FROM子句根据另一个表的行从一个表/视图/rowset_function_limited中删除行。在那个FROM子句中,您还可以编写正常的JOIN条件。实际上,通过将SELECT替换为DELETE并删除列名,可以从不包含任何聚合函数的SELECT语句创建DELETE语句。 对于TRUNCATE,你不能这样做。
•WHERE: TRUNCATE不能有WHERE条件,但DELETE可以。这意味着使用TRUNCATE您不能删除特定的行或特定的行组。 TRUNCATE TABLE类似于不带WHERE子句的DELETE语句。
•性能:TRUNCATE TABLE更快,使用更少的系统和事务日志资源。 其中一个原因是两个语句都使用了锁。DELETE语句使用行锁执行,表中的每一行都被锁定以便删除。TRUNCATE TABLE总是锁定表和页,而不是每一行。
•事务日志:DELETE语句一次删除一行,并在事务日志中为每行创建单独的条目。 TRUNCATE TABLE通过释放用于存储表数据的数据页来删除数据,并且在事务日志中只记录页的释放。
•page:执行DELETE语句后,表中仍然可以包含空页。 TRUNCATE通过释放用于存储表数据的数据页来删除数据。
•Trigger: TRUNCATE不会激活表上的删除触发器。因此,在使用TRUNCATE时必须非常小心。如果在表上定义了delete Trigger以在删除行时执行一些自动清理或记录操作,则永远不应该使用TRUNCATE。
•标识列:如果表包含标识列,则使用TRUNCATE,该列的计数器将重置为为该列定义的种子值。如果没有定义种子,则使用默认值1。 DELETE不会重置标识计数器。因此,如果希望保留标识计数器,请使用DELETE。
•Replication: DELETE可以用于事务性复制或合并复制中使用的表。 而TRUNCATE不能用于事务性复制或合并复制中涉及的表。
•Rollback: DELETE语句可以回滚。 TRUNCATE也可以回滚,前提是它被包含在TRANSACTION块中并且会话没有关闭。一旦会话关闭,您将无法回滚TRUNCATE。
• Restrictions : The DELETE statement may fail if it violates a trigger or tries to remove a row referenced by data in another table with a FOREIGN KEY constraint. If the DELETE removes multiple rows, and any one of the removed rows violates a trigger or constraint, the statement is canceled, an error is returned, and no rows are removed. And if DELETE is used against View, that View must be an Updatable view. TRUNCATE cannot be used against the table used in Indexed view. TRUNCATE cannot be used against the table referenced by a FOREIGN KEY constraint, unless a table that has a foreign key that references itself.
SQL server中删除与截断的总结 完整文章请点击这个链接:http://codaffection.com/sql-server-article/delete-vs-truncate-in-sql-server/
摘自dotnet mob文章:删除Vs截断SQL Server
DELETE语句可以有一个WHERE子句来删除特定的记录,而TRUNCATE语句不需要任何子句并擦除整个表。 重要的是,DELETE语句记录删除日期,而TRUNCATE语句不记录删除日期。
微软sql server的另一个不同之处在于,使用delete,你可以使用output语句来跟踪哪些记录被删除了,例如:
delete from [SomeTable]
output deleted.Id, deleted.Name
你不能用truncate这样做。
Truncate命令用于重新初始化表,它是一个删除表中所有行的DDL命令。DELETE是一个DML命令,用于根据某些条件删除一行或一组行,如果没有指定condition,则该命令将删除表中的所有行。
下面是这些sql命令之间一些重要区别的总结:
SQL truncate命令:
1)它是一个DDL(数据定义语言)命令,因此诸如COMMIT和ROLLBACK之类的命令不适用于此命令(这里的例外是PostgreSQL和MSSQL,它们的TRUNCATE命令的实现允许该命令在事务中使用)
2)你不能撤销删除记录的操作,它是自动发生的,并且是不可逆的(除了上面的例外情况-但是,如果该操作包含在TRANSACTION块中并且会话没有关闭)。Oracle -包含两个隐式提交,一个在语句执行之前,一个在语句执行之后。因此,该命令不能被撤回,而运行时错误将导致提交
3)删除表中的所有记录,记录不能被限制删除。对于Oracle,当表被划分为每个分区时,单个分区可以被隔离截断(TRUNCATE),从而可以从表中部分删除所有数据
4)释放表中数据所占用的空间(在磁盘的表空间中)。对于Oracle -如果你使用REUSE STORAGE子句,数据段将不会回滚,也就是说,你将保留已删除行的空间分配给表,如果表要用数据重新加载,这可能会更有效一点。高分将被重置
5) TRUNCATE比DELETE工作得快得多
6)在TRUNCATE的情况下,Oracle闪回防止回到术前状态
7) Oracle - TRUNCATE不能授予(GRANT)不使用DROP ANY表
8) TRUNCATE操作使不可用的索引重新可用
9)当启用的外键指向另一个表时,TRUNCATE不能使用,那么你可以:
执行命令:DROP CONSTRAINT,然后TRUNCATE,然后通过CREATE CONSTRAINT或播放它 SET FOREIGN_KEY_CHECKS = 0;然后截断,然后:SET_FOREIGN_KEY_CHECKS = 1;
10) TRUNCATE需要一个排他表锁,因此,关闭排他表锁是防止在表上进行TRUNCATE操作的一种方法
11) DML触发器在执行TRUNCATE后不会触发(所以在这种情况下要非常小心,如果在表中定义了删除触发器来执行自动的表清理或行删除后的登录操作,则不应该使用TRUNCATE)。在Oracle上,DDL触发器被触发
12) Oracle - TRUNCATE不能用于:database link的情况 13) TRUNCATE不返回删除的记录数量
14)事务日志-一个指示页面释放的日志(删除数据,释放用于存储表数据的数据页的分配,并只将页面释放写入事务日志)-执行速度比DELETE更快。TRUNCATE只需要调整数据库中指向表(High Water Mark)的指针,数据立即被删除,因此它使用更少的系统资源和事务日志
15)性能(获得锁)-表和页锁-在执行过程中不会降低性能
16) TRUNCATE不能用于涉及事务复制或合并复制的表
SQL删除命令:
1)该命令为DML (Data Manipulation Language)命令,因此使用了COMMIT和ROLLBACK命令
2)可以使用ROLLBACK命令撤销删除记录的操作
3)从表中删除全部或部分记录,您可以使用WHERE子句限制要删除的记录
4)不释放表中数据占用的空间(在表空间中-在磁盘上)
5) DELETE比TRUNCATE慢得多
6) Oracle Flashback适用于DELETE
7) Oracle -对于DELETE,您可以使用GRANT命令
8) DELETE操作不会使不可用的索引重新可用
9)如果外键enabled是指另一个表,可以(或不)应用,取决于外键配置(如果没有),请:
执行命令:DROP CONSTRAINT,然后TRUNCATE,然后通过CREATE CONSTRAINT或播放它 SET FOREIGN_KEY_CHECKS = 0;然后截断,然后:SET_FOREIGN_KEY_CHECKS = 1;
10) DELETE需要一个共享表锁
11)引发火灾
12) DELETE可用于:database link的情况
13) DELETE返回已删除记录的数量
14)事务日志-对于每个删除的记录(每次删除一行,并为每个删除的行在事务日志中记录一个条目)-执行速度比TRUNCATE慢。执行DELETE语句后,表可能仍然包含空白页。DELETE需要读取记录、检查约束、更新块、更新索引以及生成重做/撤销。所有这些都需要时间,因此它所花费的时间比TRUNCATE要长得多
15)性能(获得锁)-记录锁-在执行过程中降低性能-表中的每条记录都被锁定以便删除
16)可以在事务性复制或合并复制的表上使用DELETE