### 15.15.5 InnoDB 信息\_SCHEMA 缓冲池表

这`InnoDB` `INFORMATION_SCHEMA`缓冲池表提供缓冲池状态信息和有关页面内的元数据`InnoDB`缓冲池。

这`InnoDB` `INFORMATION_SCHEMA`缓冲池表包括下面列出的那些：

```
mysql> SHOW TABLES FROM INFORMATION_SCHEMA LIKE 'INNODB_BUFFER%';
+-----------------------------------------------+
| Tables_in_INFORMATION_SCHEMA (INNODB_BUFFER%) |
+-----------------------------------------------+
| INNODB_BUFFER_PAGE_LRU                        |
| INNODB_BUFFER_PAGE                            |
| INNODB_BUFFER_POOL_STATS                      |
+-----------------------------------------------+
```

#### 表概述

-   [`INNODB_BUFFER_PAGE`](information-schema-innodb-buffer-page-table.html): 保存关于每个页面的信息`InnoDB`缓冲池。

-   [`INNODB_BUFFER_PAGE_LRU`](information-schema-innodb-buffer-page-lru-table.html)：保存有关页面中的信息`InnoDB`缓冲池，特别是它们在 LRU 列表中的排序方式，该列表确定当缓冲池变满时要从缓冲池中逐出哪些页面。这[`INNODB_BUFFER_PAGE_LRU`](information-schema-innodb-buffer-page-lru-table.html)表具有相同的列[`INNODB_BUFFER_PAGE`](information-schema-innodb-buffer-page-table.html)表，除了[`INNODB_BUFFER_PAGE_LRU`](information-schema-innodb-buffer-page-lru-table.html)表有一个`LRU_POSITION`列而不是`BLOCK_ID`柱子。

-   [`INNODB_BUFFER_POOL_STATS`](information-schema-innodb-buffer-pool-stats-table.html)：提供缓冲池状态信息。许多相同的信息由[`显示引擎 INNODB 状态`](show-engine.html)输出，或者可以使用`InnoDB`缓冲池服务器状态变量。

警告

查询[`INNODB_BUFFER_PAGE`](information-schema-innodb-buffer-page-table.html)要么[`INNODB_BUFFER_PAGE_LRU`](information-schema-innodb-buffer-page-lru-table.html)表会影响性能。不要在生产系统上查询这些表，除非您知道性能影响并确定它是可以接受的。为避免影响生产系统的性能，请重现您要调查的问题并在测试实例上查询缓冲池统计信息。

[](<>)

**示例 15.6 在 INNODB 中查询系统数据\_缓冲\_页表**

此查询通过排除包含系统数据的页面来提供包含系统数据的页面的近似计数。`TABLE_NAME`值是`空值`或包含斜线`/`或期间`.`在表名中，表示用户定义的表。

```
mysql> SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE TABLE_NAME IS NULL OR (INSTR(TABLE_NAME, '/') = 0 AND INSTR(TABLE_NAME, '.') = 0);
+----------+
| COUNT(*) |
+----------+
|     1516 |
+----------+
```

此查询返回包含系统数据的页面的大致数量、缓冲池页面的总数以及包含系统数据的页面的大致百分比。

```
mysql> SELECT
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE TABLE_NAME IS NULL OR (INSTR(TABLE_NAME, '/') = 0 AND INSTR(TABLE_NAME, '.') = 0)
       ) AS system_pages,
       (
       SELECT COUNT(*)
       FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       ) AS total_pages,
       (
       SELECT ROUND((system_pages/total_pages) * 100)
       ) AS system_page_percentage;
+--------------+-------------+------------------------+
| system_pages | total_pages | system_page_percentage |
+--------------+-------------+------------------------+
|          295 |        8192 |                      4 |
+--------------+-------------+------------------------+
```

缓冲池中系统数据的类型可以通过查询`PAGE_TYPE`价值。例如，以下查询返回八个不同的`PAGE_TYPE`包含系统数据的页面之间的值：

```
mysql> SELECT DISTINCT PAGE_TYPE FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE TABLE_NAME IS NULL OR (INSTR(TABLE_NAME, '/') = 0 AND INSTR(TABLE_NAME, '.') = 0);
+-------------------+
| PAGE_TYPE         |
+-------------------+
| SYSTEM            |
| IBUF_BITMAP       |
| UNKNOWN           |
| FILE_SPACE_HEADER |
| INODE             |
| UNDO_LOG          |
| ALLOCATED         |
+-------------------+
```

[](<>)

**示例 15.7 在 INNODB 中查询用户数据\_缓冲\_页表**

此查询通过计算包含用户数据的页面来提供包含用户数据的页面的近似计数。`TABLE_NAME`值为`非空`和`不喜欢'%INNODB_TABLES%'`.

```
mysql> SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE TABLE_NAME IS NOT NULL AND TABLE_NAME NOT LIKE '%INNODB_TABLES%';
+----------+
| COUNT(*) |
+----------+
|     7897 |
+----------+
```

此查询返回包含用户数据的页面的大致数量、缓冲池页面的总数以及包含用户数据的页面的大致百分比。

```
mysql> SELECT
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE TABLE_NAME IS NOT NULL AND (INSTR(TABLE_NAME, '/') > 0 OR INSTR(TABLE_NAME, '.') > 0)
       ) AS user_pages,
       (
       SELECT COUNT(*)
       FROM information_schema.INNODB_BUFFER_PAGE
       ) AS total_pages,
       (
       SELECT ROUND((user_pages/total_pages) * 100)
       ) AS user_page_percentage;
+------------+-------------+----------------------+
| user_pages | total_pages | user_page_percentage |
+------------+-------------+----------------------+
|       7897 |        8192 |                   96 |
+------------+-------------+----------------------+
```

此查询标识缓冲池中具有页面的用户定义表：

```
mysql> SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE TABLE_NAME IS NOT NULL AND (INSTR(TABLE_NAME, '/') > 0 OR INSTR(TABLE_NAME, '.') > 0)
       AND TABLE_NAME NOT LIKE '`mysql`.`innodb_%';
+-------------------------+
| TABLE_NAME              |
+-------------------------+
| `employees`.`salaries`  |
| `employees`.`employees` |
+-------------------------+
```

[](<>)

**示例 15.8 查询 INNODB 中的索引数据\_缓冲\_页表**

有关索引页的信息，请查询`INDEX_NAME`列使用索引的名称。例如，以下查询返回页数和页的总数据大小`emp_no`上定义的索引`员工工资`桌子：

```
mysql> SELECT INDEX_NAME, COUNT(*) AS Pages,
ROUND(SUM(IF(COMPRESSED_SIZE = 0, @@GLOBAL.innodb_page_size, COMPRESSED_SIZE))/1024/1024)
AS 'Total Data (MB)'
FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
WHERE INDEX_NAME='emp_no' AND TABLE_NAME = '`employees`.`salaries`';
+------------+-------+-----------------+
| INDEX_NAME | Pages | Total Data (MB) |
+------------+-------+-----------------+
| emp_no     |  1609 |              25 |
+------------+-------+-----------------+
```

此查询返回在`员工工资`桌子：

```
mysql> SELECT INDEX_NAME, COUNT(*) AS Pages,
       ROUND(SUM(IF(COMPRESSED_SIZE = 0, @@GLOBAL.innodb_page_size, COMPRESSED_SIZE))/1024/1024)
       AS 'Total Data (MB)'
       FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE
       WHERE TABLE_NAME = '`employees`.`salaries`'
       GROUP BY INDEX_NAME;
+------------+-------+-----------------+
| INDEX_NAME | Pages | Total Data (MB) |
+------------+-------+-----------------+
| emp_no     |  1608 |              25 |
| PRIMARY    |  6086 |              95 |
+------------+-------+-----------------+
```

[](<>)

**示例 15.9 查询 LRU_INNODB 中的 POSITION 数据\_缓冲\_页\_LRU 表**

这[`INNODB_BUFFER_PAGE_LRU`](information-schema-innodb-buffer-page-lru-table.html)表保存有关页面中的信息`InnoDB`缓冲池，特别是它们的排序方式，它决定了当缓冲池变满时要从缓冲池中驱逐哪些页面。此页面的定义与[`INNODB_BUFFER_PAGE`](information-schema-innodb-buffer-page-table.html)，除了这个表有一个`LRU_POSITION`列而不是`BLOCK_ID`柱子。

该查询计算 LRU 列表中特定位置的位置数`雇员.雇员`桌子。

```
mysql> SELECT COUNT(LRU_POSITION) FROM INFORMATION_SCHEMA.INNODB_BUFFER_PAGE_LRU
       WHERE TABLE_NAME='`employees`.`employees`' AND LRU_POSITION < 3072;
+---------------------+
| COUNT(LRU_POSITION) |
+---------------------+
|                 548 |
+---------------------+
```

[](<>)

**示例 15.10 查询 INNODB\_缓冲\_水池\_统计表**

这[`INNODB_BUFFER_POOL_STATS`](information-schema-innodb-buffer-pool-stats-table.html)表提供的信息类似于[`显示引擎 INNODB 状态`](show-engine.html)和`InnoDB`缓冲池状态变量。

```
mysql> SELECT * FROM information_schema.INNODB_BUFFER_POOL_STATS \G
*************************** 1. row ***************************
                         POOL_ID: 0
                       POOL_SIZE: 8192
                    FREE_BUFFERS: 1
                  DATABASE_PAGES: 8173
              OLD_DATABASE_PAGES: 3014
         MODIFIED_DATABASE_PAGES: 0
              PENDING_DECOMPRESS: 0
                   PENDING_READS: 0
               PENDING_FLUSH_LRU: 0
              PENDING_FLUSH_LIST: 0
                PAGES_MADE_YOUNG: 15907
            PAGES_NOT_MADE_YOUNG: 3803101
           PAGES_MADE_YOUNG_RATE: 0
       PAGES_MADE_NOT_YOUNG_RATE: 0
               NUMBER_PAGES_READ: 3270
            NUMBER_PAGES_CREATED: 13176
            NUMBER_PAGES_WRITTEN: 15109
                 PAGES_READ_RATE: 0
               PAGES_CREATE_RATE: 0
              PAGES_WRITTEN_RATE: 0
                NUMBER_PAGES_GET: 33069332
                        HIT_RATE: 0
    YOUNG_MAKE_PER_THOUSAND_GETS: 0
NOT_YOUNG_MAKE_PER_THOUSAND_GETS: 0
         NUMBER_PAGES_READ_AHEAD: 2713
       NUMBER_READ_AHEAD_EVICTED: 0
                 READ_AHEAD_RATE: 0
         READ_AHEAD_EVICTED_RATE: 0
                    LRU_IO_TOTAL: 0
                  LRU_IO_CURRENT: 0
                UNCOMPRESS_TOTAL: 0
              UNCOMPRESS_CURRENT: 0
```

为了比较，[`显示引擎 INNODB 状态`](show-engine.html)输出和`InnoDB`缓冲池状态变量输出如下所示，基于相同的数据集。

有关更多信息[`显示引擎 INNODB 状态`](show-engine.html)输出，见[第 15.17.3 节，“InnoDB 标准监视器和锁定监视器输出”](innodb-standard-monitor.html).

```
mysql> SHOW ENGINE INNODB STATUS \G
...
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 579084
Buffer pool size   8192
Free buffers       1
Database pages     8173
Old database pages 3014
Modified db pages  0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 15907, not young 3803101
0.00 youngs/s, 0.00 non-youngs/s
Pages read 3270, created 13176, written 15109
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 8173, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
...
```

有关状态变量的描述，请参见[第 5.1.10 节，“服务器状态变量”](server-status-variables.html).

```
mysql> SHOW STATUS LIKE 'Innodb_buffer%';
+---------------------------------------+-------------+
| Variable_name                         | Value       |
+---------------------------------------+-------------+
| Innodb_buffer_pool_dump_status        | not started |
| Innodb_buffer_pool_load_status        | not started |
| Innodb_buffer_pool_resize_status      | not started |
| Innodb_buffer_pool_pages_data         | 8173        |
| Innodb_buffer_pool_bytes_data         | 133906432   |
| Innodb_buffer_pool_pages_dirty        | 0           |
| Innodb_buffer_pool_bytes_dirty        | 0           |
| Innodb_buffer_pool_pages_flushed      | 15109       |
| Innodb_buffer_pool_pages_free         | 1           |
| Innodb_buffer_pool_pages_misc         | 18          |
| Innodb_buffer_pool_pages_total        | 8192        |
| Innodb_buffer_pool_read_ahead_rnd     | 0           |
| Innodb_buffer_pool_read_ahead         | 2713        |
| Innodb_buffer_pool_read_ahead_evicted | 0           |
| Innodb_buffer_pool_read_requests      | 33069332    |
| Innodb_buffer_pool_reads              | 558         |
| Innodb_buffer_pool_wait_free          | 0           |
| Innodb_buffer_pool_write_requests     | 11985961    |
+---------------------------------------+-------------+
```
