truncate vs delete in mysql

1

Click here to load reader

Upload: md-biplob-hossain

Post on 22-Jan-2018

42 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Truncate vs Delete in MySQL

DELETE 1. DELETE is a DML Command. 2. DELETE statement is executed using a rowlock; each row in the table is locked for deletion. 3. We can specify filters in where clause 4. It deletes specified data if where condition exists. 5. Delete activates a trigger because the operations are logged individually. 6. Slower than truncate because, it keeps logs. 7. Rollback is possible. 8. Delete has no auto commit 9. Delete does not recover space 10.Delete can remove only some rows 11.Delete can be applied to tables and tables inside a cluser. 12.Delete does not affect the data object id 13.Flashback works across deletes 14.Delete can be granted on a table to another user or role 15.Delete generates a small amount of redo and a large amount of undo. 16.Delete does not. 17.Treatment with delete depends on the configuration of the foreign keys 18.Delete requires a shared table lock. 19.DML triggers fire on a truncate.

TRUNCATE 1. TRUNCATE is a DDL command. 2. TRUNCATE TABLE always locks the table and page but not each row. 3. Cannot use Where Condition. 4. It removes all the data. 5. TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row

deletions. 6. Faster in performance wise, because it doesn't keep any logs. 7. Rollback is not possible. 8. Truncate is autocommited (actually, there are two commits involved I believe) 9. Truncate recovers space (unless you use the REUSE STORAGE clause) 10.Truncate removes all rows except where used in a partitioning context. 11.Truncate applies only to tables or the entire cluster 12.Truncate assigns a new data object id unless there has never been an insert against the

table (even a single insert that is rolled back will cause a new data object id to be assigned).

13.Truncate prevents flashback operations to before the operation. 14.Truncate cannot be without using a DROP ANY TABLE grant. 15.Truncate generates a negligible amount of each. 16.Truncate operation renders unusable indexes usable again 17.A truncate cannot be applied when an enabled foreign key references the table. 18.Truncate requires an exclusive table lock 19.DML triggers do not fire on a truncate.

Truncate Delete

DDL command DML command

Cannot be rolled back Can be rolled back

Associated with commit Need to commit explicitly after

Will not fire trigger associated with the table Will fire trigger associated with the table

Cannot be used with where clause Where clause can be used

Reset the high water mark No association with high water mark

Generally executes faster than delete Generally executes slower than truncate

Cannot run truncate for a parent table with constraints Delete can be applied after deleting the child records