文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

17.11.2 文件空间管理17.11.2 File Space Management

您在配置文件中使用 innodb_data_file_path 配置选项定义的数据文件组成了 InnoDB 系统表空间。这些文件在逻辑上被连接以形成系统表空间。没有使用条带化。您无法定义表在系统表空间中的分配位置。在新创建的系统表空间中,InnoDB 从第一个数据文件开始分配空间。The data files that you define in the configuration file using the innodb_data_file_path configuration option form the InnoDB system tablespace. The files are logically concatenated to form the system tablespace. There is no striping in use. You cannot define where within the system tablespace your tables are allocated. In a newly created system tablespace, InnoDB allocates space starting from the first data file.

为了避免将所有表和索引存储在系统表空间中所带来的问题,您可以启用 innodb_file_per_table 配置选项(默认设置),该选项会将每个新创建的表存储在单独的表空间文件中(扩展名为 .ibd)。以这种方式存储的表,磁盘文件内的碎片较少,并且当表被截断时,空间会返回给操作系统,而不是仍由 InnoDB 在系统表空间中保留。有关更多信息,请参阅 第 17.6.3.2 节,“单表表空间”。To avoid the issues that come with storing all tables and indexes inside the system tablespace, you can enable the innodb_file_per_table configuration option (the default), which stores each newly created table in a separate tablespace file (with extension .ibd). For tables stored this way, there is less fragmentation within the disk file, and when the table is truncated, the space is returned to the operating system rather than still being reserved by InnoDB within the system tablespace. For more information, see Section 17.6.3.2, “File-Per-Table Tablespaces”.

您也可以将表存储在通用表空间中。通用表空间是使用CREATE TABLESPACE语法创建的共享表空间。它们可以创建在 MySQL 数据目录之外,能够容纳多个表,并支持所有行格式的表。更多信息,请参见第 17.6.3.3 节,“通用表空间”。You can also store tables in general tablespaces. General tablespaces are shared tablespaces created using CREATE TABLESPACE syntax. They can be created outside of the MySQL data directory, are capable of holding multiple tables, and support tables of all row formats. For more information, see Section 17.6.3.3, “General Tablespaces”.

页、区段、段和表空间Pages, Extents, Segments, and Tablespaces

每个表空间由数据库页组成。MySQL 实例中的每个表空间都有相同的页大小。默认情况下,所有表空间的页大小为 16KB;你可以在创建 MySQL 实例时通过指定innodb_page_size选项将页大小减小到 8KB 或 4KB。你也可以将页大小增加到 32KB 或 64KB。更多信息,请参考innodb_page_size文档。Each tablespace consists of database pages. Every tablespace in a MySQL instance has the same page size. By default, all tablespaces have a page size of 16KB; you can reduce the page size to 8KB or 4KB by specifying the innodb_page_size option when you create the MySQL instance. You can also increase the page size to 32KB or 64KB. For more information, refer to the innodb_page_size documentation.

页面被分组到大小为 1MB 的区段中,适用于最多 16KB 的页面(64 个连续的 16KB 页面,或 128 个 8KB 页面,或 256 个 4KB 页面)。对于 32KB 的页面,区段大小为 2MB。对于 64KB 的页面,区段大小为 4MB。表空间中的“文件”在InnoDB中被称为段。(这些段不同于回滚段,后者实际上包含许多表空间段。)The pages are grouped into extents of size 1MB for pages up to 16KB in size (64 consecutive 16KB pages, or 128 8KB pages, or 256 4KB pages). For a page size of 32KB, extent size is 2MB. For page size of 64KB, extent size is 4MB. The “files” inside a tablespace are called segments in InnoDB. (These segments are different from the rollback segment, which actually contains many tablespace segments.)

当一个段在表空间中增长时,InnoDB 会一次分配前 32 页。之后,InnoDB 开始将整个区段分配给该段。InnoDB 可以一次向一个大段添加最多 4 个区段,以确保数据的良好顺序性。When a segment grows inside the tablespace, InnoDB allocates the first 32 pages to it one at a time. After that, InnoDB starts to allocate whole extents to the segment. InnoDB can add up to 4 extents at a time to a large segment to ensure good sequentiality of data.

在 InnoDB 中,每个索引分配两个段。一个用于 B 树 的非叶子节点,另一个用于叶子节点。在磁盘上保持叶子节点的连续性可以实现更好的顺序 I/O 操作,因为这些叶子节点包含实际的表数据。Two segments are allocated for each index in InnoDB. One is for nonleaf nodes of the B-tree, the other is for the leaf nodes. Keeping the leaf nodes contiguous on disk enables better sequential I/O operations, because these leaf nodes contain the actual table data.

表空间中的一些页面包含其他页面的位图,因此 InnoDB 表空间中的少数区段不能作为整体分配给段,只能作为单独的页面分配。Some pages in the tablespace contain bitmaps of other pages, and therefore a few extents in an InnoDB tablespace cannot be allocated to segments as a whole, but only as individual pages.

当您通过执行 SHOW TABLE STATUS 语句来查询表空间中可用的空闲空间时,InnoDB 会报告表空间中明确空闲的区段。InnoDB 总是为清理和其他内部用途保留一些区段;这些保留的区段不计入空闲空间。When you ask for available free space in the tablespace by issuing a SHOW TABLE STATUS statement, InnoDB reports the extents that are definitely free in the tablespace. InnoDB always reserves some extents for cleanup and other internal purposes; these reserved extents are not included in the free space.

当您从表中删除数据时,InnoDB 会收缩相应的 B 树索引。释放的空间是否可以被其他用户使用取决于删除模式是释放单个页还是释放表空间的范围。删除表或删除表中的所有行保证可以将空间释放给其他用户,但请记住,删除的行只有在 清理 操作中才会被物理移除,该操作会在它们不再用于事务回滚或一致性读取后自动进行。(见 第 17.3 节,“InnoDB 多版本控制”。)When you delete data from a table, InnoDB contracts the corresponding B-tree indexes. Whether the freed space becomes available for other users depends on whether the pattern of deletes frees individual pages or extents to the tablespace. Dropping a table or deleting all rows from it is guaranteed to release the space to other users, but remember that deleted rows are physically removed only by the purge operation, which happens automatically some time after they are no longer needed for transaction rollbacks or consistent reads. (See Section 17.3, “InnoDB Multi-Versioning”.)

配置保留文件段页的百分比Configuring the Percentage of Reserved File Segment Pages

innodb_segment_reserve_factor 变量允许定义表空间文件段页保留为空页的百分比。保留一定比例的页用于未来增长,以便 B 树中的页能够连续分配。修改保留页的百分比的能力可以微调 InnoDB,以解决数据碎片化或存储空间利用效率低下的问题。The innodb_segment_reserve_factor variablepermits defining the percentage of tablespace file segment pages reserved as empty pages. A percentage of pages are reserved for future growth so that pages in the B-tree can be allocated contiguously. The ability to modify the percentage of reserved pages permits fine-tuning InnoDB to address issues of data fragmentation or inefficient use of storage space.

该设置适用于每表文件和通用表空间。innodb_segment_reserve_factor 的默认设置为 12.5%。The setting is applicable to file-per-table and general tablespaces. The innodb_segment_reserve_factor default setting is 12.5 percent.

innodb_segment_reserve_factor 变量是动态的,可以使用 SET 语句进行配置。例如:The innodb_segment_reserve_factor variable is dynamic and can be configured using a SET statement. For example:

mysql> SET GLOBAL innodb_segment_reserve_factor=10;

页面与表行的关系How Pages Relate to Table Rows

对于 4KB、8KB、16KB 和 32KB 的 innodb_page_size 设置,最大行长度略小于数据库页面大小的一半。例如,对于默认的 16KB InnoDB 页面大小,最大行长度略小于 8KB。对于 64KB 的 innodb_page_size 设置,最大行长度略小于 16KB。For for 4KB, 8KB, 16KB, and 32KB innodb_page_size settings, the maximum row length is slightly less than half a database page size. For example, the maximum row length is slightly less than 8KB for the default 16KB InnoDB page size. For a 64KB innodb_page_size setting, the maximum row length is slightly less than 16KB.

如果一行没有超过最大行长度,则整个行都存储在页面的本地。如果一行超过了最大行长度,可变长度列将被选择用于外部页面存储,直到该行符合最大行长度限制。可变长度列的外部页面存储因行格式而异:If a row does not exceed the maximum row length, all of it is stored locally within the page. If a row exceeds the maximum row length, variable-length columns are chosen for external off-page storage until the row fits within the maximum row length limit. External off-page storage for variable-length columns differs by row format:

  • COMPACT 和 REDUNDANT 行格式COMPACT and REDUNDANT Row Formats

    当为外部页存储选择可变长度列时,InnoDB 会将前 768 字节存储在行本地,其余部分存储到溢出页中。每个这样的列都有自己的溢出页列表。768 字节的前缀附带一个 20 字节的值,该值存储列的实际长度,并指向溢出列表中存储其余值的位置。参见 第 17.10 节,“InnoDB 行格式”。When a variable-length column is chosen for external off-page storage, InnoDB stores the first 768 bytes locally in the row, and the rest externally into overflow pages. Each such column has its own list of overflow pages. The 768-byte prefix is accompanied by a 20-byte value that stores the true length of the column and points into the overflow list where the rest of the value is stored. See Section 17.10, “InnoDB Row Formats”.

  • DYNAMIC 和 COMPRESSED 行格式DYNAMIC and COMPRESSED Row Formats

    当为外部页存储选择可变长度列时,InnoDB 会在行本地存储一个 20 字节的指针,其余部分存储到溢出页中。参见 第 17.10 节,“InnoDB 行格式”。When a variable-length column is chosen for external off-page storage, InnoDB stores a 20-byte pointer locally in the row, and the rest externally into overflow pages. See Section 17.10, “InnoDB Row Formats”.

LONGBLOB 和 LONGTEXT 列必须小于 4GB,并且总行长度,包括 BLOB 和 TEXT 列,必须小于 4GB。LONGBLOB and LONGTEXT columns must be less than 4GB, and the total row length, including BLOB and TEXT columns, must be less than 4GB.