### 27.19.1 使用性能模式的查询分析

以下示例演示如何使用 Performance Schema 语句事件和阶段事件来检索与提供的分析信息相当的数据[`显示资料`](show-profiles.html)和[`显示资料`](show-profile.html)陈述。

这[`setup_actors`](performance-schema-setup-actors-table.html)table 可用于限制按主机、用户或帐户收集历史事件，以减少运行时开销和历史表中收集的数据量。该示例的第一步显示了如何将历史事件的收集限制为特定用户。

Performance Schema 以皮秒（万亿分之一秒）为单位显示事件计时器信息，以将计时数据标准化为标准单位。在以下示例中，`TIMER_WAIT`值除以 1000000000000 以显示以秒为单位的数据。值也被截断到小数点后 6 位，以便以相同的格式显示数据[`显示资料`](show-profiles.html)和[`显示资料`](show-profile.html)陈述。

1.  将历史事件的集合限制为运行查询的用户。默认，[`setup_actors`](performance-schema-setup-actors-table.html)配置为允许对所有前台线程进行监视和历史事件收集：

    ```
    mysql> SELECT * FROM performance_schema.setup_actors;
    +------+------+------+---------+---------+
    | HOST | USER | ROLE | ENABLED | HISTORY |
    +------+------+------+---------+---------+
    | %    | %    | %    | YES     | YES     |
    +------+------+------+---------+---------+
    ```

    更新默认行[`setup_actors`](performance-schema-setup-actors-table.html)表禁用所有前台线程的历史事件收集和监控，并插入一个新行，为运行查询的用户启用监控和历史事件收集：

    ```
    mysql> UPDATE performance_schema.setup_actors
           SET ENABLED = 'NO', HISTORY = 'NO'
           WHERE HOST = '%' AND USER = '%';

    mysql> INSERT INTO performance_schema.setup_actors
           (HOST,USER,ROLE,ENABLED,HISTORY)
           VALUES('localhost','test_user','%','YES','YES');
    ```

    中的数据[`setup_actors`](performance-schema-setup-actors-table.html)表现在应该类似于以下内容：

    ```
    mysql> SELECT * FROM performance_schema.setup_actors;
    +-----------+-----------+------+---------+---------+
    | HOST      | USER      | ROLE | ENABLED | HISTORY |
    +-----------+-----------+------+---------+---------+
    | %         | %         | %    | NO      | NO      |
    | localhost | test_user | %    | YES     | YES     |
    +-----------+-----------+------+---------+---------+
    ```

2.  通过更新[`setup_instruments`](performance-schema-setup-instruments-table.html)桌子。某些仪器可能已经默认启用。

    ```
    mysql> UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES', TIMED = 'YES'
           WHERE NAME LIKE '%statement/%';

    mysql> UPDATE performance_schema.setup_instruments
           SET ENABLED = 'YES', TIMED = 'YES'
           WHERE NAME LIKE '%stage/%';
    ```

3.  确保这件事`events_statements_*`和`events_stages_*`消费者已启用。一些消费者可能已经默认启用。

    ```
    mysql> UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
           WHERE NAME LIKE '%events_statements_%';

    mysql> UPDATE performance_schema.setup_consumers
           SET ENABLED = 'YES'
           WHERE NAME LIKE '%events_stages_%';
    ```

4.  在您正在监视的用户帐户下，运行您要分析的语句。例如：

    ```
    mysql> SELECT * FROM employees.employees WHERE emp_no = 10001;
    +--------+------------+------------+-----------+--------+------------+
    | emp_no | birth_date | first_name | last_name | gender | hire_date |
    +--------+------------+------------+-----------+--------+------------+
    |  10001 | 1953-09-02 | Georgi     | Facello   | M      | 1986-06-26 |
    +--------+------------+------------+-----------+--------+------------+
    ```

5.  识别`EVENT_ID`通过查询语句的[`events_statements_history_long`](performance-schema-events-statements-history-long-table.html)桌子。这一步类似于运行[`显示资料`](show-profiles.html)识别`查询ID`.以下查询产生类似于[`显示资料`](show-profiles.html)：

    ```
    mysql> SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT
           FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%10001%';
    +----------+----------+--------------------------------------------------------+
    | event_id | duration | sql_text                                               |
    +----------+----------+--------------------------------------------------------+
    |       31 | 0.028310 | SELECT * FROM employees.employees WHERE emp_no = 10001 |
    +----------+----------+--------------------------------------------------------+
    ```

6.  查询[`events_stages_history_long`](performance-schema-events-stages-history-long-table.html)表来检索语句的阶段事件。阶段使用事件嵌套链接到语句。每个阶段事件记录都有一个`NESTING_EVENT_ID`包含`EVENT_ID`的父声明。

    ```
    mysql> SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration
           FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=31;
    +--------------------------------+----------+
    | Stage                          | Duration |
    +--------------------------------+----------+
    | stage/sql/starting             | 0.000080 |
    | stage/sql/checking permissions | 0.000005 |
    | stage/sql/Opening tables       | 0.027759 |
    | stage/sql/init                 | 0.000052 |
    | stage/sql/System lock          | 0.000009 |
    | stage/sql/optimizing           | 0.000006 |
    | stage/sql/statistics           | 0.000082 |
    | stage/sql/preparing            | 0.000008 |
    | stage/sql/executing            | 0.000000 |
    | stage/sql/Sending data         | 0.000017 |
    | stage/sql/end                  | 0.000001 |
    | stage/sql/query end            | 0.000004 |
    | stage/sql/closing tables       | 0.000006 |
    | stage/sql/freeing items        | 0.000272 |
    | stage/sql/cleaning up          | 0.000001 |
    +--------------------------------+----------+
    ```
