这些性能提示补充了第 10.2.5.1 节“优化 INSERT”中快速插入的一般准则语句”。These performance tips supplement the general guidelines for fast inserts in Section 10.2.5.1, “Optimizing INSERT Statements”.
将数据导入
InnoDB时,关闭自动提交模式,因为它会为每次插入执行日志刷新到磁盘。要在导入操作期间禁用自动提交,请使用SET autocommit和COMMIT语句:When importing data intoInnoDB, turn off autocommit mode, because it performs a log flush to disk for every insert. To disable autocommit during your import operation, surround it withSET autocommitandCOMMITstatements:SET autocommit=0;
... SQL import statements ...COMMIT;mysqldump 选项
--opt创建转储文件,这些文件可以快速导入到InnoDB表,即使没有使用SET autocommit和COMMIT语句。The mysqldump option--optcreates dump files that are fast to import into anInnoDBtable, even without wrapping them with theSET autocommitandCOMMITstatements.如果对辅助键有
UNIQUE约束,则可以通过在导入会话期间暂时关闭唯一性检查来加快表导入速度:If you haveUNIQUEconstraints 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;对于大表,这可以节省大量磁盘 I/O,因为
InnoDB可以使用其更改缓冲区批量写入辅助索引记录。确保数据不包含重复键。For big tables, this saves a lot of disk I/O becauseInnoDBcan use its change buffer to write secondary index records in a batch. Be certain that the data contains no duplicate keys.如果您的表中有
FOREIGN KEY约束,您可以通过在导入会话期间关闭外键检查来加快表导入速度:If you haveFOREIGN KEYconstraints 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;对于大表,这可以节省大量磁盘 I/O。For big tables, this can save a lot of disk I/O.
如果需要插入多行,请使用多行
INSERT语法来减少客户端和服务器之间的通信开销:Use the multiple-rowINSERTsyntax to reduce communication overhead between the client and the server if you need to insert many rows:INSERT INTO yourtable VALUES (1,2), (5,5), ...;
此技巧对于插入到任何表中都有效,而不仅仅是
InnoDB表。This tip is valid for inserts into any table, not justInnoDBtables.当使用自动递增列,将
innodb_autoinc_lock_mode设置为 2(交错)而不是 1(连续)。有关详细信息,请参阅 第 17.6.1.6 节“InnoDB 中的 AUTO_INCRMENT 处理”。When doing bulk inserts into tables with auto-increment columns, setinnodb_autoinc_lock_modeto 2 (interleaved) instead of 1 (consecutive). See Section 17.6.1.6, “AUTO_INCREMENT Handling in InnoDB” for details.执行批量插入时,按
PRIMARY KEY顺序插入行会更快。InnoDB表使用聚集索引,这使得按照PRIMARY KEY的顺序使用数据相对较快。对于不完全适合缓冲池的表,按PRIMARY KEY顺序执行批量插入尤其重要。When performing bulk inserts, it is faster to insert rows inPRIMARY KEYorder.InnoDBtables use a clustered index, which makes it relatively fast to use data in the order of thePRIMARY KEY. Performing bulk inserts inPRIMARY KEYorder is particularly important for tables that do not fit entirely within the buffer pool.为了在将数据加载到
InnoDBFULLTEXT索引时获得最佳性能,请遵循以下一组步骤:For optimal performance when loading data into anInnoDBFULLTEXTindex, follow this set of steps:在创建表时定义列
FTS_DOC_IDtime,类型为BIGINT UNSIGNED NOT NULL,具有名为FTS_DOC_ID_INDEX的唯一索引。例如:Define a columnFTS_DOC_IDat table creation time, of typeBIGINT UNSIGNED NOT NULL, with a unique index namedFTS_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.
加载数据后创建
FULLTEXT索引。Create theFULLTEXTindex after the data is loaded.
NoteWhen adding
FTS_DOC_IDcolumn at table creation time, ensure that theFTS_DOC_IDcolumn is updated when theFULLTEXTindexed column is updated, as theFTS_DOC_IDmust increase monotonically with eachINSERT或者UPDATE. If you choose not to add theFTS_DOC_IDat table creation time and haveInnoDBmanage DOC IDs for you,InnoDBadds theFTS_DOC_IDas a hidden column with the nextCREATE FULLTEXT INDEXcall. This approach, however, requires a table rebuild which can impact performance.When addingFTS_DOC_IDcolumn at table creation time, ensure that theFTS_DOC_IDcolumn is updated when theFULLTEXTindexed column is updated, as theFTS_DOC_IDmust increase monotonically with eachINSERTorUPDATE. If you choose not to add theFTS_DOC_IDat table creation time and haveInnoDBmanage DOC IDs for you,InnoDBadds theFTS_DOC_IDas a hidden column with the nextCREATE FULLTEXT INDEXcall. This approach, however, requires a table rebuild which can impact performance.如果将数据加载到新 MySQL 实例中,请考虑使用
ALTER INSTANCE {ENABLE|DISABLE} INNODB REDO_LOG语法。禁用重做日志记录有助于避免重做日志写入,从而加快数据加载速度。有关详细信息,请参阅禁用重做日志记录。If loading data into a new MySQL instance, consider disabling redo logging usingALTER INSTANCE {ENABLE|DISABLE} INNODB REDO_LOGsyntax. Disabling redo logging helps speed up data loading by avoiding redo log writes. For more information, see Disabling Redo Logging.Warning此功能仅用于将数据加载到新的 MySQL 实例中。 请勿在生产系统上禁用重做日志记录。允许在禁用重做日志记录时关闭并重新启动服务器,但在禁用重做日志记录时服务器意外停止可能会导致数据丢失和实例损坏。This feature is intended only for loading data into a new MySQL instance. Do not disable redo logging on a production system. It is permitted to shutdown and restart the server while redo logging is disabled, but an unexpected server stoppage while redo logging is disabled can cause data loss and instance corruption.
使用 MySQL Shell 导入数据。 MySQL Shell 的并行表导入实用程序
util.importTable()为大型数据文件提供快速数据导入到 MySQL 关系表的功能。 MySQL Shell 的转储加载实用程序util.loadDump()还提供并行加载功能。请参阅 MySQL Shell 实用程序。Use MySQL Shell to import data. MySQL Shell's parallel table import utilityutil.importTable()provides rapid data import to a MySQL relational table for large data files. MySQL Shell's dump loading utilityutil.loadDump()also offers parallel load capabilities. See MySQL Shell Utilities.