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

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

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

29.12.6 性能模式语句事件表29.12.6 Performance Schema Statement Event Tables

性能模式(Performance Schema)对语句执行进行监控。语句事件发生在事件层次结构的高层。在事件层次结构中,等待事件(wait events)嵌套在阶段事件(stage events)中,阶段事件嵌套在语句事件(statement events)中,而语句事件又嵌套在事务事件(transaction events)中。The Performance Schema instruments statement execution. Statement events occur at a high level of the event hierarchy. Within the event hierarchy, wait events nest within stage events, which nest within statement events, which nest within transaction events.

这些表存储语句事件:These tables store statement events:

以下各节描述了语句事件表。同时,还有汇总表用于聚合语句事件的信息;请参见 第 29.12.20.3 节,“语句汇总表”。The following sections describe the statement event tables. There are also summary tables that aggregate information about statement events; see Section 29.12.20.3, “Statement Summary Tables”.

有关三个 events_statements_xxx 事件表之间关系的更多信息,请参见 第 29.9 节,“当前和历史事件的性能模式表”。For more information about the relationship between the three events_statements_xxx event tables, see Section 29.9, “Performance Schema Tables for Current and Historical Events”.

配置语句事件收集Configuring Statement Event Collection

要控制是否收集语句事件,请设置相关仪器和消费者的状态:To control whether to collect statement events, set the state of the relevant instruments and consumers:

  • setup_instruments 表包含名称以 statement 开头的仪器。使用这些仪器可以启用或禁用单个语句事件类别的收集。The setup_instruments table contains instruments with names that begin with statement. Use these instruments to enable or disable collection of individual statement event classes.

  • setup_consumers 表包含具有名称的消费者值,这些名称对应于当前和历史语句事件表的名称,以及语句摘要消费者。使用这些消费者来过滤语句事件的收集和语句摘要。The setup_consumers table contains consumer values with names corresponding to the current and historical statement event table names, and the statement digest consumer. Use these consumers to filter collection of statement events and statement digesting.

语句工具默认是启用的,并且 events_statements_current、events_statements_history 和 statements_digest 语句消费者默认启用:The statement instruments are enabled by default, and the events_statements_current, events_statements_history, and statements_digest statement consumers are enabled by default:

mysql> SELECT NAME, ENABLED, TIMED
       FROM performance_schema.setup_instruments
       WHERE NAME LIKE 'statement/%';
+---------------------------------------------+---------+-------+
| NAME                                        | ENABLED | TIMED |
+---------------------------------------------+---------+-------+
| statement/sql/select                        | YES     | YES   |
| statement/sql/create_table                  | YES     | YES   |
| statement/sql/create_index                  | YES     | YES   |
...
| statement/sp/stmt                           | YES     | YES   |
| statement/sp/set                            | YES     | YES   |
| statement/sp/set_trigger_field              | YES     | YES   |
| statement/scheduler/event                   | YES     | YES   |
| statement/com/Sleep                         | YES     | YES   |
| statement/com/Quit                          | YES     | YES   |
| statement/com/Init DB                       | YES     | YES   |
...
| statement/abstract/Query                    | YES     | YES   |
| statement/abstract/new_packet               | YES     | YES   |
| statement/abstract/relay_log                | YES     | YES   |
+---------------------------------------------+---------+-------+
mysql> SELECT *
       FROM performance_schema.setup_consumers
       WHERE NAME LIKE '%statements%';
+--------------------------------+---------+
| NAME                           | ENABLED |
+--------------------------------+---------+
| events_statements_current      | YES     |
| events_statements_history      | YES     |
| events_statements_history_long | NO      |
| statements_digest              | YES     |
+--------------------------------+---------+

要在服务器启动时控制语句事件收集,请在您的 my.cnf 文件中使用如下行:To control statement event collection at server startup, use lines like these in your my.cnf file:

  • 启用:Enable:

    [mysqld]
    performance-schema-instrument='statement/%=ON'
    performance-schema-consumer-events-statements-current=ON
    performance-schema-consumer-events-statements-history=ON
    performance-schema-consumer-events-statements-history-long=ON
    performance-schema-consumer-statements-digest=ON
    
  • 禁用:Disable:

    [mysqld]
    performance-schema-instrument='statement/%=OFF'
    performance-schema-consumer-events-statements-current=OFF
    performance-schema-consumer-events-statements-history=OFF
    performance-schema-consumer-events-statements-history-long=OFF
    performance-schema-consumer-statements-digest=OFF
    

要在运行时控制语句事件收集,请更新 setup_instruments 和 setup_consumers 表:To control statement event collection at runtime, update the setup_instruments and setup_consumers tables:

  • 启用:Enable:

    UPDATE performance_schema.setup_instruments
    SET ENABLED = 'YES', TIMED = 'YES'
    WHERE NAME LIKE 'statement/%';
    
    UPDATE performance_schema.setup_consumers
    SET ENABLED = 'YES'
    WHERE NAME LIKE '%statements%';
    
  • 禁用:Disable:

    UPDATE performance_schema.setup_instruments
    SET ENABLED = 'NO', TIMED = 'NO'
    WHERE NAME LIKE 'statement/%';
    
    UPDATE performance_schema.setup_consumers
    SET ENABLED = 'NO'
    WHERE NAME LIKE '%statements%';
    

要只收集特定的语句事件,请仅启用相应的语句工具。要仅为特定语句事件表收集语句事件,请启用语句工具,但只启用对应所需表的语句消费者。To collect only specific statement events, enable only the corresponding statement instruments. To collect statement events only for specific statement event tables, enable the statement instruments but only the statement consumers corresponding to the desired tables.

有关配置事件收集的更多信息,请参见第 29.3 节,“性能模式启动配置”,以及第 29.4 节,“性能模式运行时配置”。For additional information about configuring event collection, see Section 29.3, “Performance Schema Startup Configuration”, and Section 29.4, “Performance Schema Runtime Configuration”.

语句监控Statement Monitoring

语句监控从服务器看到线程上活动被请求的那一刻开始,到所有活动停止为止。通常,这意味着从服务器收到客户端的第一个数据包的时间,到服务器完成发送响应的时间。存储程序中的语句像其他语句一样受到监控。Statement monitoring begins from the moment the server sees that activity is requested on a thread, to the moment when all activity has ceased. Typically, this means from the time the server gets the first packet from the client to the time the server has finished sending the response. Statements within stored programs are monitored like other statements.

当性能架构对请求(服务器命令或 SQL 语句)进行检测时,它使用的检测器名称按阶段从更一般(或“抽象””)到更具体的顺序,直到达到最终的检测器名称。When the Performance Schema instruments a request (server command or SQL statement), it uses instrument names that proceed in stages from more general (or “abstract”) to more specific until it arrives at a final instrument name.

最终的检测器名称对应于服务器命令和 SQL 语句:Final instrument names correspond to server commands and SQL statements:

  • 服务器命令对应于 COM_xxx codes,这些代码在 mysql_com.h 头文件中定义,并在 sql/sql_parse.cc 中处理。示例有 COM_PING 和 COM_QUIT。命令的工具名称以 statement/com 开头,例如 statement/com/Ping 和 statement/com/Quit。Server commands correspond to the COM_xxx codes defined in the mysql_com.h header file and processed in sql/sql_parse.cc. Examples are COM_PING and COM_QUIT. Instruments for commands have names that begin with statement/com, such as statement/com/Ping and statement/com/Quit.

  • SQL 语句以文本形式表示,例如 DELETE FROM t1 或 SELECT * FROM t2。SQL 语句的工具名称以 statement/sql 开头,例如 statement/sql/delete 和 statement/sql/select。SQL statements are expressed as text, such as DELETE FROM t1 or SELECT * FROM t2. Instruments for SQL statements have names that begin with statement/sql, such as statement/sql/delete and statement/sql/select.

一些最终的工具名称是特定于错误处理的:Some final instrument names are specific to error handling:

  • statement/com/Error 用于处理服务器接收到的带外消息。它可用于检测服务器无法理解的客户端发送的命令。这可能有助于识别配置错误的客户端,或者使用比服务器更新的 MySQL 版本的客户端,或试图攻击服务器的客户端。statement/com/Error accounts for messages received by the server that are out of band. It can be used to detect commands sent by clients that the server does not understand. This may be helpful for purposes such as identifying clients that are misconfigured or using a version of MySQL more recent than that of the server, or clients that are attempting to attack the server.

  • statement/sql/error 用于统计无法解析的 SQL 语句。它可以用来检测客户端发送的格式错误的查询。解析失败的查询不同于解析成功但在执行过程中出错的查询。例如,SELECT * FROM 是格式错误的,此时使用 statement/sql/error 工具。相比之下,SELECT * 可以解析,但会因 No tables used 错误而执行失败。在这种情况下,使用 statement/sql/select,语句事件会包含表明错误性质的信息。statement/sql/error accounts for SQL statements that fail to parse. It can be used to detect malformed queries sent by clients. A query that fails to parse differs from a query that parses but fails due to an error during execution. For example, SELECT * FROM is malformed, and the statement/sql/error instrument is used. By contrast, SELECT * parses but fails with a No tables used error. In this case, statement/sql/select is used and the statement event contains information to indicate the nature of the error.

请求可以来自以下任一来源:A request can be obtained from any of these sources:

  • 作为客户端的命令或语句请求,客户端以数据包形式发送请求As a command or statement request from a client, which sends the request as packets

  • 作为从副本的中继日志读取的语句字符串As a statement string read from the relay log on a replica

  • 作为事件调度器的事件As an event from the Event Scheduler

请求的详细信息最初是未知的,性能模式从抽象到具体的仪器名称,顺序取决于请求的来源。The details for a request are not initially known and the Performance Schema proceeds from abstract to specific instrument names in a sequence that depends on the source of the request.

对于来自客户端的请求:For a request received from a client:

  1. 当服务器在套接字级别检测到一个新数据包时,会以抽象仪器名称 statement/abstract/new_packet 开始一个新的语句。When the server detects a new packet at the socket level, a new statement is started with an abstract instrument name of statement/abstract/new_packet.

  2. 当服务器读取数据包编号时,它会更多地了解收到的请求类型,并且性能模式(Performance Schema)会细化仪表名称。例如,如果请求是一个 COM_PING 数据包,则仪表名称变为 statement/com/Ping,这是最终名称。如果请求是一个 COM_QUERY 数据包,则已知其对应一个 SQL 语句,但不是特定的语句类型。在这种情况下,仪表名称会从一个抽象名称变为一个更具体但仍然抽象的名称 statement/abstract/Query,并且该请求需要进一步分类。When the server reads the packet number, it knows more about the type of request received, and the Performance Schema refines the instrument name. For example, if the request is a COM_PING packet, the instrument name becomes statement/com/Ping and that is the final name. If the request is a COM_QUERY packet, it is known to correspond to an SQL statement but not the particular type of statement. In this case, the instrument changes from one abstract name to a more specific but still abstract name, statement/abstract/Query, and the request requires further classification.

  3. 如果请求是一个语句,则会读取语句文本并将其提供给解析器。解析后,将知道确切的语句类型。例如,如果请求是一个 INSERT 语句,Performance Schema 会将仪表名称从 statement/abstract/Query 精确到 statement/sql/insert,这是最终名称。If the request is a statement, the statement text is read and given to the parser. After parsing, the exact statement type is known. If the request is, for example, an INSERT statement, the Performance Schema refines the instrument name from statement/abstract/Query to statement/sql/insert, which is the final name.

对于从副本的中继日志中读取为语句的请求:For a request read as a statement from the relay log on a replica:

  1. 中继日志中的语句以文本形式存储,并按文本读取。没有网络协议,因此不使用 statement/abstract/new_packet 仪表。相反,初始仪表是 statement/abstract/relay_log。Statements in the relay log are stored as text and are read as such. There is no network protocol, so the statement/abstract/new_packet instrument is not used. Instead, the initial instrument is statement/abstract/relay_log.

  2. 当语句被解析时,确切的语句类型就会被识别。例如,如果请求是一个 INSERT 语句,性能模式会将工具名称从 statement/abstract/Query 优化为 statement/sql/insert,这就是最终名称。When the statement is parsed, the exact statement type is known. If the request is, for example, an INSERT statement, the Performance Schema refines the instrument name from statement/abstract/Query to statement/sql/insert, which is the final name.

上述描述仅适用于基于语句的复制。对于基于行的复制,在副本处理行更改时进行的表 I/O 可以被监控,但中继日志中的行事件不会以单独语句的形式出现。The preceding description applies only for statement-based replication. For row-based replication, table I/O done on the replica as it processes row changes can be instrumented, but row events in the relay log do not appear as discrete statements.

对于从事件调度器接收到的请求:For a request received from the Event Scheduler:

事件执行使用名称 statement/scheduler/event 进行监控。这是最终名称。The event execution is instrumented using the name statement/scheduler/event. This is the final name.

在事件主体中执行的语句使用 statement/sql/* 名称进行检测,而不使用任何前置的抽象检测。事件是一个存储程序,而存储程序在执行前会在内存中预编译。因此,在运行时不会进行解析,并且每条语句的类型在执行时已知。Statements executed within the event body are instrumented using statement/sql/* names, without use of any preceding abstract instrument. An event is a stored program, and stored programs are precompiled in memory before execution. Consequently, there is no parsing at runtime and the type of each statement is known by the time it executes.

在事件体内执行的语句是子语句。例如,如果一个事件执行了一个 INSERT 语句,那么事件本身的执行是父操作,使用 statement/scheduler/event 仪表化,而 INSERT 是子操作,使用 statement/sql/insert 仪表化。父/子关系存在于各自独立的仪表化操作 之间。这不同于发生在单个仪表化操作 内部 的抽象到最终仪表名称的细化序列。Statements executed within the event body are child statements. For example, if an event executes an INSERT statement, execution of the event itself is the parent, instrumented using statement/scheduler/event, and the INSERT is the child, instrumented using statement/sql/insert. The parent/child relationship holds between separate instrumented operations. This differs from the sequence of refinement that occurs within a single instrumented operation, from abstract to final instrument names.

For statistics to be collected for statements, it is not sufficient to enable only the final statement/sql/* instruments used for individual statement types. The abstract statement/abstract/* instruments must be enabled as well. This should not normally be an issue because all statement instruments are enabled by default. However, an application that enables or disables statement instruments selectively must take into account that disabling abstract instruments also disables statistics collection for the individual statement instruments. For example, to collect statistics for INSERT statements, statement/sql/insert must be enabled, but also statement/abstract/new_packet以及statement/abstract/Query. Similarly, for replicated statements to be instrumented, statement/abstract/relay_log must be enabled.For statistics to be collected for statements, it is not sufficient to enable only the final statement/sql/* instruments used for individual statement types. The abstract statement/abstract/* instruments must be enabled as well. This should not normally be an issue because all statement instruments are enabled by default. However, an application that enables or disables statement instruments selectively must take into account that disabling abstract instruments also disables statistics collection for the individual statement instruments. For example, to collect statistics for INSERT statements, statement/sql/insert must be enabled, but also statement/abstract/new_packet and statement/abstract/Query. Similarly, for replicated statements to be instrumented, statement/abstract/relay_log must be enabled.

抽象工具如 statement/abstract/Query 的统计数据不会被汇总,因为没有任何语句会被归类为以抽象工具作为最终语句名称。No statistics are aggregated for abstract instruments such as statement/abstract/Query because no statement is ever classified with an abstract instrument as the final statement name.