truncate和 delete的區別:

一、一個表要刪除所有數據如何實現:①delete,②truncatemysql

EG:刪除a表中的全部數據:sql

CREATE TABLE `b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16711412 DEFAULT CHARSET=utf8;性能

快速導入數據:測試

insert into a(name) values('qcj');
insert into a(name) values('qcj_a');
insert into a(name) values('qcj_b');日誌

insert into a(name) select name from a;orm

mysql> insert into a(name) select name from a;
Query OK, 25165824 rows affected (32 min 53.16 sec)
Records: 25165824 Duplicates: 0 Warnings:ci

將a表從新複製一份brem

測試:io

複製表a,delete和truncate分別刪除表測試:table

mysql> truncate table b;
Query OK, 0 rows affected (5.79 sec)

mysql> delete from a;
Query OK, 50331648 rows affected (32 min 29.32 sec)

show tables status like 'table_name'

mysql> show table status like 'a'\G;
*************************** 1. row ***************************
Name: a
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 1509949440
Max_data_length: 0
Index_length: 0
Data_free: 335544320
Auto_increment: 50920682
Create_time: 2016-03-23 14:44:34
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> show table status like 'b'\G;
*************************** 1. row ***************************
Name: b
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 1
Create_time: 2016-03-23 15:22:55
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)

ERROR:
No query specified

 

區別:

1.delete 刪除表後,會存在碎片 而且 刪除後,不會自增ID號的派發

 truncate 速度快,而且自增id 會被初始化 

2.truncate只能一次清空,不能按條件刪除。可是delete能夠按條件清除部分記錄。 3.truncate清空數據表性能(速度)比delete快。 4.truncate不會記錄到系統日誌,不會觸發delete觸發器

相關文章
相關標籤/搜索