## 27.1 性能架构快速入门

本节简要介绍 Performance Schema，并通过示例说明如何使用它。有关其他示例，请参阅[第 27.19 节，“使用性能模式诊断问题”](performance-schema-examples.html).

性能模式默认启用。要显式启用或禁用它，请使用[`性能模式`](performance-schema-system-variables.html#sysvar_performance_schema)变量设置为适当的值。例如，在服务器中使用这些行`我的.cnf`文件：

```
[mysqld]
performance_schema=ON
```

当服务器启动时，它会看到[`性能模式`](performance-schema-system-variables.html#sysvar_performance_schema)并尝试初始化性能模式。要验证初始化是否成功，请使用以下语句：

```
mysql> SHOW VARIABLES LIKE 'performance_schema';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| performance_schema | ON    |
+--------------------+-------+
```

一个值`在`表示 Performance Schema 初始化成功，可以使用了。一个值`离开`表示发生了一些错误。检查服务器错误日志以获取有关问题的信息。

Performance Schema 是作为存储引擎实现的，因此您可以在[`INFORMATION_SCHEMA.ENGINES`](information-schema-engines-table.html)表或[`显示引擎`](show-engines.html)陈述：

```
mysql> SELECT * FROM INFORMATION_SCHEMA.ENGINES
       WHERE ENGINE='PERFORMANCE_SCHEMA'\G
*************************** 1. row ***************************
      ENGINE: PERFORMANCE_SCHEMA
     SUPPORT: YES
     COMMENT: Performance Schema
TRANSACTIONS: NO
          XA: NO
  SAVEPOINTS: NO

mysql> SHOW ENGINES\G
...
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
...
```

这[`PERFORMANCE_SCHEMA`](performance-schema.html)存储引擎对表中的表进行操作`性能模式`数据库。你（们）能做到`性能模式`默认数据库，因此对其表的引用无需使用数据库名称进行限定：

```
mysql> USE performance_schema;
```

性能模式表存储在`性能模式`数据库。与任何其他数据库一样，可以通过从`INFORMATION_SCHEMA`数据库或通过使用[`显示`](show.html)陈述。例如，使用以下任一语句来查看存在哪些 Performance Schema 表：

```
mysql> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
       WHERE TABLE_SCHEMA = 'performance_schema';
+------------------------------------------------------+
| TABLE_NAME                                           |
+------------------------------------------------------+
| accounts                                             |
| cond_instances                                       |
...
| events_stages_current                                |
| events_stages_history                                |
| events_stages_history_long                           |
| events_stages_summary_by_account_by_event_name       |
| events_stages_summary_by_host_by_event_name          |
| events_stages_summary_by_thread_by_event_name        |
| events_stages_summary_by_user_by_event_name          |
| events_stages_summary_global_by_event_name           |
| events_statements_current                            |
| events_statements_history                            |
| events_statements_history_long                       |
...
| file_instances                                       |
| file_summary_by_event_name                           |
| file_summary_by_instance                             |
| host_cache                                           |
| hosts                                                |
| memory_summary_by_account_by_event_name              |
| memory_summary_by_host_by_event_name                 |
| memory_summary_by_thread_by_event_name               |
| memory_summary_by_user_by_event_name                 |
| memory_summary_global_by_event_name                  |
| metadata_locks                                       |
| mutex_instances                                      |
| objects_summary_global_by_type                       |
| performance_timers                                   |
| replication_connection_configuration                 |
| replication_connection_status                        |
| replication_applier_configuration                    |
| replication_applier_status                           |
| replication_applier_status_by_coordinator            |
| replication_applier_status_by_worker                 |
| rwlock_instances                                     |
| session_account_connect_attrs                        |
| session_connect_attrs                                |
| setup_actors                                         |
| setup_consumers                                      |
| setup_instruments                                    |
| setup_objects                                        |
| socket_instances                                     |
| socket_summary_by_event_name                         |
| socket_summary_by_instance                           |
| table_handles                                        |
| table_io_waits_summary_by_index_usage                |
| table_io_waits_summary_by_table                      |
| table_lock_waits_summary_by_table                    |
| threads                                              |
| users                                                |
+------------------------------------------------------+

mysql> SHOW TABLES FROM performance_schema;
+------------------------------------------------------+
| Tables_in_performance_schema                         |
+------------------------------------------------------+
| accounts                                             |
| cond_instances                                       |
| events_stages_current                                |
| events_stages_history                                |
| events_stages_history_long                           |
...
```

随着额外检测的实施，性能模式表的数量会随着时间的推移而增加。

的名称`性能模式`数据库是小写的，其中的表名也是小写的。查询应以小写形式指定名称。

要查看单个表的结构，请使用[`显示创建表`](show-create-table.html)：

```
mysql> SHOW CREATE TABLE performance_schema.setup_consumers\G
*************************** 1. row ***************************
       Table: setup_consumers
Create Table: CREATE TABLE `setup_consumers` (
  `NAME` varchar(64) NOT NULL,
  `ENABLED` enum('YES','NO') NOT NULL,
  PRIMARY KEY (`NAME`)
) ENGINE=PERFORMANCE_SCHEMA DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

表结构也可以通过从表中选择来获得，例如[`INFORMATION_SCHEMA.COLUMNS`](information-schema-columns-table.html)或通过使用诸如[`显示列`](show-columns.html).

中的表`性能模式`数据库可以根据其中的信息类型进行分组：当前事件、事件历史和摘要、对象实例和设置（配置）信息。以下示例说明了这些表的一些用途。有关每个组中的表的详细信息，请参阅[第 27.12 节，“性能模式表描述”](performance-schema-table-descriptions.html).

最初，并非所有仪器和消费者都已启用，因此性能模式不会收集所有事件。要打开所有这些并启用事件计时，请执行两条语句（行数可能因 MySQL 版本而异）：

```
mysql> UPDATE performance_schema.setup_instruments
       SET ENABLED = 'YES', TIMED = 'YES';
Query OK, 560 rows affected (0.04 sec)
mysql> UPDATE performance_schema.setup_consumers
       SET ENABLED = 'YES';
Query OK, 10 rows affected (0.00 sec)
```

要查看服务器目前正在做什么，请检查[`事件_等待_当前`](performance-schema-events-waits-current-table.html)桌子每个线程包含一行，显示每个线程最近监视的事件：

```
mysql> SELECT *
       FROM performance_schema.events_waits_current\G
*************************** 1. row ***************************
            THREAD_ID: 0
             EVENT_ID: 5523
         END_EVENT_ID: 5523
           EVENT_NAME: wait/synch/mutex/mysys/THR_LOCK::mutex
               SOURCE: thr_lock.c:525
          TIMER_START: 201660494489586
            TIMER_END: 201660494576112
           TIMER_WAIT: 86526
                SPINS: NULL
        OBJECT_SCHEMA: NULL
          OBJECT_NAME: NULL
           INDEX_NAME: NULL
          OBJECT_TYPE: NULL
OBJECT_INSTANCE_BEGIN: 142270668
     NESTING_EVENT_ID: NULL
   NESTING_EVENT_TYPE: NULL
            OPERATION: lock
      NUMBER_OF_BYTES: NULL
                FLAGS: 0
...
```

此事件表示线程0正在等待86526皮秒以获取对的锁定`锁：互斥锁`，在`迈西`子系统。前几列提供了以下信息：

-   ID列指示事件来自哪个线程以及事件编号。

-   `事件名称`指示检测了什么，以及`来源`指示哪个源文件包含插入指令的代码。

-   计时器列显示事件开始和停止的时间以及所用的时间。如果事件仍在进行中，则`计时器结束`和`等等`价值观是`无效的`.计时器值是近似值，以皮秒表示。有关计时器和事件时间收集的信息，请参阅[第27.4.1节，“性能模式事件计时”](performance-schema-timing.html).

    历史记录表包含与当前事件表相同的行，但有更多的行，并显示服务器“最近”而不是“当前”在做什么这个[`事件等待历史`](performance-schema-events-waits-history-table.html)和[`事件等待历史漫长`](performance-schema-events-waits-history-long-table.html)表分别包含每个线程最近的10个事件和最近的10000个事件。例如，要查看线程13生成的最近事件的信息，请执行以下操作：


```
mysql> SELECT EVENT_ID, EVENT_NAME, TIMER_WAIT
       FROM performance_schema.events_waits_history
       WHERE THREAD_ID = 13
       ORDER BY EVENT_ID;
+----------+-----------------------------------------+------------+
| EVENT_ID | EVENT_NAME                              | TIMER_WAIT |
+----------+-----------------------------------------+------------+
|       86 | wait/synch/mutex/mysys/THR_LOCK::mutex  |     686322 |
|       87 | wait/synch/mutex/mysys/THR_LOCK_malloc  |     320535 |
|       88 | wait/synch/mutex/mysys/THR_LOCK_malloc  |     339390 |
|       89 | wait/synch/mutex/mysys/THR_LOCK_malloc  |     377100 |
|       90 | wait/synch/mutex/sql/LOCK_plugin        |     614673 |
|       91 | wait/synch/mutex/sql/LOCK_open          |     659925 |
|       92 | wait/synch/mutex/sql/THD::LOCK_thd_data |     494001 |
|       93 | wait/synch/mutex/mysys/THR_LOCK_malloc  |     222489 |
|       94 | wait/synch/mutex/mysys/THR_LOCK_malloc  |     214947 |
|       95 | wait/synch/mutex/mysys/LOCK_alarm       |     312993 |
+----------+-----------------------------------------+------------+
```

当新事件添加到历史记录表中时，如果该表已满，则旧事件将被丢弃。

摘要表提供了一段时间内所有事件的汇总信息。此组中的表格以不同的方式汇总事件数据。要查看哪些仪器执行次数最多或等待时间最多，请对[`事件\u等待\u摘要\u全局\u按事件\u名称`](performance-schema-wait-summary-tables.html)桌上`明星伯爵`或`求和计时器等待`列，对应于`计数（*）`或`总和（计时器等待）`在所有事件中分别计算的值：

```
mysql> SELECT EVENT_NAME, COUNT_STAR
       FROM performance_schema.events_waits_summary_global_by_event_name
       ORDER BY COUNT_STAR DESC LIMIT 10;
+---------------------------------------------------+------------+
| EVENT_NAME                                        | COUNT_STAR |
+---------------------------------------------------+------------+
| wait/synch/mutex/mysys/THR_LOCK_malloc            |       6419 |
| wait/io/file/sql/FRM                              |        452 |
| wait/synch/mutex/sql/LOCK_plugin                  |        337 |
| wait/synch/mutex/mysys/THR_LOCK_open              |        187 |
| wait/synch/mutex/mysys/LOCK_alarm                 |        147 |
| wait/synch/mutex/sql/THD::LOCK_thd_data           |        115 |
| wait/io/file/myisam/kfile                         |        102 |
| wait/synch/mutex/sql/LOCK_global_system_variables |         89 |
| wait/synch/mutex/mysys/THR_LOCK::mutex            |         89 |
| wait/synch/mutex/sql/LOCK_open                    |         88 |
+---------------------------------------------------+------------+

mysql> SELECT EVENT_NAME, SUM_TIMER_WAIT
       FROM performance_schema.events_waits_summary_global_by_event_name
       ORDER BY SUM_TIMER_WAIT DESC LIMIT 10;
+----------------------------------------+----------------+
| EVENT_NAME                             | SUM_TIMER_WAIT |
+----------------------------------------+----------------+
| wait/io/file/sql/MYSQL_LOG             |     1599816582 |
| wait/synch/mutex/mysys/THR_LOCK_malloc |     1530083250 |
| wait/io/file/sql/binlog_index          |     1385291934 |
| wait/io/file/sql/FRM                   |     1292823243 |
| wait/io/file/myisam/kfile              |      411193611 |
| wait/io/file/myisam/dfile              |      322401645 |
| wait/synch/mutex/mysys/LOCK_alarm      |      145126935 |
| wait/io/file/sql/casetest              |      104324715 |
| wait/synch/mutex/sql/LOCK_plugin       |       86027823 |
| wait/io/file/sql/pid                   |       72591750 |
+----------------------------------------+----------------+
```

这些结果表明`这是我的锁`互斥是“热门”的，无论是在使用频率还是线程等待获取互斥的时间上。

笔记

这个`这是我的锁`互斥仅在调试版本中使用。在生产构建中，它不是热的，因为它不存在。

实例表记录了检测的对象类型。服务器使用插入指令的对象时，会生成一个事件。这些表提供了事件名称、注释或状态信息。例如[`文件实例`](performance-schema-file-instances-table.html)表列出了用于文件I/O操作的仪器及其关联文件的实例：

```
mysql> SELECT *
       FROM performance_schema.file_instances\G
*************************** 1. row ***************************
 FILE_NAME: /opt/mysql-log/60500/binlog.000007
EVENT_NAME: wait/io/file/sql/binlog
OPEN_COUNT: 0
*************************** 2. row ***************************
 FILE_NAME: /opt/mysql/60500/data/mysql/tables_priv.MYI
EVENT_NAME: wait/io/file/myisam/kfile
OPEN_COUNT: 1
*************************** 3. row ***************************
 FILE_NAME: /opt/mysql/60500/data/mysql/columns_priv.MYI
EVENT_NAME: wait/io/file/myisam/kfile
OPEN_COUNT: 1
...
```

设置表用于配置和显示监控特性。例如[`仪器设置`](performance-schema-setup-instruments-table.html)列出可收集事件的仪器集，并显示其中哪些仪器已启用：

```
mysql> SELECT NAME, ENABLED, TIMED
       FROM performance_schema.setup_instruments;
+---------------------------------------------------+---------+-------+
| NAME                                              | ENABLED | TIMED |
+---------------------------------------------------+---------+-------+
...
| stage/sql/end                                     | NO      | NO    |
| stage/sql/executing                               | NO      | NO    |
| stage/sql/init                                    | NO      | NO    |
| stage/sql/insert                                  | NO      | NO    |
...
| statement/sql/load                                | YES     | YES   |
| statement/sql/grant                               | YES     | YES   |
| statement/sql/check                               | YES     | YES   |
| statement/sql/flush                               | YES     | YES   |
...
| wait/synch/mutex/sql/LOCK_global_read_lock        | YES     | YES   |
| wait/synch/mutex/sql/LOCK_global_system_variables | YES     | YES   |
| wait/synch/mutex/sql/LOCK_lock_db                 | YES     | YES   |
| wait/synch/mutex/sql/LOCK_manager                 | YES     | YES   |
...
| wait/synch/rwlock/sql/LOCK_grant                  | YES     | YES   |
| wait/synch/rwlock/sql/LOGGER::LOCK_logger         | YES     | YES   |
| wait/synch/rwlock/sql/LOCK_sys_init_connect       | YES     | YES   |
| wait/synch/rwlock/sql/LOCK_sys_init_slave         | YES     | YES   |
...
| wait/io/file/sql/binlog                           | YES     | YES   |
| wait/io/file/sql/binlog_index                     | YES     | YES   |
| wait/io/file/sql/casetest                         | YES     | YES   |
| wait/io/file/sql/dbopt                            | YES     | YES   |
...
```

要了解如何解释仪器名称，请参阅[第27.6节，“性能模式仪表命名约定”](performance-schema-instrument-naming.html).

要控制是否为仪器收集事件，请设置`启用`重视`对`或`不`.例如：

```
mysql> UPDATE performance_schema.setup_instruments
       SET ENABLED = 'NO'
       WHERE NAME = 'wait/synch/mutex/sql/LOCK_mysql_create_db';
```

性能模式使用收集的事件来更新`性能模式`数据库，充当事件信息的“消费者”。这个[`设置用户`](performance-schema-setup-consumers-table.html)表列出了可用的使用者和已启用的使用者：

```
mysql> SELECT * FROM performance_schema.setup_consumers;
+----------------------------------+---------+
| NAME                             | ENABLED |
+----------------------------------+---------+
| events_stages_current            | NO      |
| events_stages_history            | NO      |
| events_stages_history_long       | NO      |
| events_statements_cpu            | NO      |
| events_statements_current        | YES     |
| events_statements_history        | YES     |
| events_statements_history_long   | NO      |
| events_transactions_current      | YES     |
| events_transactions_history      | YES     |
| events_transactions_history_long | NO      |
| events_waits_current             | NO      |
| events_waits_history             | NO      |
| events_waits_history_long        | NO      |
| global_instrumentation           | YES     |
| thread_instrumentation           | YES     |
| statements_digest                | YES     |
+----------------------------------+---------+
```

要控制性能模式是否将使用者作为事件信息的目标，请设置其`启用`价值

有关设置表以及如何使用它们控制事件收集的更多信息，请参阅[第27.4.2节，“性能模式事件筛选”](performance-schema-filtering.html).

有些杂项表格不属于前面的任何一组。例如[`性能计时器`](performance-schema-performance-timers-table.html)列出可用的事件计时器及其特性。有关计时器的信息，请参阅[第27.4.1节，“性能模式事件计时”](performance-schema-timing.html).
