These performance tips supplement the general guidelines for fast inserts in Section 8.2.5.1, “Optimizing INSERT Statements”.这些性能提示补充了第8.2.5.1节,“优化INSERT语句”中有关快速插入的一般指南。
When importing data into 将数据导入InnoDB
, turn off autocommit mode, because it performs a log flush to disk for every insert. InnoDB
时,请关闭自动提交模式,因为它会对每次插入执行磁盘日志刷新。To disable autocommit during your import operation, surround it with 要在导入操作期间禁用自动提交,请使用SET autocommit
and COMMIT
statements:SET autocommit
和COMMIT
语句:
SET autocommit=0;
... SQL import statements ...
COMMIT;
The mysqldump option --opt
creates dump files that are fast to import into an InnoDB
table, even without wrapping them with the SET autocommit
and COMMIT
statements.mysqldump
选项--opt
创建的转储文件可以快速导入到InnoDB
表中,甚至不用SET autocommit
和COMMIT
语句包装它们。
If you have 如果对辅助键有唯一性约束,则可以通过在导入会话期间临时关闭唯一性检查来加快表导入的速度:UNIQUE
constraints on secondary keys, you can speed up table imports by temporarily turning off the uniqueness checks during the import session:
SET unique_checks=0;
... SQL import statements ...
SET unique_checks=1;
For big tables, this saves a lot of disk I/O because 对于大型表,这会节省大量磁盘I/O,因为InnoDB
can use its change buffer to write secondary index records in a batch. InnoDB
可以使用其更改缓冲区批量写入二级索引记录。Be certain that the data contains no duplicate keys.确保数据不包含重复的密钥。
If you have 如果表中有外键约束,可以通过在导入会话期间关闭外键检查来加快表导入的速度:FOREIGN KEY
constraints in your tables, you can speed up table imports by turning off the foreign key checks for the duration of the import session:
SET foreign_key_checks=0;
... SQL import statements ...
SET foreign_key_checks=1;
For big tables, this can save a lot of disk I/O.对于大型表,这可以节省大量磁盘I/O。
Use the multiple-row 如果需要插入多行,请使用多行INSERT
syntax to reduce communication overhead between the client and the server if you need to insert many rows:INSERT
语法来减少客户端和服务器之间的通信开销:
INSERT INTO yourtable VALUES (1,2), (5,5), ...;
This tip is valid for inserts into any table, not just 此技巧适用于插入任何表,而不仅仅是InnoDB
tables.InnoDB
表。
When doing bulk inserts into tables with auto-increment columns, set 在对具有自动增量列的表执行大容量插入时,将innodb_autoinc_lock_mode
to 2 (interleaved) instead of 1 (consecutive). innodb_autoinc_lock_mode
设置为2(交错),而不是1(连续)。See Section 15.6.1.6, “AUTO_INCREMENT Handling in InnoDB” for details.详见第15.6.1.6节,“InnoDB
中的自动增量处理”。
When performing bulk inserts, it is faster to insert rows in 执行大容量插入时,按主键顺序插入行会更快。PRIMARY KEY
order. InnoDB
tables use a clustered index, which makes it relatively fast to use data in the order of the PRIMARY KEY
. InnoDB
表使用聚集索引,这使得按主键顺序使用数据的速度相对较快。Performing bulk inserts in 对于不完全适合缓冲池的表,按主键顺序执行大容量插入尤其重要。PRIMARY KEY
order is particularly important for tables that do not fit entirely within the buffer pool.
For optimal performance when loading data into an 为了在将数据加载到InnoDB
FULLTEXT
index, follow this set of steps:InnoDB
全文索引时获得最佳性能,请执行以下步骤:
Define a column 在表创建时定义一个列FTS_DOC_ID
at table creation time, of type BIGINT UNSIGNED NOT NULL
, with a unique index named FTS_DOC_ID_INDEX
. FTS_DOC_ID
,类型为BIGINT UNSIGNED NOT NULL
,唯一索引名为FTS_DOC_ID_INDEX
。For example:例如:
CREATE TABLE t1 ( FTS_DOC_ID BIGINT unsigned NOT NULL AUTO_INCREMENT, title varchar(255) NOT NULL DEFAULT '', text mediumtext NOT NULL, PRIMARY KEY (`FTS_DOC_ID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE UNIQUE INDEX FTS_DOC_ID_INDEX on t1(FTS_DOC_ID);
Load the data into the table.将数据加载到表中。
Create the 加载数据后创建全文索引。FULLTEXT
index after the data is loaded.
When adding 在创建表时添加FTS_DOC_ID
column at table creation time, ensure that the FTS_DOC_ID
column is updated when the FULLTEXT
indexed column is updated, as the FTS_DOC_ID
must increase monotonically with each INSERT
or UPDATE
. FTS_DOC_ID
列时,请确保在更新全文索引列时更新FTS_DOC_ID
列,因为FTS_DOC_ID
必须随着每次插入或更新而单调增加。If you choose not to add the 如果您选择不在表创建时添加FTS_DOC_ID
at table creation time and have InnoDB
manage DOC IDs for you, InnoDB
adds the FTS_DOC_ID
as a hidden column with the next CREATE FULLTEXT INDEX
call. FTS_DOC_ID
,并让InnoDB
为您管理文档ID,InnoDB
将FTS_DOC_ID
作为隐藏列添加到下一次创建全文索引调用中。This approach, however, requires a table rebuild which can impact performance.然而,这种方法需要重建表,这可能会影响性能。