(Reproduced) The difference between drop, truncate and delete

Reprinted from https://www.cnblogs.com/zhizhao/p/7825469.html

(1) The delete process of the DELETE statement is to delete a row from the table each time, and at the same time the delete operation of the row is recorded in the log as a transaction and saved in the log for rollback operation.

TRUNCATE TABLE deletes all data from the table at one time and does not record individual delete operation records into the log for storage. Deleted rows cannot be recovered. And the delete trigger related to the table will not be activated during the delete process. The execution speed is fast.

(2) The space occupied by tables and indexes.

When the table is TRUNCATE, the space occupied by the table and index will be restored to the initial size,

DELETE operation will not reduce the space occupied by the table or index.

The drop statement releases all the space occupied by the table.

(3) Generally speaking, drop> truncate> delete

(4) The scope of application.

TRUNCATE 只能对TABLE;         DELETE可以是table和view

(5) TRUNCATE and DELETE only delete data, DROP deletes the entire table (structure and data).

(6) truncate and delete without where: only delete data, without deleting the structure of the table (definition) drop statement will delete the constraint (constrain), trigger (trigger) index (index); dependent The stored procedures/functions in this table will be retained, but their status will become: invalid.

(7) The delete statement is DML (data maintain language), this operation will be placed in the rollback segment, and it will take effect after the transaction is committed. If there is a corresponding tigger, it will be triggered when it is executed.

(8) truncate and drop are DLL (data define language), the operation takes effect immediately, the original data is not placed in the rollback segment and cannot be rolled back

(9) When there is no backup, use drop and truncate with caution. To delete some data rows, use delete and pay attention to combining where to restrict the scope of influence. The rollback segment must be large enough. To delete the table, use drop; if you want to keep the table and delete the data in the table, if it has nothing to do with the transaction, you can use truncate. If it is related to business, or the teacher wants to trigger the trigger, still use delete.

(10) Truncate table table name is fast and efficient, because:
truncate table is functionally the same as the DELETE statement without WHERE clause: both delete all rows in the table. But TRUNCATE TABLE is faster than DELETE, and it uses less system and transaction log resources. The DELETE statement deletes one row at a time, and records an entry in the transaction log for each row deleted. TRUNCATE TABLE deletes data by releasing data pages used to store table data, and only records the release of pages in the transaction log.

(11) TRUNCATE TABLE deletes all rows in the table, but the table structure and its columns, constraints, indexes, etc. remain unchanged. The count value used for the new row identification is reset to the seed of the column. If you want to keep the mark count value, use DELETE instead. If you want to delete the table definition and its data, use the DROP TABLE statement.

(12) For tables referenced by FOREIGN KEY constraints, TRUNCATE TABLE cannot be used, but DELETE statements without WHERE clauses should be used. Since TRUNCATE TABLE is not recorded in the log, it cannot activate triggers.

One, delete

1. Delete is DML. When the delete operation is executed, a row is deleted from the table each time, and the row's delete operation is recorded in the redo and undo tablespaces for rollback and redo operations, but Note that the table space must be large enough, and the commit operation needs to be manually submitted to take effect, and the operation can be undone by rollback.

2. Delete can delete the data that meets the conditions in the table according to the conditions. If the where clause is not specified, all records in the table are deleted.

3. The delete statement does not affect the extent occupied by the table, and the high watermark remains unchanged.

Two, truncate

1. Truncate is DDL and will be submitted implicitly. Therefore, it cannot be rolled back and the trigger will not be triggered.

2, truncate will delete all records in the table, and will reset the high waterline and all indexes, by default the space will be released to minextents extents, unless reuse storage is used. No log is recorded, so the execution speed is very fast, but the operation cannot be undone by rollback (if you accidentally truncate a table, it can be restored, but it cannot be restored by rollback).

3. For tables referenced by foreign key constraints, truncate table cannot be used, but the delete statement without the where clause should be used.

4. truncatetable cannot be used for tables that participate in indexed views.

Three, drop

1. The drop is DDL and will be submitted implicitly, so it cannot be rolled back and the trigger will not be triggered.

2. The drop statement deletes the table structure and all data, and releases all the space occupied by the table.

3. The drop statement will delete the constraints, triggers, indexes, and stored procedures/functions that depend on the table's structure, but will remain in the invalid state.

to sum up:

1. In terms of speed, generally speaking, drop> truncate> delete.

2. Be careful when using drop and truncate. Although it can be restored, in order to reduce trouble, you must be careful.

3. If you want to delete part of the data and use delete, pay attention to the where clause, and the rollback segment should be large enough;

If you want to delete the table, of course use drop;

If you want to keep the table and delete all data, if it has nothing to do with the transaction, use truncate;

If it is related to the transaction, or want to trigger the trigger, still use delete;

If you are arranging the fragments inside the table, you can use truncate to keep up with the reuse stroage, and then re-import/insert the data.

Guess you like

Origin blog.csdn.net/weixin_39195030/article/details/87928359