Mysql difference of drop, truncate and delete

Today, suddenly thought of a problem when finishing mysql database notes, is the drop, the difference between truncate and delete, at first glance there are three functions deleted, but the specific point of view there are still many differences. I leave that role briefly about the three, there is the distinction between the older generation has them in detail sorted out, end of the article there is a link, check yourself.

delete


  1. delete a DML (data maintain language), this operation will be placed in the rollback segment, not take effect until the transaction commits, if there is a corresponding trigger trigger, then the time of execution can be triggered
  2. When the delete operation, each row deleted from the table, delete the row and simultaneously recording operation for rollback (ROLLBACK) in the redo and undo and redo operations tablespace, 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.
  3. 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.
  4. delete statement does not affect the extent occupied by the table (that is, in the area of the table structure), high waterline (high watermark) remain unchanged original position. (Present in the high-water line in paragraph (segment), which is used at the junction between the two data blocks with the unused data blocks identified segment has been used, the scan table data when the high-water mark of all data blocks must be scanned.) (about the innodb table structure, you can refer to this blog )

truncate


  1. truncate is DDL (data define language) That will take immediate effect, the original data is not placed rollback segment can not be rolled back, it will not trigger flip-flop

  2. truncate table deletes all records and reset the high water line and all the indexes, by default to the extent minextents of free space (that is, the area within the structure of the table in paragraph), unless reuse storage (use this words, extent where the space will not be recovered, it only removes the data out, freespace space after deleting data, only for the use of this table, the other can not be used). 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 Table Foreign Key (foreign key) constraint references can not be used truncate table (being given (Can not truncate a table referenced in a foreign key constraint)), you can not use the drop table (being given (Can not delete or update a parent row: a foreign key constraint fails)), but should use the delete statement with no where clause.
  4. truncate table can not be used involved table index view.

drop


  1. drop is 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. delete statement drop table structure dependent constraints, triggers, indexes, depending on the table stored procedure / function will be retained, but becomes invalid state.

to sum up


  1. In terms of speed, in general, drop> truncate> delete.
  2. You must pay attention when using drop and truncate, although can be restored, but in order to reduce trouble, still have to be careful.
  3. If you want to delete some data delete, to bring attention to the where clause, rollback segments should be large enough; if you want to delete the table, of course, with a drop;
  4. If you want to keep all the data tables will be deleted, and if the transaction is independent (not roll back), you can use truncate;
  5. If and matters relating to, or want to trigger the trigger, or use the delete;
  6. If it is inside finishing table fragmentation, you can truncate keep reuse stroage, reintroduced into / insert the data.

Difference drop, truncate and delete

Guess you like

Origin www.cnblogs.com/michealjy/p/11596963.html