删除表和截断表之间的区别?

时间:2020-03-06 14:44:44  来源:igfitidea点击:

在报表汇总中,我构建了一些表。后来我根本不需要它们了。有人提到要截断它们,因为这样会更快。

解决方案

DROP TABLE删除表。

TRUNCATE TABLE清空它,但保留其结构以备将来使用。

truncate删除所有行,但不删除表本身,从本质上讲,它等效于不带where子句的删除,但通常更快。

Drop完全摆脱了表,同时也删除了定义。截断将清空表,但不会删除该定义。

截断表将清空表。删除表会完全删除它。任一种都会很快,但是删除它可能会更快(取决于数据库引擎)。

如果我们不再需要它,请将其删除,以免使架构混乱。

在SQL标准中,DROP表删除该表,而表架构TRUNCATE删除所有行。

TRUNCATE TABLE保留所有旧索引和其他内容。显然,DROP TABLE将摆脱该表,并要求我们稍后重新创建它。

我认为意思是DELETE TABLE和TRUNCATE TABLE之间的区别。

滴台

remove the table from the database.

删除表

without a condition delete all rows. If there are trigger and references then this will process for every row. Also a index will be modify if there one.

截断表

set the row count zero and without logging each row. That it is many faster as the other both.

从表中删除记录会记录每次删除,并为删除的记录执行删除触发器。截断是一种更强大的命令,可清空表而不记录每一行。由于需要检查每一行上的外键,因此SQL Server阻止我们使用带有引用该表的外键的表来对其进行截断。

截断通常是超快的,非常适合清除临时表中的数据。它的确保留了表的结构以备将来使用。

如果我们实际上要删除表定义以及数据,只需删除表即可。

有关更多信息,请参见此MSDN文章。

DROP和TRUNC做不同的事情:

截断表

Removes all rows from a table without
  logging the individual row deletions.
  TRUNCATE TABLE is similar to the
  DELETE statement with no WHERE clause;
  however, TRUNCATE TABLE is faster and
  uses fewer system and transaction log
  resources.

滴台

Removes one or more table definitions
  and all data, indexes, triggers,
  constraints, and permission
  specifications for those tables.

就速度而言,差异应该很小。而且无论如何,如果我们根本不需要表结构,请一定使用DROP。

此处的答案与问题相对应,但我将回答我们没有提出的问题。 "我应该使用截断还是删除?"如果要从表中删除所有行,通常会希望进行截断,因为这样做要快得多。为什么要快得多?至少对于Oracle,它会重置高水位线。这基本上是对数据的取消引用,并允许db将其重用于其他用途。

这些答案都没有指出这两个操作之间的重要区别。删除表是可以回滚的操作。但是,截断不能回滚['TRUNCATE TABLE'也可以回滚]。这样,如果有很多行,则删除非常大的表可能会非常昂贵,因为所有行都必须记录在临时空间中,以防我们决定将其回滚。

通常,如果我想摆脱一个大表,我会截断它,然后将其删除。这样,无需记录就可以删除数据,并且可以删除表,并且由于无需记录数据,因此删除操作非常便宜。

重要的是要指出,尽管截断仅会删除数据,然后离开表,而drop实际上会删除数据和表本身。 (假设外键不排除这种动作)

我对上面的其中一项陈述进行了更正..."截断无法回滚"

截断可以回滚。在某些情况下,我们无法执行截断或者删除表,例如,当我们具有外键引用时。对于诸如月度报告之类的任务,一旦不再需要时,我可能会放下桌子。如果我更频繁地执行此汇总报告,那么我可能会保留该表并使用truncate。

希望这会有所帮助,这里有一些我们应该会觉得有用的信息...

请参阅以下文章以了解更多详细信息:
http://sqlblog.com/blogs/denis_gobo/archive/2007/06/13/1458.aspx

另外,有关删除和截断的更多详细信息,请参见本文:
http://www.sql-server-performance.com/faq/delete_truncate_difference_p1.aspx

谢谢!
杰夫

TRUNCATE TABLE is functionally
  identical to DELETE statement with no
  WHERE clause: both remove all rows in
  the table. But TRUNCATE TABLE is
  faster and uses fewer system and
  transaction log resources than DELETE.
  
  The DELETE statement removes rows one
  at a time and records an entry in the
  transaction log for each deleted row.
  TRUNCATE TABLE removes the data by
  deallocating the data pages used to
  store the table's data, and only the
  page deallocations are recorded in the
  transaction log.
  
  TRUNCATE TABLE removes all rows from a
  table, but the table structure and its
  columns, constraints, indexes and so
  on remain. The counter used by an
  identity for new rows is reset to the
  seed for the column. If you want to
  retain the identity counter, use
  DELETE instead. If you want to remove
  table definition and its data, use the
  DROP TABLE statement.
  
  You cannot use TRUNCATE TABLE on a
  table referenced by a FOREIGN KEY
  constraint; instead, use DELETE
  statement without a WHERE clause.
  Because TRUNCATE TABLE is not logged,
  it cannot activate a trigger.
  
  TRUNCATE TABLE may not be used on
  tables participating in an indexed
  view.

来自http://msdn.microsoft.com/zh-cn/library/aa260621(SQL.80).aspx

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.
  
  TRUNCATE
  
  TRUNCATE removes all rows from a table. The operation cannot be rolled back ... As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.

来自:http://www.orafaq.com/faq/difference_between_truncate_delete_and_drop_commands

  • DELETE语句一次删除一个行,并在事务日志中为每个删除的行记录一个条目。 TRUNCATE TABLE通过取消分配用于存储表数据的数据页来删除数据,并仅在事务日志中记录页面的解除分配
  • 我们可以在DELETE中使用WHERE子句,但在TRUNCATE中则不能使用它
  • 当使用行锁执行DELETE语句时,表中的每一行都被锁定以删除。 TRUNCATE TABLE总是锁定表和页面,但不锁定每一行
  • 执行DELETE语句后,该表仍可以包含空白页。如果删除操作未使用表锁,则该表(堆)将包含许多空白页。对于索引,删除操作可以留下空白页,尽管这些页将通过后台清理过程快速释放。
  • TRUNCATE TABLE从表中删除所有行,但表结构及其列,约束,索引等仍保留
  • DELETE语句不是RESEED身份列,而是TRUNCATE语句RESEEDS``IDENTITY
  • 参与索引视图。
  • 通过使用事务复制或者合并复制来发布
  • TRUNCATE TABLE无法激活触发器,因为该操作不会记录单个行的删除