8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 10 优化 / 10.6 MyISAM 表的优化 / 10.6.3 优化 REPAIR TABLE 语句
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 67%

10.6.3 优化 REPAIR TABLE 语句10.6.3 Optimizing REPAIR TABLE Statements

REPAIR TABLE 对于 MyISAM 表来说,类似于使用 myisamchk 进行修复操作,并且某些相同的性能优化也适用:REPAIR TABLE for MyISAM tables is similar to using myisamchk for repair operations, and some of the same performance optimizations apply:

  • myisamchk 有控制内存分配的变量。您可以通过设置这些变量来提高其性能,如在 第 6.6.4.6 节,“myisamchk 内存使用” 中所描述的那样。myisamchk has variables that control memory allocation. You may be able to its improve performance by setting these variables, as described in Section 6.6.4.6, “myisamchk Memory Usage”.

  • 对于 REPAIR TABLE,同样的原则适用,但由于修复是由服务器执行的,因此您需要设置服务器系统变量,而不是 myisamchk 变量。此外,除了设置内存分配变量外,增加 myisam_max_sort_file_size 系统变量可以提高修复使用更快的文件排序方法的可能性,从而避免使用较慢的键缓存方法进行修复。在设置该变量为系统允许的最大文件大小之前,请先检查是否有足够的空闲空间来保存表文件的副本。该空闲空间必须存在于包含原始表文件的文件系统中。For REPAIR TABLE, the same principle applies, but because the repair is done by the server, you set server system variables instead of myisamchk variables. Also, in addition to setting memory-allocation variables, increasing the myisam_max_sort_file_size system variable increases the likelihood that the repair uses the faster filesort method and avoids the slower repair by key cache method. Set the variable to the maximum file size for your system, after checking to be sure that there is enough free space to hold a copy of the table files. The free space must be available in the file system containing the original table files.

假设使用以下选项设置其内存分配变量来执行一个 myisamchk 表修复操作:Suppose that a myisamchk table-repair operation is done using the following options to set its memory-allocation variables:

--key_buffer_size=128M --myisam_sort_buffer_size=256M
--read_buffer_size=64M --write_buffer_size=64M

这些 myisamchk 变量中的一些对应于服务器系统变量:Some of those myisamchk variables correspond to server system variables:

myisamchk 变量myisamchk Variable 系统变量System Variable
key_buffer_size key_buffer_size
myisam_sort_buffer_size myisam_sort_buffer_size
read_buffer_size read_buffer_size
write_buffer_size 无none

每个服务器系统变量都可以在运行时设置,其中一些变量(myisam_sort_buffer_size,read_buffer_size)除了全局值外还有会话值。设置会话值会将更改的影响限制在当前会话内,而不会影响其他用户。更改仅全局变量(key_buffer_size,myisam_max_sort_file_size)也会影响其他用户。对于key_buffer_size,必须考虑缓冲区是与其他用户共享的。例如,如果您将myisamchk key_buffer_size变量设置为 128MB,您可以将相应的key_buffer_size系统变量设置得更大(如果尚未设置更大),以允许其他会话的活动使用键缓冲区。然而,更改全局键缓冲区大小会使缓冲区失效,从而导致其他会话的磁盘 I/O 增加和性能下降。避免此问题的一个替代方法是使用单独的键缓冲区,将其分配给要修复的表的索引,并在修复完成后释放它。参见第 10.10.2.2 节,“多重键缓冲区”。Each of the server system variables can be set at runtime, and some of them (myisam_sort_buffer_size, read_buffer_size) have a session value in addition to a global value. Setting a session value limits the effect of the change to your current session and does not affect other users. Changing a global-only variable (key_buffer_size, myisam_max_sort_file_size) affects other users as well. For key_buffer_size, you must take into account that the buffer is shared with those users. For example, if you set the myisamchk key_buffer_size variable to 128MB, you could set the corresponding key_buffer_size system variable larger than that (if it is not already set larger), to permit key buffer use by activity in other sessions. However, changing the global key buffer size invalidates the buffer, causing increased disk I/O and slowdown for other sessions. An alternative that avoids this problem is to use a separate key cache, assign to it the indexes from the table to be repaired, and deallocate it when the repair is complete. See Section 10.10.2.2, “Multiple Key Caches”.

根据前述说明,可以进行 REPAIR TABLE 操作,以使用与 myisamchk 命令类似的设置。在这里,会分配一个单独的 128MB 键缓冲区,并假定文件系统允许至少 100GB 的文件大小。Based on the preceding remarks, a REPAIR TABLE operation can be done as follows to use settings similar to the myisamchk command. Here a separate 128MB key buffer is allocated and the file system is assumed to permit a file size of at least 100GB.

SET SESSION myisam_sort_buffer_size = 256*1024*1024;
SET SESSION read_buffer_size = 64*1024*1024;
SET GLOBAL myisam_max_sort_file_size = 100*1024*1024*1024;
SET GLOBAL repair_cache.key_buffer_size = 128*1024*1024;
CACHE INDEX tbl_name IN repair_cache;
LOAD INDEX INTO CACHE tbl_name;
REPAIR TABLE tbl_name ;
SET GLOBAL repair_cache.key_buffer_size = 0;

如果您打算更改全局变量,但希望只在 REPAIR TABLE 操作期间更改,以最小化对其他用户的影响,可将其值保存在用户变量中,然后在操作后恢复。例如:If you intend to change a global variable but want to do so only for the duration of a REPAIR TABLE operation to minimally affect other users, save its value in a user variable and restore it afterward. For example:

SET @old_myisam_sort_buffer_size = @@GLOBAL.myisam_max_sort_file_size;
SET GLOBAL myisam_max_sort_file_size = 100*1024*1024*1024;
REPAIR TABLE tbl_name ;
SET GLOBAL myisam_max_sort_file_size = @old_myisam_max_sort_file_size;

可以在服务器启动时全局设置影响REPAIR TABLE的系统变量,以便默认情况下生效。例如,将以下行添加到服务器的my.cnf文件中:The system variables that affect REPAIR TABLE can be set globally at server startup if you want the values to be in effect by default. For example, add these lines to the server my.cnf file:

[mysqld]
myisam_sort_buffer_size=256M
key_buffer_size=1G
myisam_max_sort_file_size=100G

这些设置不包括read_buffer_size。将read_buffer_size全局设置为较大值会对所有会话生效,并可能因服务器在多会话同时运行时分配过多内存而导致性能下降。These settings do not include read_buffer_size. Setting read_buffer_size globally to a large value does so for all sessions and can cause performance to suffer due to excessive memory allocation for a server with many simultaneous sessions.