性能模式会记录阶段,这是语句执行过程中的步骤,例如解析语句、打开表或执行 filesort 操作。阶段对应于 SHOW
PROCESSLIST 显示的线程状态,或在 Information Schema PROCESSLIST 表中可见的线程状态。当状态值发生变化时,阶段开始和结束。The Performance Schema instruments stages, which are steps
during the statement-execution process, such as parsing a
statement, opening a table, or performing a
filesort operation. Stages correspond to the
thread states displayed by SHOW
PROCESSLIST or that are visible in the Information
Schema PROCESSLIST table. Stages
begin and end when state values change.
在事件层次结构中,等待事件嵌套在阶段事件中,阶段事件嵌套在语句事件中,语句事件嵌套在事务事件中。Within the event hierarchy, wait events nest within stage events, which nest within statement events, which nest within transaction events.
这些表存储阶段事件:These tables store stage events:
events_stages_current:每个线程的当前阶段事件。events_stages_current: The current stage event for each thread.events_stages_history:每个线程最近结束的阶段事件。events_stages_history: The most recent stage events that have ended per thread.events_stages_history_long:最近全球(跨所有线程)结束的阶段事件。events_stages_history_long: The most recent stage events that have ended globally (across all threads).
以下各节描述了阶段事件表。还有汇总表,用于聚合阶段事件的信息;请参见 第 29.12.20.2 节,“阶段汇总表”。The following sections describe the stage event tables. There are also summary tables that aggregate information about stage events; see Section 29.12.20.2, “Stage Summary Tables”.
有关三个阶段事件表之间关系的更多信息,请参见 第 29.9 节,“当前和历史事件的性能模式表”。For more information about the relationship between the three stage event tables, see Section 29.9, “Performance Schema Tables for Current and Historical Events”.
配置阶段事件收集Configuring Stage Event Collection
要控制是否收集阶段事件,请设置相关仪器和消费者的状态:To control whether to collect stage events, set the state of the relevant instruments and consumers:
setup_instruments表包含名称以stage开头的仪器。使用这些仪器来启用或禁用单个阶段事件类的收集。Thesetup_instrumentstable contains instruments with names that begin withstage. Use these instruments to enable or disable collection of individual stage event classes.setup_consumers表包含名称与当前和历史阶段事件表名对应的消费者值。使用这些消费者来过滤阶段事件的收集。Thesetup_consumerstable contains consumer values with names corresponding to the current and historical stage event table names. Use these consumers to filter collection of stage events.
除了那些提供语句进度信息的工具之外,阶段工具默认情况下是禁用的。例如:Other than those instruments that provide statement progress information, the stage instruments are disabled by default. For example:
mysql>SELECT NAME, ENABLED, TIMEDFROM performance_schema.setup_instrumentsWHERE NAME RLIKE 'stage/sql/[a-c]';+----------------------------------------------------+---------+-------+ | NAME | ENABLED | TIMED | +----------------------------------------------------+---------+-------+ | stage/sql/After create | NO | NO | | stage/sql/allocating local table | NO | NO | | stage/sql/altering table | NO | NO | | stage/sql/committing alter table to storage engine | NO | NO | | stage/sql/Changing master | NO | NO | | stage/sql/Checking master version | NO | NO | | stage/sql/checking permissions | NO | NO | | stage/sql/cleaning up | NO | NO | | stage/sql/closing tables | NO | NO | | stage/sql/Connecting to master | NO | NO | | stage/sql/converting HEAP to MyISAM | NO | NO | | stage/sql/Copying to group table | NO | NO | | stage/sql/Copying to tmp table | NO | NO | | stage/sql/copy to tmp table | NO | NO | | stage/sql/Creating sort index | NO | NO | | stage/sql/creating table | NO | NO | | stage/sql/Creating tmp table | NO | NO | +----------------------------------------------------+---------+-------+
提供语句进度信息的阶段事件工具默认情况下是启用并计时的:Stage event instruments that provide statement progress information are enabled and timed by default:
mysql>SELECT NAME, ENABLED, TIMEDFROM performance_schema.setup_instrumentsWHERE ENABLED='YES' AND NAME LIKE "stage/%";+------------------------------------------------------+---------+-------+ | NAME | ENABLED | TIMED | +------------------------------------------------------+---------+-------+ | stage/sql/copy to tmp table | YES | YES | | stage/sql/Applying batch of row changes (write) | YES | YES | | stage/sql/Applying batch of row changes (update) | YES | YES | | stage/sql/Applying batch of row changes (delete) | YES | YES | | stage/innodb/alter table (end) | YES | YES | | stage/innodb/alter table (flush) | YES | YES | | stage/innodb/alter table (insert) | YES | YES | | stage/innodb/alter table (log apply index) | YES | YES | | stage/innodb/alter table (log apply table) | YES | YES | | stage/innodb/alter table (merge sort) | YES | YES | | stage/innodb/alter table (read PK and internal sort) | YES | YES | | stage/innodb/buffer pool load | YES | YES | | stage/innodb/clone (file copy) | YES | YES | | stage/innodb/clone (redo copy) | YES | YES | | stage/innodb/clone (page copy) | YES | YES | +------------------------------------------------------+---------+-------+
阶段消费者默认情况下是禁用的:The stage consumers are disabled by default:
mysql>SELECT *FROM performance_schema.setup_consumersWHERE NAME LIKE 'events_stages%';+----------------------------+---------+ | NAME | ENABLED | +----------------------------+---------+ | events_stages_current | NO | | events_stages_history | NO | | events_stages_history_long | NO | +----------------------------+---------+
要在服务器启动时控制阶段事件收集,请在您的 my.cnf 文件中使用如下行:To control stage event collection at server startup, use lines
like these in your my.cnf file:
启用:Enable:
[mysqld] performance-schema-instrument='stage/%=ON' performance-schema-consumer-events-stages-current=ON performance-schema-consumer-events-stages-history=ON performance-schema-consumer-events-stages-history-long=ON
禁用:Disable:
[mysqld] performance-schema-instrument='stage/%=OFF' performance-schema-consumer-events-stages-current=OFF performance-schema-consumer-events-stages-history=OFF performance-schema-consumer-events-stages-history-long=OFF
要在运行时控制阶段事件收集,请更新 setup_instruments 和 setup_consumers 表:To control stage 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 'stage/%'; UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE 'events_stages%';
禁用:Disable:
UPDATE performance_schema.setup_instruments SET ENABLED = 'NO', TIMED = 'NO' WHERE NAME LIKE 'stage/%'; UPDATE performance_schema.setup_consumers SET ENABLED = 'NO' WHERE NAME LIKE 'events_stages%';
要仅收集特定的阶段事件,请只启用相应的阶段工具。要仅收集特定阶段事件表的阶段事件,请启用阶段工具,但只启用与所需表对应的阶段消费者。To collect only specific stage events, enable only the corresponding stage instruments. To collect stage events only for specific stage event tables, enable the stage instruments but only the stage 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”.
阶段事件进度信息Stage Event Progress Information
Performance Schema 阶段事件表包含两列,结合起来可为每行提供阶段进度指示器:The Performance Schema stage event tables contain two columns that, taken together, provide a stage progress indicator for each row:
WORK_COMPLETED:阶段已完成的工作单元数WORK_COMPLETED: The number of work units completed for the stageWORK_ESTIMATED:阶段预期的工作单元数WORK_ESTIMATED: The number of work units expected for the stage
如果没有提供某个工具的信息,每一列都是NULL。如果信息可用,对其的解释完全依赖于工具的实现。性能模式表提供了存储进度数据的容器,但不对度量本身的语义做任何假设:Each column is NULL if no progress
information is provided for an instrument. Interpretation of the
information, if it is available, depends entirely on the
instrument implementation. The Performance Schema tables provide
a container to store progress data, but make no assumptions
about the semantics of the metric itself:
一个“工作单位”是一个整数度量值,在执行过程中会随时间增加,例如处理的字节数、行数、文件数或表数。特定工具的“工作单位”定义由提供数据的工具代码决定。A “work unit” is an integer metric that increases over time during execution, such as the number of bytes, rows, files, or tables processed. The definition of “work unit” for a particular instrument is left to the instrumentation code providing the data.
WORK_COMPLETED的值可以一次增加一个或多个单位,具体取决于被监控的代码。TheWORK_COMPLETEDvalue can increase one or many units at a time, depending on the instrumented code.WORK_ESTIMATED的值可以在阶段期间变化,这取决于已插装的代码。TheWORK_ESTIMATEDvalue can change during the stage, depending on the instrumented code.
阶段事件进度指示器的插装可以实现以下任何行为:Instrumentation for a stage event progress indicator can implement any of the following behaviors:
无进度插装No progress instrumentation
这是最典型的情况,不提供进度数据。
WORK_COMPLETED和WORK_ESTIMATED列都是NULL。This is the most typical case, where no progress data is provided. TheWORK_COMPLETEDandWORK_ESTIMATEDcolumns are bothNULL.无界进度插装Unbounded progress instrumentation
只有
WORK_COMPLETED列有意义。WORK_ESTIMATED列没有提供数据,显示为 0。Only theWORK_COMPLETEDcolumn is meaningful. No data is provided for theWORK_ESTIMATEDcolumn, which displays 0.通过查询
events_stages_current表中的被监控会话,监控应用程序可以报告到目前为止已经完成了多少工作,但无法报告阶段是否接近完成。目前,没有阶段以这种方式进行计量。By querying theevents_stages_currenttable for the monitored session, a monitoring application can report how much work has been performed so far, but cannot report whether the stage is near completion. Currently, no stages are instrumented like this.有界进度计量Bounded progress instrumentation
WORK_COMPLETED和WORK_ESTIMATED列都是有意义的。TheWORK_COMPLETEDandWORK_ESTIMATEDcolumns are both meaningful.这种类型的进度指示器适用于具有明确完成标准的操作,例如稍后描述的表复制工具。通过查询受监控会话的
events_stages_current表,监控应用程序可以报告到目前为止已经完成了多少工作,并可以通过计算WORK_COMPLETED/WORK_ESTIMATED比例来报告阶段的总体完成百分比。This type of progress indicator is appropriate for an operation with a defined completion criterion, such as the table-copy instrument described later. By querying theevents_stages_currenttable for the monitored session, a monitoring application can report how much work has been performed so far, and can report the overall completion percentage for the stage, by computing theWORK_COMPLETED/WORK_ESTIMATEDratio.
stage/sql/copy to tmp table 工具说明了进度指示器的工作原理。在执行 ALTER TABLE 语句时,会使用 stage/sql/copy to tmp table 阶段,根据要复制的数据量,这个阶段可能会执行很长时间。The stage/sql/copy to tmp table instrument
illustrates how progress indicators work. During execution of an
ALTER TABLE statement, the
stage/sql/copy to tmp table stage is used,
and this stage can execute potentially for a long time,
depending on the size of the data to copy.
表复制任务有一个已定义的终止条件(所有行已复制),并且 stage/sql/copy to tmp table 阶段会提供有限的进度信息:所使用的工作单元是已复制的行数,WORK_COMPLETED 和 WORK_ESTIMATED 都是有意义的,它们的比值表示任务完成的百分比。The table-copy task has a defined termination (all rows copied),
and the stage/sql/copy to tmp table stage is
instrumented to provided bounded progress information: The work
unit used is number of rows copied,
WORK_COMPLETED and
WORK_ESTIMATED are both meaningful, and their
ratio indicates task percentage complete.
要启用该工具和相关的使用者,请执行以下语句:To enable the instrument and the relevant consumers, execute these statements:
UPDATE performance_schema.setup_instruments SET ENABLED='YES' WHERE NAME='stage/sql/copy to tmp table'; UPDATE performance_schema.setup_consumers SET ENABLED='YES' WHERE NAME LIKE 'events_stages_%';
要查看正在进行的 ALTER
TABLE 语句的进度,请从 events_stages_current 表中进行选择。To see the progress of an ongoing ALTER
TABLE statement, select from the
events_stages_current table.