要使用 MySQL 性能模式,必须在服务器启动时启用它,以便进行事件收集。To use the MySQL Performance Schema, it must be enabled at server startup to enable event collection to occur.
性能模式(Performance Schema)默认启用。要显式启用或禁用它,请以 performance_schema 变量设置为适当的值来启动服务器。例如,在服务器 my.cnf 文件中使用这些行:The Performance Schema is enabled by default. To enable or disable
it explicitly, start the server with the
performance_schema variable set
to an appropriate value. For example, use these lines in the
server my.cnf file:
[mysqld] performance_schema=ON
如果服务器在性能模式(Performance Schema)初始化期间无法分配任何内部缓冲区,性能模式会自行禁用,并将 performance_schema 设置为 OFF,服务器将以无监测方式运行。If the server is unable to allocate any internal buffer during
Performance Schema initialization, the Performance Schema disables
itself and sets
performance_schema to
OFF, and the server runs without
instrumentation.
性能模式还允许在服务器启动时配置监测器(instrument)和消费者(consumer)。The Performance Schema also permits instrument and consumer configuration at server startup.
要在服务器启动时控制监测器,请使用如下形式的选项:To control an instrument at server startup, use an option of this form:
--performance-schema-instrument='instrument_name=value'
这里,instrument_name 是监测器名称,例如 wait/synch/mutex/sql/LOCK_open,而 value 是以下值之一:Here, instrument_name is an instrument
name such as wait/synch/mutex/sql/LOCK_open,
and value is one of these values:
OFF、FALSE或0:禁用该监测器OFF,FALSE, or0: Disable the instrumentON,TRUE或1:启用并计时该工具ON,TRUE, or1: Enable and time the instrumentCOUNTED:启用并计数(而不是计时)该工具COUNTED: Enable and count (rather than time) the instrument
每个 --performance-schema-instrument 选项只能指定一个工具名称,但可以提供多个该选项来配置多个工具。此外,工具名称允许使用模式来配置与模式匹配的工具。要将所有条件同步工具配置为启用并计数,请使用此选项:Each
--performance-schema-instrument
option can specify only one instrument name, but multiple
instances of the option can be given to configure multiple
instruments. In addition, patterns are permitted in instrument
names to configure instruments that match the pattern. To
configure all condition synchronization instruments as enabled and
counted, use this option:
--performance-schema-instrument='wait/synch/cond/%=COUNTED'
要禁用所有工具,请使用此选项:To disable all instruments, use this option:
--performance-schema-instrument='%=OFF'
例外:memory/performance_schema/% 工具是内置的,启动时无法禁用。Exception: The memory/performance_schema/%
instruments are built in and cannot be disabled at startup.
较长的仪器名称字符串优先于较短的模式名称,无论顺序如何。有关指定模式以选择仪器的信息,请参见 第 29.4.9 节,“为过滤操作命名仪器或消费者”。Longer instrument name strings take precedence over shorter pattern names, regardless of order. For information about specifying patterns to select instruments, see Section 29.4.9, “Naming Instruments or Consumers for Filtering Operations”.
无法识别的仪器名称将被忽略。安装的插件可能稍后会创建该仪器,到那时该名称将被识别并配置。An unrecognized instrument name is ignored. It is possible that a plugin installed later may create the instrument, at which time the name is recognized and configured.
要在服务器启动时控制消费者,请使用如下形式的选项:To control a consumer at server startup, use an option of this form:
--performance-schema-consumer-consumer_name=value
这里,consumer_name 是消费者名称,例如 events_waits_history,而 value 是以下值之一:Here, consumer_name is a consumer name
such as events_waits_history, and
value is one of these values:
OFF、FALSE或0:不为该消费者收集事件OFF,FALSE, or0: Do not collect events for the consumerON、TRUE或1:为该消费者收集事件ON,TRUE, or1: Collect events for the consumer
例如,要启用 events_waits_history 消费者,请使用此选项:For example, to enable the events_waits_history
consumer, use this option:
--performance-schema-consumer-events-waits-history=ON
允许的消费者名称可以通过检查 setup_consumers 表来找到。不允许使用模式。在 setup_consumers 表中的消费者名称使用下划线,但对于启动时设置的消费者,名称中的破折号和下划线是等效的。The permitted consumer names can be found by examining the
setup_consumers table. Patterns are
not permitted. Consumer names in the
setup_consumers table use
underscores, but for consumers set at startup, dashes and
underscores within the name are equivalent.
性能模式(Performance Schema)包含几个提供配置信息的系统变量:The Performance Schema includes several system variables that provide configuration information:
mysql> SHOW VARIABLES LIKE 'perf%';
+--------------------------------------------------------+---------+
| Variable_name | Value |
+--------------------------------------------------------+---------+
| performance_schema | ON |
| performance_schema_accounts_size | 100 |
| performance_schema_digests_size | 200 |
| performance_schema_events_stages_history_long_size | 10000 |
| performance_schema_events_stages_history_size | 10 |
| performance_schema_events_statements_history_long_size | 10000 |
| performance_schema_events_statements_history_size | 10 |
| performance_schema_events_waits_history_long_size | 10000 |
| performance_schema_events_waits_history_size | 10 |
| performance_schema_hosts_size | 100 |
| performance_schema_max_cond_classes | 80 |
| performance_schema_max_cond_instances | 1000 |
...
performance_schema 变量为 ON 或 OFF,用于指示性能模式是否启用。其他变量指示表的大小(行数)或内存分配值。The performance_schema variable
is ON or OFF to indicate
whether the Performance Schema is enabled or disabled. The other
variables indicate table sizes (number of rows) or memory
allocation values.
启用性能模式时,性能模式实例的数量会影响服务器的内存占用,可能影响很大。性能模式会自动调整许多参数,仅在需要时使用内存;详见 第 29.17 节,“性能模式内存分配模型”。With the Performance Schema enabled, the number of Performance Schema instances affects the server memory footprint, perhaps to a large extent. The Performance Schema autoscales many parameters to use memory only as required; see Section 29.17, “The Performance Schema Memory-Allocation Model”.
要更改 Performance Schema 系统变量的值,请在服务器启动时设置它们。例如,将以下行放入 my.cnf 文件中,以更改等待事件历史表的大小:To change the value of Performance Schema system variables, set
them at server startup. For example, put the following lines in a
my.cnf file to change the sizes of the
history tables for wait events:
[mysqld] performance_schema performance_schema_events_waits_history_size=20 performance_schema_events_waits_history_long_size=15000
如果性能模式的几个参数未被显式设置,它会在服务器启动时自动调整这些参数的值。例如,它会以这种方式调整控制事件等待表大小的参数。性能模式按增量分配内存,根据实际服务器负载调整内存使用,而不是在服务器启动时分配所需的全部内存。因此,许多大小参数根本无需设置。要查看哪些参数是自动调整或自动缩放的,请使用 mysqld --verbose --help 并检查选项说明,或参见 第 29.15 节,“性能模式系统变量”。The Performance Schema automatically sizes the values of several of its parameters at server startup if they are not set explicitly. For example, it sizes the parameters that control the sizes of the events waits tables this way. The Performance Schema allocates memory incrementally, scaling its memory use to actual server load, instead of allocating all the memory it needs during server startup. Consequently, many sizing parameters need not be set at all. To see which parameters are autosized or autoscaled, use mysqld --verbose --help and examine the option descriptions, or see Section 29.15, “Performance Schema System Variables”.
对于每个在服务器启动时未设置的自动调整参数,性能模式会根据以下系统值的值来确定如何设置其值,这些系统值被视为有关您如何配置 MySQL 服务器的“提示”:For each autosized parameter that is not set at server startup, the Performance Schema determines how to set its value based on the value of the following system values, which are considered as “hints” about how you have configured your MySQL server:
max_connections open_files_limit table_definition_cache table_open_cache
要覆盖给定参数的自动尺寸调整或自动缩放,请在启动时将其设置为除 −1 以外的值。在这种情况下,性能模式将为其分配指定的值。To override autosizing or autoscaling for a given parameter, set it to a value other than −1 at startup. In this case, the Performance Schema assigns it the specified value.
在运行时,SHOW VARIABLES 显示自动调整参数实际设置的值。自动缩放参数显示为 −1。At runtime, SHOW VARIABLES displays
the actual values that autosized parameters were set to.
Autoscaled parameters display with a value of −1.
如果性能模式(Performance Schema)被禁用,其自动大小(autosized)和自动缩放(autoscaled)参数仍保持为 −1,并且 SHOW VARIABLES 会显示 −1。If the Performance Schema is disabled, its autosized and
autoscaled parameters remain set to −1 and
SHOW VARIABLES displays −1.