SQL中TRUNCATE和DELETE的区别是什么?

如果你的答案是针对特定平台的,请注明。


当前回答

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.

参考资料请点击这里

其他回答

对原始答案的一个小修正——删除也会产生大量的重做(因为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不会记录任何东西”是正确的。我想更进一步:

Truncate不会在事务的上下文中执行。

截断比删除的速度优势应该是明显的。根据你的情况,这种优势从微不足道到巨大不等。

然而,我看到截断无意中破坏了引用完整性,并违反了其他约束。通过在事务外部修改数据而获得的权力,必须与在没有安全网的情况下走钢丝时继承的责任相平衡。

下面是我对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.

简而言之,truncate不会记录任何内容(因此更快,但不能撤消),而delete会记录日志(并且可以是更大事务的一部分,将回滚等)。如果您在开发中有不想在表中使用的数据,通常最好截断,因为您不会冒填满事务日志的风险

都是很好的答案,我必须补充一句:

由于TRUNCATE TABLE是一个DDL(数据定义语言)命令,而不是DML(数据操作语言)命令,删除触发器不会运行。