表和索引上的许多 DDL 操作(
CREATE、ALTER和DROP语句)可以在线执行。有关详细信息,请参阅第 17.12 节“InnoDB 和在线 DDL”。Many DDL operations on tables and indexes (CREATE,ALTER, andDROPstatements) can be performed online. See Section 17.12, “InnoDB and Online DDL” for details.在线 DDL 支持添加二级索引,这意味着您通常可以通过创建不带二级索引的表,然后在加载数据后添加二级索引来加快创建和加载表及关联索引的过程。Online DDL support for adding secondary indexes means that you can generally speed up the process of creating and loading a table and associated indexes by creating the table without secondary indexes, then adding secondary indexes after the data is loaded.
使用
TRUNCATE TABLE清空表,而不是DELETE FROM tbl_name。外键约束可以使TRUNCATE语句像常规DELETE语句一样工作,在这种情况下,一系列命令如DROP TABLE和CREATE TABLE可能是最快的。UseTRUNCATE TABLEto empty a table, notDELETE FROM. Foreign key constraints can make atbl_nameTRUNCATEstatement work like a regularDELETEstatement, in which case a sequence of commands likeDROP TABLEandCREATE TABLEmight be fastest.由于主键是每个
InnoDB表的存储布局不可或缺的一部分,并且更改主键的定义涉及重新组织整个表,因此始终将主键设置为CREATE TABLE语句,并提前计划,以便之后不需要ALTER或DROP主键。Because the primary key is integral to the storage layout of eachInnoDBtable, and changing the definition of the primary key involves reorganizing the whole table, always set up the primary key as part of theCREATE TABLEstatement, and plan ahead so that you do not need toALTERorDROPthe primary key afterward.