8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

10.11.3 并发插入10.11.3 Concurrent Inserts

MyISAM 存储引擎支持并发插入,以减少特定表的读写之间的竞争:如果一个 MyISAM 表的数据文件中没有空洞(中间没有被删除的行),则可以执行 INSERT 语句将行添加到表的末尾,同时 SELECT 语句可以从表中读取行。如果有多个 INSERT 语句,它们会排队并依次执行,同时与 SELECT 语句并发执行。并发 INSERT 的结果可能不会立即可见。The MyISAM storage engine supports concurrent inserts to reduce contention between readers and writers for a given table: If a MyISAM table has no holes in the data file (deleted rows in the middle), an INSERT statement can be executed to add rows to the end of the table at the same time that SELECT statements are reading rows from the table. If there are multiple INSERT statements, they are queued and performed in sequence, concurrently with the SELECT statements. The results of a concurrent INSERT may not be visible immediately.

concurrent_insert 系统变量可以设置以修改并发插入处理。默认情况下,该变量设置为 AUTO(或 1),并发插入将按上述方式处理。如果 concurrent_insert 设置为 NEVER(或 0),则禁用并发插入。如果该变量设置为 ALWAYS(或 2),即使表中存在已删除的行,也允许在表的末尾进行并发插入。另请参阅 concurrent_insert 系统变量的描述。The concurrent_insert system variable can be set to modify the concurrent-insert processing. By default, the variable is set to AUTO (or 1) and concurrent inserts are handled as just described. If concurrent_insert is set to NEVER (or 0), concurrent inserts are disabled. If the variable is set to ALWAYS (or 2), concurrent inserts at the end of the table are permitted even for tables that have deleted rows. See also the description of the concurrent_insert system variable.

如果您正在使用二进制日志,对于 CREATE ... SELECT 或 INSERT ... SELECT 语句,同时插入会被转换为普通插入。这样做是为了确保在备份操作期间应用日志时,您可以重新创建表的精确副本。参见 第 7.4.4 节,“二进制日志”。此外,对于这些语句,会对被选中的表施加读锁,以阻止向该表的插入。其效果是,该表的同时插入也必须等待。If you are using the binary log, concurrent inserts are converted to normal inserts for CREATE ... SELECT or INSERT ... SELECT statements. This is done to ensure that you can re-create an exact copy of your tables by applying the log during a backup operation. See Section 7.4.4, “The Binary Log”. In addition, for those statements a read lock is placed on the selected-from table such that inserts into that table are blocked. The effect is that concurrent inserts for that table must wait as well.

使用 LOAD DATA 时,如果你在一个 MyISAM 表上指定了 CONCURRENT,且该表满足并发插入的条件(即中间没有空闲块),其他会话在 LOAD DATA 执行期间仍然可以从表中检索数据。即使没有其他会话同时使用该表,使用 CONCURRENT 选项也会稍微影响 LOAD DATA 的性能。With LOAD DATA, if you specify CONCURRENT with a MyISAM table that satisfies the condition for concurrent inserts (that is, it contains no free blocks in the middle), other sessions can retrieve data from the table while LOAD DATA is executing. Use of the CONCURRENT option affects the performance of LOAD DATA a bit, even if no other session is using the table at the same time.

如果您指定HIGH_PRIORITY,它会覆盖如果服务器是使用该选项启动的情况下--low-priority-updates选项的效果。它还会导致不使用并发插入。If you specify HIGH_PRIORITY, it overrides the effect of the --low-priority-updates option if the server was started with that option. It also causes concurrent inserts not to be used.

对于LOCK TABLE来说,READ LOCAL和READ之间的区别在于,READ LOCAL允许在持有锁期间执行不冲突的INSERT语句(并发插入)。然而,如果您在持有锁的同时打算使用服务器外部的进程操作数据库,则不能使用这一点。For LOCK TABLE, the difference between READ LOCAL and READ is that READ LOCAL permits nonconflicting INSERT statements (concurrent inserts) to execute while the lock is held. However, this cannot be used if you are going to manipulate the database using processes external to the server while you hold the lock.