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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 17 InnoDB 存储引擎 / 17.15 InnoDB INFORMATION_SCHEMA 表 / 17.15.3 InnoDB INFORMATION_SCHEMA 模式对象表
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 83%

17.15.3 InnoDB INFORMATION_SCHEMA 模式对象表17.15.3 InnoDB INFORMATION_SCHEMA Schema Object Tables

您可以使用 InnoDB INFORMATION_SCHEMA 表来提取由 InnoDB 管理的模式对象的元数据。这些信息来自数据字典。传统上,您可以使用 第 17.17 节,“InnoDB 监视器” 中的方法,通过设置 InnoDB 监视器并解析 SHOW ENGINE INNODB STATUS 语句的输出,来获取此类信息。InnoDB INFORMATION_SCHEMA 表接口允许您使用 SQL 查询这些数据。You can extract metadata about schema objects managed by InnoDB using InnoDB INFORMATION_SCHEMA tables. This information comes from the data dictionary. Traditionally, you would get this type of information using the techniques from Section 17.17, “InnoDB Monitors”, setting up InnoDB monitors and parsing the output from the SHOW ENGINE INNODB STATUS statement. The InnoDB INFORMATION_SCHEMA table interface allows you to query this data using SQL.

InnoDB INFORMATION_SCHEMA 模式对象表包括这里列出的表:InnoDB INFORMATION_SCHEMA schema object tables include the tables listed here:

  • INNODB_DATAFILES

  • INNODB_TABLESTATS

  • INNODB_FOREIGN

  • INNODB_COLUMNS

  • INNODB_INDEXES

  • INNODB_FIELDS

  • INNODB_TABLESPACES

  • INNODB_TABLESPACES_BRIEF

  • INNODB_FOREIGN_COLS

  • INNODB_TABLES

这些表名表明了所提供数据的类型:The table names are indicative of the type of data provided:

  • INNODB_TABLES 提供关于 InnoDB 表的元数据。INNODB_TABLES provides metadata about InnoDB tables.

  • INNODB_COLUMNS 提供关于 InnoDB 表列的元数据。INNODB_COLUMNS provides metadata about InnoDB table columns.

  • INNODB_INDEXES 提供关于 InnoDB 索引的元数据。INNODB_INDEXES provides metadata about InnoDB indexes.

  • INNODB_FIELDS 提供关于 InnoDB 索引的关键列(字段)的元数据。INNODB_FIELDS provides metadata about the key columns (fields) of InnoDB indexes.

  • INNODB_TABLESTATS 提供了有关 InnoDB 表的底层状态信息视图,这些信息来源于内存数据结构。INNODB_TABLESTATS provides a view of low-level status information about InnoDB tables that is derived from in-memory data structures.

  • INNODB_DATAFILES 提供了 InnoDB 文件按表存储和通用表空间的数据文件路径信息。INNODB_DATAFILES provides data file path information for InnoDB file-per-table and general tablespaces.

  • INNODB_TABLESPACES 提供了有关 InnoDB 文件按表存储、通用表空间和撤销表空间的元数据。INNODB_TABLESPACES provides metadata about InnoDB file-per-table, general, and undo tablespaces.

  • INNODB_TABLESPACES_BRIEF 提供关于 InnoDB 表空间的部分元数据。INNODB_TABLESPACES_BRIEF provides a subset of metadata about InnoDB tablespaces.

  • INNODB_FOREIGN 提供关于在 InnoDB 表上定义的外键的元数据。INNODB_FOREIGN provides metadata about foreign keys defined on InnoDB tables.

  • INNODB_FOREIGN_COLS 提供关于在 InnoDB 表上定义的外键列的元数据。INNODB_FOREIGN_COLS provides metadata about the columns of foreign keys that are defined on InnoDB tables.

InnoDB INFORMATION_SCHEMA 架构对象表可以通过诸如 TABLE_ID、INDEX_ID 和 SPACE 等字段进行连接,从而让你轻松检索要研究或监控的对象的所有可用数据。InnoDB INFORMATION_SCHEMA schema object tables can be joined together through fields such as TABLE_ID, INDEX_ID, and SPACE, allowing you to easily retrieve all available data for an object you want to study or monitor.

有关每个表的列的信息,请参考 InnoDB INFORMATION_SCHEMA 文档。Refer to the InnoDB INFORMATION_SCHEMA documentation for information about the columns of each table.

示例 17.2 InnoDB INFORMATION_SCHEMA 架构对象表Example 17.2 InnoDB INFORMATION_SCHEMA Schema Object Tables

这个示例使用了一个简单的表(t1)和一个单一索引(i1)来演示在 InnoDB INFORMATION_SCHEMA 模式对象表中可以找到的元数据类型。This example uses a simple table (t1) with a single index (i1) to demonstrate the type of metadata found in the InnoDB INFORMATION_SCHEMA schema object tables.

  1. 创建一个测试数据库和表 t1:Create a test database and table t1:

    mysql> CREATE DATABASE test;
    
    mysql> USE test;
    
    mysql> CREATE TABLE t1 (
           col1 INT,
           col2 CHAR(10),
           col3 VARCHAR(10))
           ENGINE = InnoDB;
    
    mysql> CREATE INDEX i1 ON t1(col1);
    
  2. 在创建表 t1 后,查询 INNODB_TABLES 来找到 test/t1 的元数据:After creating the table t1, query INNODB_TABLES to locate the metadata for test/t1:

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TABLES WHERE NAME='test/t1' \G
    *************************** 1. row ***************************
         TABLE_ID: 71
             NAME: test/t1
             FLAG: 1
           N_COLS: 6
            SPACE: 57
       ROW_FORMAT: Compact
    ZIP_PAGE_SIZE: 0
     INSTANT_COLS: 0
    

    表 t1 的 TABLE_ID 为 71。FLAG 字段提供有关表格格式和存储特性的位级信息。该表有六列,其中三列是由 InnoDB 创建的隐藏列(DB_ROW_ID、DB_TRX_ID 和 DB_ROLL_PTR)。表的 SPACE ID 为 57(值为 0 表示表位于系统表空间中)。ROW_FORMAT 为紧凑型(Compact)。ZIP_PAGE_SIZE 仅适用于具有 Compressed 行格式的表。INSTANT_COLS 显示在使用 ALTER TABLE ... ADD COLUMN 并带 ALGORITHM=INSTANT 添加第一个即时列之前表中的列数。Table t1 has a TABLE_ID of 71. The FLAG field provides bit level information about table format and storage characteristics. There are six columns, three of which are hidden columns created by InnoDB (DB_ROW_ID, DB_TRX_ID, and DB_ROLL_PTR). The ID of the table's SPACE is 57 (a value of 0 would indicate that the table resides in the system tablespace). The ROW_FORMAT is Compact. ZIP_PAGE_SIZE only applies to tables with a Compressed row format. INSTANT_COLS shows number of columns in the table prior to adding the first instant column using ALTER TABLE ... ADD COLUMN with ALGORITHM=INSTANT.

  3. 使用来自 INNODB_TABLES 的 TABLE_ID 信息,查询 INNODB_COLUMNS 表以获取该表列的信息。Using the TABLE_ID information from INNODB_TABLES, query the INNODB_COLUMNS table for information about the table's columns.

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_COLUMNS where TABLE_ID = 71\G
    *************************** 1. row ***************************
         TABLE_ID: 71
             NAME: col1
              POS: 0
            MTYPE: 6
           PRTYPE: 1027
              LEN: 4
      HAS_DEFAULT: 0
    DEFAULT_VALUE: NULL
    *************************** 2. row ***************************
         TABLE_ID: 71
             NAME: col2
              POS: 1
            MTYPE: 2
           PRTYPE: 524542
              LEN: 10
      HAS_DEFAULT: 0
    DEFAULT_VALUE: NULL
    *************************** 3. row ***************************
         TABLE_ID: 71
             NAME: col3
              POS: 2
            MTYPE: 1
           PRTYPE: 524303
              LEN: 10
      HAS_DEFAULT: 0
    DEFAULT_VALUE: NULL
    

    除了 TABLE_ID 和列 NAME 外,INNODB_COLUMNS 提供了每一列的序号位置(POS,从 0 开始并按顺序递增)、列的 MTYPE 或 “主类型”(6 = INT, 2 = CHAR, 1 = VARCHAR)、PRTYPE 或 “精确类型”(一个二进制值,其位表示 MySQL 数据类型、字符集编码和可空性),以及列长度(LEN)。HAS_DEFAULT 和 DEFAULT_VALUE 列仅适用于使用 ALTER TABLE ... ADD COLUMN 并带 ALGORITHM=INSTANT 即时添加的列。In addition to the TABLE_ID and column NAME, INNODB_COLUMNS provides the ordinal position (POS) of each column (starting from 0 and incrementing sequentially), the column MTYPE or “main type” (6 = INT, 2 = CHAR, 1 = VARCHAR), the PRTYPE or “precise type” (a binary value with bits that represent the MySQL data type, character set code, and nullability), and the column length (LEN). The HAS_DEFAULT and DEFAULT_VALUE columns only apply to columns added instantly using ALTER TABLE ... ADD COLUMN with ALGORITHM=INSTANT.

  4. 再次使用来自 INNODB_TABLES 的 TABLE_ID 信息,查询 INNODB_INDEXES 以获取与表 t1 相关的索引信息。Using the TABLE_ID information from INNODB_TABLES once again, query INNODB_INDEXES for information about the indexes associated with table t1.

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_INDEXES WHERE TABLE_ID = 71 \G
    *************************** 1. row ***************************
           INDEX_ID: 111
               NAME: GEN_CLUST_INDEX
           TABLE_ID: 71
               TYPE: 1
           N_FIELDS: 0
            PAGE_NO: 3
              SPACE: 57
    MERGE_THRESHOLD: 50
    *************************** 2. row ***************************
           INDEX_ID: 112
               NAME: i1
           TABLE_ID: 71
               TYPE: 0
           N_FIELDS: 1
            PAGE_NO: 4
              SPACE: 57
    MERGE_THRESHOLD: 50
    

    INNODB_INDEXES 返回两个索引的数据。第一个索引是 GEN_CLUST_INDEX,这是一个聚簇索引,如果表没有用户定义的聚簇索引,InnoDB 会创建它。第二个索引(i1)是用户定义的辅助索引。INNODB_INDEXES returns data for two indexes. The first index is GEN_CLUST_INDEX, which is a clustered index created by InnoDB if the table does not have a user-defined clustered index. The second index (i1) is the user-defined secondary index.

    INDEX_ID 是索引的标识符,在一个实例中的所有数据库中都是唯一的。TABLE_ID 标识索引关联的表。索引的 TYPE 值表示索引的类型(1 = 聚集索引,0 = 次级索引)。N_FILEDS 值表示组成索引的字段数量。PAGE_NO 是索引 B 树的根页号,SPACE 是索引所在表空间的 ID。非零值表示索引不位于系统表空间中。MERGE_THRESHOLD 定义索引页中数据量的百分比阈值。如果在删除行或通过更新操作缩短行时,索引页中的数据量低于此值(默认值为 50%),InnoDB 会尝试将该索引页与相邻的索引页合并。The INDEX_ID is an identifier for the index that is unique across all databases in an instance. The TABLE_ID identifies the table that the index is associated with. The index TYPE value indicates the type of index (1 = Clustered Index, 0 = Secondary index). The N_FILEDS value is the number of fields that comprise the index. PAGE_NO is the root page number of the index B-tree, and SPACE is the ID of the tablespace where the index resides. A nonzero value indicates that the index does not reside in the system tablespace. MERGE_THRESHOLD defines a percentage threshold value for the amount of data in an index page. If the amount of data in an index page falls below the this value (the default is 50%) when a row is deleted or when a row is shortened by an update operation, InnoDB attempts to merge the index page with a neighboring index page.

  5. 使用来自 INNODB_INDEXES 的 INDEX_ID 信息,查询 INNODB_FIELDS 获取索引 i1 的字段信息。Using the INDEX_ID information from INNODB_INDEXES, query INNODB_FIELDS for information about the fields of index i1.

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FIELDS where INDEX_ID = 112 \G
    *************************** 1. row ***************************
    INDEX_ID: 112
        NAME: col1
         POS: 0
    

    INNODB_FIELDS 提供被索引字段的 NAME 以及其在索引中的顺序位置。如果索引 (i1) 是在多个字段上定义的,INNODB_FIELDS 将提供每个被索引字段的元数据。INNODB_FIELDS provides the NAME of the indexed field and its ordinal position within the index. If the index (i1) had been defined on multiple fields, INNODB_FIELDS would provide metadata for each of the indexed fields.

  6. 使用来自 INNODB_TABLES 的 SPACE 信息,查询 INNODB_TABLESPACES 表以获取关于表的表空间的信息。Using the SPACE information from INNODB_TABLES, query INNODB_TABLESPACES table for information about the table's tablespace.

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TABLESPACES WHERE SPACE = 57 \G
    *************************** 1. row ***************************
              SPACE: 57
              NAME: test/t1
              FLAG: 16417
        ROW_FORMAT: Dynamic
         PAGE_SIZE: 16384
     ZIP_PAGE_SIZE: 0
        SPACE_TYPE: Single
     FS_BLOCK_SIZE: 4096
         FILE_SIZE: 114688
    ALLOCATED_SIZE: 98304
    AUTOEXTEND_SIZE: 0
    SERVER_VERSION: 8.4.0
     SPACE_VERSION: 1
        ENCRYPTION: N
             STATE: normal
    

    除了表空间的 SPACE ID 和关联表的 NAME 之外,INNODB_TABLESPACES 提供表空间 FLAG 数据,这些是关于表空间格式和存储特性的位级信息。此外,还提供表空间的 ROW_FORMAT、PAGE_SIZE 以及其他若干表空间元数据项。In addition to the SPACE ID of the tablespace and the NAME of the associated table, INNODB_TABLESPACES provides tablespace FLAG data, which is bit level information about tablespace format and storage characteristics. Also provided are tablespace ROW_FORMAT, PAGE_SIZE, and several other tablespace metadata items.

  7. 再次使用来自 INNODB_TABLES 的 SPACE 信息,查询 INNODB_DATAFILES 获取表空间数据文件的位置。Using the SPACE information from INNODB_TABLES once again, query INNODB_DATAFILES for the location of the tablespace data file.

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_DATAFILES WHERE SPACE = 57 \G
    *************************** 1. row ***************************
    SPACE: 57
     PATH: ./test/t1.ibd
    

    数据文件位于 MySQL 的 data 目录下的 test 目录中。如果使用 CREATE TABLE 语句的 DATA DIRECTORY 子句在 MySQL 数据目录之外的某个位置创建了 每表单独文件 表空间,那么表空间 PATH 将是完全限定的目录路径。The datafile is located in the test directory under MySQL's data directory. If a file-per-table tablespace were created in a location outside the MySQL data directory using the DATA DIRECTORY clause of the CREATE TABLE statement, the tablespace PATH would be a fully qualified directory path.

  8. 作为最后一步,向表 t1(TABLE_ID = 71)插入一行数据,并查看 INNODB_TABLESTATS 表中的数据。该表中的数据被 MySQL 优化器用于计算在查询 InnoDB 表时使用哪个索引。这些信息来源于内存中的数据结构。As a final step, insert a row into table t1 (TABLE_ID = 71) and view the data in the INNODB_TABLESTATS table. The data in this table is used by the MySQL optimizer to calculate which index to use when querying an InnoDB table. This information is derived from in-memory data structures.

    mysql> INSERT INTO t1 VALUES(5, 'abc', 'def');
    Query OK, 1 row affected (0.06 sec)
    
    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TABLESTATS where TABLE_ID = 71 \G
    *************************** 1. row ***************************
             TABLE_ID: 71
                 NAME: test/t1
    STATS_INITIALIZED: Initialized
             NUM_ROWS: 1
     CLUST_INDEX_SIZE: 1
     OTHER_INDEX_SIZE: 0
     MODIFIED_COUNTER: 1
              AUTOINC: 0
            REF_COUNT: 1
    

    STATS_INITIALIZED 字段表示是否已为该表收集统计信息。NUM_ROWS 是该表当前估计的行数。CLUST_INDEX_SIZE 和 OTHER_INDEX_SIZE 字段分别报告存储表的聚集索引和二级索引的磁盘页数。MODIFIED_COUNTER 值显示由 DML 操作及外键的级联操作修改的行数。AUTOINC 值是任何基于自增操作的下一次分配号码。表 t1 上没有定义自增列,因此该值为 0。REF_COUNT 值是一个计数器。当计数器达到 0 时,表示表的元数据可以从表缓存中驱逐。The STATS_INITIALIZED field indicates whether or not statistics have been collected for the table. NUM_ROWS is the current estimated number of rows in the table. The CLUST_INDEX_SIZE and OTHER_INDEX_SIZE fields report the number of pages on disk that store clustered and secondary indexes for the table, respectively. The MODIFIED_COUNTER value shows the number of rows modified by DML operations and cascade operations from foreign keys. The AUTOINC value is the next number to be issued for any autoincrement-based operation. There are no autoincrement columns defined on table t1, so the value is 0. The REF_COUNT value is a counter. When the counter reaches 0, it signifies that the table metadata can be evicted from the table cache.


例 17.3 外键 INFORMATION_SCHEMA 架构对象表Example 17.3 Foreign Key INFORMATION_SCHEMA Schema Object Tables

INNODB_FOREIGN 和 INNODB_FOREIGN_COLS 表提供有关外键关系的数据。此示例使用一个父表和一个具有外键关系的子表来演示在 INNODB_FOREIGN 和 INNODB_FOREIGN_COLS 表中发现的数据。The INNODB_FOREIGN and INNODB_FOREIGN_COLS tables provide data about foreign key relationships. This example uses a parent table and child table with a foreign key relationship to demonstrate the data found in the INNODB_FOREIGN and INNODB_FOREIGN_COLS tables.

  1. 创建包含父表和子表的测试数据库:Create the test database with parent and child tables:

    mysql> CREATE DATABASE test;
    
    mysql> USE test;
    
    mysql> CREATE TABLE parent (id INT NOT NULL,
           PRIMARY KEY (id)) ENGINE=INNODB;
    
    mysql> CREATE TABLE child (id INT, parent_id INT,
        ->     INDEX par_ind (parent_id),
        ->     CONSTRAINT fk1
        ->     FOREIGN KEY (parent_id) REFERENCES parent(id)
        ->     ON DELETE CASCADE) ENGINE=INNODB;
    
  2. 在创建父表和子表后,查询 INNODB_FOREIGN,并找到 test/child 和 test/parent 外键关系的外键信息:After the parent and child tables are created, query INNODB_FOREIGN and locate the foreign key data for the test/child and test/parent foreign key relationship:

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FOREIGN \G
    *************************** 1. row ***************************
          ID: test/fk1
    FOR_NAME: test/child
    REF_NAME: test/parent
      N_COLS: 1
        TYPE: 1
    

    元数据包括外键 ID (fk1),它的名称来自在子表上定义的 CONSTRAINT。FOR_NAME 是定义外键的子表名称。REF_NAME 是父表名称(即 “被引用” 表)。N_COLS 是外键索引中列的数量。TYPE 是一个数值,表示位标志,用于提供关于外键列的附加信息。在此情况下,TYPE 值为 1,这表明外键指定了 ON DELETE CASCADE 选项。有关 TYPE 值的更多信息,请参阅 INNODB_FOREIGN 表定义。Metadata includes the foreign key ID (fk1), which is named for the CONSTRAINT that was defined on the child table. The FOR_NAME is the name of the child table where the foreign key is defined. REF_NAME is the name of the parent table (the “referenced” table). N_COLS is the number of columns in the foreign key index. TYPE is a numerical value representing bit flags that provide additional information about the foreign key column. In this case, the TYPE value is 1, which indicates that the ON DELETE CASCADE option was specified for the foreign key. See the INNODB_FOREIGN table definition for more information about TYPE values.

  3. 使用外键 ID,查询 INNODB_FOREIGN_COLS 以查看外键列的数据。Using the foreign key ID, query INNODB_FOREIGN_COLS to view data about the columns of the foreign key.

    mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_FOREIGN_COLS WHERE ID = 'test/fk1' \G
    *************************** 1. row ***************************
              ID: test/fk1
    FOR_COL_NAME: parent_id
    REF_COL_NAME: id
             POS: 0
    

    FOR_COL_NAME 是子表中外键列的名称,REF_COL_NAME 是父表中被引用列的名称。POS 值是外键索引中键字段的序号位置,从零开始。FOR_COL_NAME is the name of the foreign key column in the child table, and REF_COL_NAME is the name of the referenced column in the parent table. The POS value is the ordinal position of the key field within the foreign key index, starting at zero.


示例 17.4 连接 InnoDB INFORMATION_SCHEMA 模式对象表Example 17.4 Joining InnoDB INFORMATION_SCHEMA Schema Object Tables

该示例演示了如何连接三个 InnoDB INFORMATION_SCHEMA 架构对象表(INNODB_TABLES、INNODB_TABLESPACES 和 INNODB_TABLESTATS)以收集 employees 示例数据库中表的文件格式、行格式、页大小和索引大小信息。This example demonstrates joining three InnoDB INFORMATION_SCHEMA schema object tables (INNODB_TABLES, INNODB_TABLESPACES, and INNODB_TABLESTATS) to gather file format, row format, page size, and index size information about tables in the employees sample database.

以下表别名用于简化查询字符串:The following table aliases are used to shorten the query string:

IF() 控制流函数用于处理压缩表。如果表是压缩的,索引大小使用 ZIP_PAGE_SIZE 而不是 PAGE_SIZE 来计算。以字节为单位报告的 CLUST_INDEX_SIZE 和 OTHER_INDEX_SIZE 会除以 1024*1024 以提供以兆字节(MB)为单位的索引大小。MB 值使用 ROUND() 函数四舍五入到零小数位。An IF() control flow function is used to account for compressed tables. If a table is compressed, the index size is calculated using ZIP_PAGE_SIZE rather than PAGE_SIZE. CLUST_INDEX_SIZE and OTHER_INDEX_SIZE, which are reported in bytes, are divided by 1024*1024 to provide index sizes in megabytes (MBs). MB values are rounded to zero decimal spaces using the ROUND() function.

mysql> SELECT a.NAME, a.ROW_FORMAT,
        @page_size :=
         IF(a.ROW_FORMAT='Compressed',
          b.ZIP_PAGE_SIZE, b.PAGE_SIZE)
          AS page_size,
         ROUND((@page_size * c.CLUST_INDEX_SIZE)
          /(1024*1024)) AS pk_mb,
         ROUND((@page_size * c.OTHER_INDEX_SIZE)
          /(1024*1024)) AS secidx_mb
       FROM INFORMATION_SCHEMA.INNODB_TABLES a
       INNER JOIN INFORMATION_SCHEMA.INNODB_TABLESPACES b on a.NAME = b.NAME
       INNER JOIN INFORMATION_SCHEMA.INNODB_TABLESTATS c on b.NAME = c.NAME
       WHERE a.NAME LIKE 'employees/%'
       ORDER BY a.NAME DESC;
+------------------------+------------+-----------+-------+-----------+
| NAME                   | ROW_FORMAT | page_size | pk_mb | secidx_mb |
+------------------------+------------+-----------+-------+-----------+
| employees/titles       | Dynamic    |     16384 |    20 |        11 |
| employees/salaries     | Dynamic    |     16384 |    93 |        34 |
| employees/employees    | Dynamic    |     16384 |    15 |         0 |
| employees/dept_manager | Dynamic    |     16384 |     0 |         0 |
| employees/dept_emp     | Dynamic    |     16384 |    12 |        10 |
| employees/departments  | Dynamic    |     16384 |     0 |         0 |
+------------------------+------------+-----------+-------+-----------+