如下摘自官方文檔:https://dev.mysql.com/doc/refman/5.7/en/insert.htmlphp
語法:html
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM [PARTITION (,...)] [WHERE ] [ORDER BY ...] [LIMIT ]
性能:
When you do not need to know the number of deleted rows, the statement is a faster way to empty a table than a statement with no clause. Unlike , cannot be used within a transaction or if you have a lock on the table. SeeSection 14.1.34, 「TRUNCATE TABLE Syntax」 and Section 14.3.5, 「LOCK TABLES and UNLOCK TABLES Syntax」.
簡單理解是:truncate 在不鎖表的狀況下,很快:
若是想用delete刪除快點:
The time required to delete individual rows in a table is exactly proportional to the number of indexes. To delete rows more quickly, you can increase the size of the key cache by increasing the system variable
能夠配置的key_buffer_size大小
多表刪除:
(1)不帶別名tbl_namepartition_namewhere_conditionrow_countTRUNCATE TABLEDELETEWHEREDELETETRUNCATE TABLEMyISAMkey_buffer_size
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
Or:mysql
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
(2)帶別名:必須寫別名:
If you declare an alias for a table, you must use the alias when referring to the table:sql
DELETE t1 FROM test AS t1, test2 WHERE ...
Correct:性能
DELETE a1, a2 FROM t1 AS a1 INNER JOIN t2 AS a2 WHERE a1.id=a2.id; DELETE FROM a1, a2 USING t1 AS a1 INNER JOIN t2 AS a2 WHERE a1.id=a2.id;
二、刪除mysql 中重複記錄,並保留一條
DELETE FROM `tb_phones` WHERE id NOT IN(SELECT * FROM(SELECT id FROM `tb_phones` GROUP BY phone)AS b);