Mysql difference in drop, truncate and delete

(. 1) the DELETE process statement each deletion is the deletion of a row from the table, and simultaneously the row as a delete transaction records saved for rollback operation performed in the log .

   TRUNCATE  TABLE deleted once all the data from the single table does not delete stored records logged, delete rows can not be restored . And in the process deleted not activate delete trigger associated with a table . Fast execution speed.

(2) table and index space occupied.

   When the table is TRUNCATE, this table and index space occupied will return to its original size,

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

   drop statement table space occupied by the fully relieved.

(3) In general, then delete the speed, drop> truncate> delete

(4) Application.

    TRUNCATE can only TABLE; DELETE can be a table and view

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

(. 6) TRUNCATE delete without a where: Delete only data, without deleting the table structure (as defined) drop statement removes the table structure is dependent constraints (constrain), the trigger (Trigger) index (index); dependent in the table stored procedure / function will be retained, but its status changes: invalid.

(7) the Delete statement DML (data maintain Language), this operation will be placed in the rollback segment, take effect until the transaction commits. If there is a corresponding tigger, when the execution will be triggered.

(. 8) TRUNCATE, drop a DLL (data define language), operations with immediate effect, the original data is not placed in the rollback segment, you can not roll back

(9) In the absence of a backup, the prudent use of drop and truncate. To remove some rows of data using delete and pay much attention to where the constraints of influence. Rollback segments should be large enough. To delete a table with drop; if you want to retain the table and delete the data in the table, if unrelated to the transaction, with truncate can be realized. If and related matters, or teachers want to trigger trigger, or a delete.

(10)  Truncate Table table is fast and efficient, because: 
TRUNCATE Table and DELETE statement WHERE clause is not functionally the same: both delete all rows in the table. But  TRUNCATE TABLE is faster than DELETE speed, and fewer system and transaction log resources used . DELETE statement deletes each row, and each row is a record deleted in the transaction log. TRUNCATE TABLE to delete data stored by the release of the data page table data used, and only release page recorded in the transaction log. 

(11)  All TRUNCATE TABLE to delete rows in the table, but the table structure and its columns, constraints, indexes and so on remain unchanged. New line count value reseeded identification used for that column. If you want to keep the identity count value, use DELETE. If you want to remove table definition and its data, use the DROP TABLE statement. 

(12)  to the table referenced by FOREIGN KEY constraint can not be used TRUNCATE TABLE, but should not use the DELETE statement WHERE clause. Because  TRUNCATE TABLE is not logged, it can not activate the trigger.

One, delete

. 1, is the DML delete, delete operation is performed, each row deleted from the table, and simultaneously records the row delete operation for rollback (ROLLBACK) and redo operations in the redo and undo table space, but Note that the table space is large enough, you need to manually submit (commit) operations to take effect, you can undo the operation by the rollback.

2, delete to delete the table according to the conditions to meet the conditions of the data, if you do not specify the where clause, then delete all records in the table.

3, delete statement does not affect the table occupied by the extent, high waterline (high watermark) remain unchanged original position.

二、truncate

1, truncate is DDL, implicitly submit, therefore, can not be rolled back, not fire the trigger.

2, truncate will delete all records in the table, and will be re-set the high-water line and all the indexes, by default minextents free up space to a extent, unless reuse storage ,. The log does not record, so the execution is very fast, but can not be undone by rollback operation (if accidentally truncate a table out, also can be restored, but can not rollback to restore).

3, for a key sheet (ForeignKey) constraint references can not be used truncate table, and use the delete statement without a where clause.

4, truncatetable not be used to participate in a table indexed view.

Three, drop

1, drop a DDL, implicitly submit, therefore, can not be rolled back, not fire the trigger.

2, drop statement to delete all table structure and data, and the release of all the space occupied by the table.

3, drop statement removes a table structure-dependent constraints, triggers, indexes, depending on the table stored procedure / function will be retained, but becomes invalid state.

 

总结:

1、在速度上,一般来说,drop> truncate > delete。

2、在使用drop和truncate时一定要注意,虽然可以恢复,但为了减少麻烦,还是要慎重。

3、如果想删除部分数据用delete,注意带上where子句,回滚段要足够大;

   如果想删除表,当然用drop; 

   如果想保留表而将所有数据删除,如果和事务无关,用truncate即可;

   如果和事务有关,或者想触发trigger,还是用delete;

   如果是整理表内部的碎片,可以用truncate跟上reuse stroage,再重新导入/插入数据。

 

 

Guess you like

Origin www.cnblogs.com/vegetableDD/p/11726513.html