Empty mysql table data in two ways and the difference

In MySQL delete data in two ways:

1, truncate (truncated) belonging gross type Empty

2, delete part of the refinement of the deleted

Deletion

If you need to clear the table of all the data, the following two are available:

delete from tablename;
truncate table tablename;

And if you just delete the part of the data, you can only use the delete:

delete from tablename where case1 and case2;

the difference

When refined delete some data, only delete.

And when all the empty tables of data, both of which can, at this time there is a certain difference between the two ways:

1, the return value

truncate the return value is 0, and delete returns the number of records deleted

mysql> truncate serviceHost;
Query OK, 0 rows affected (0.04 sec)
mysql> delete from serviceHost where creator='test';
Query OK, 4 rows affected (0.01 sec)

2, increment field

If the table has a maximum increment field, TRUNCATE reset to 1, and the delete will remain increment.

3, efficiency

truncate table does not scan, equivalent to re-create the table, leaving only the structure of the table, and then delete the original table, the efficiency is very high.
delete full table scans, to make a judgment based on where clause and therefore inefficient.

4, the operation log

truncate the server logs do not write, can not be recovered.
delete write server log.

5, flip-flop

truncate does not activate the trigger, delete activates the trigger.

Published 28 original articles · won praise 20 · views 6044

Guess you like

Origin blog.csdn.net/monkeyapi/article/details/103645164