13.1.37 TRUNCATE TABLE Statement语句

TRUNCATE [TABLE] tbl_name

TRUNCATE TABLE empties a table completely. TRUNCATE TABLE完全清空表。It requires the DROP privilege. 它需要DROP特权。Logically, TRUNCATE TABLE is similar to a DELETE statement that deletes all rows, or a sequence of DROP TABLE and CREATE TABLE statements.从逻辑上讲,TRUNCATE TABLE类似于删除所有行的DELETE语句,或者类似于DROP TABLECREATE TABLE的语句序列。

To achieve high performance, TRUNCATE TABLE bypasses the DML method of deleting data. 为了获得高性能,TRUNCATE TABLE绕过了删除数据的DML方法。Thus, it does not cause ON DELETE triggers to fire, it cannot be performed for InnoDB tables with parent-child foreign key relationships, and it cannot be rolled back like a DML operation. 因此,它不会引发ON DELETE触发器,不能对具有父子外键关系的InnoDB表执行,也不能像DML操作那样回滚。However, TRUNCATE TABLE operations on tables that use an atomic DDL-supported storage engine are either fully committed or rolled back if the server halts during their operation. 但是,对使用原子DDL支持的存储引擎的表执行的TRUNCATE TABLE操作要么是完全提交的,要么是在操作期间服务器停止时回滚的。For more information, see Section 13.1.1, “Atomic Data Definition Statement Support”.有关更多信息,请参阅第13.1.1节,“原子数据定义语句支持”

Although TRUNCATE TABLE is similar to DELETE, it is classified as a DDL statement rather than a DML statement. 尽管TRUNCATE TABLE类似于DELETE,但它被分类为DDL语句而不是DML语句。It differs from DELETE in the following ways:它与DELETE的不同之处在于:

TRUNCATE TABLE is treated for purposes of binary logging and replication as DDL rather than DML, and is always logged as a statement.出于二进制日志记录和复制的目的,TRUNCATE TABLE被视为DDL而不是DML,并且总是作为语句记录。

TRUNCATE TABLE for a table closes all handlers for the table that were opened with HANDLER OPEN.针对表的TRUNCATE TABLE关闭在HANDLER OPEN中打开的表的所有处理程序。

In MySQL 5.7 and earlier, on a system with a large buffer pool and innodb_adaptive_hash_index enabled, a TRUNCATE TABLE operation could cause a temporary drop in system performance due to an LRU scan that occurred when removing the table's adaptive hash index entries (Bug #68184). 在MySQL5.7及更早版本中,在启用了大型缓冲池和innodb_adaptive_hash_index的系统上,由于删除表的自适应哈希索引项时发生的LRU扫描,TRUNCATE TABLE操作可能会导致系统性能临时下降(错误Š68184)。The remapping of TRUNCATE TABLE to DROP TABLE and CREATE TABLE in MySQL 8.0 avoids the problematic LRU scan.MySQL 8.0中TRUNCATE TABLEDROP TABLECREATE TABLE的重新映射避免了有问题的LRU扫描。

TRUNCATE TABLE can be used with Performance Schema summary tables, but the effect is to reset the summary columns to 0 or NULL, not to remove rows. TRUNCATE TABLE可用于性能架构摘要表,但其效果是将摘要列重置为0或NULL,而不是删除行。See Section 27.12.20, “Performance Schema Summary Tables”.请参阅第27.12.20节,“性能模式汇总表”

Truncating an InnoDB table that resides in a file-per-table tablespace drops the existing tablespace and creates a new one. 截断驻留在每个表空间的文件中的InnoDB表会删除现有的表空间并创建一个新的表空间。As of MySQL 8.0.21, if the tablespace was created with an earlier version and resides in an unknown directory, InnoDB creates the new tablespace in the default location and writes the following warning to the error log: 从MySQL 8.0.21开始,如果表空间是用早期版本创建的,并且驻留在未知目录中,InnoDB将在默认位置创建新表空间,并将以下警告写入错误日志:The DATA DIRECTORY location must be in a known directory. 数据目录位置必须在已知目录中。The DATA DIRECTORY location will be ignored and the file will be put into the default datadir location. 数据目录位置将被忽略,文件将放入默认的datadir位置。Known directories are those defined by the datadir, innodb_data_home_dir, and innodb_directories variables. 已知目录是由datadirinnodb_data_home_dirinnodb_directories变量定义的目录。To have TRUNCATE TABLE create the tablespace in its current location, add the directory to the innodb_directories setting before running TRUNCATE TABLE.要让TRUNCATE TABLE在其当前位置创建表空间,请在运行TRUNCATE TABLE之前将目录添加到innodb_directories设置中。