性能模式监视事务。在事件层级中,等待事件嵌套在阶段事件中,阶段事件嵌套在语句事件中,语句事件嵌套在事务事件中。The Performance Schema instruments transactions. Within the event hierarchy, wait events nest within stage events, which nest within statement events, which nest within transaction events.
这些表用于存储事务事件:These tables store transaction events:
events_transactions_current:每个线程的当前事务事件。events_transactions_current: The current transaction event for each thread.events_transactions_history:每个线程最近结束的事务事件。events_transactions_history: The most recent transaction events that have ended per thread.events_transactions_history_long:最近在全局(跨所有线程)结束的事务事件。events_transactions_history_long: The most recent transaction events that have ended globally (across all threads).
以下各节描述了事务事件表。还有一些汇总表,它们汇总了有关事务事件的信息;请参阅 第 29.12.20.5 节,“事务汇总表”。The following sections describe the transaction event tables. There are also summary tables that aggregate information about transaction events; see Section 29.12.20.5, “Transaction Summary Tables”.
有关这三个事务事件表之间关系的更多信息,请参阅 第 29.9 节,“当前和历史事件的性能模式表”。For more information about the relationship between the three transaction event tables, see Section 29.9, “Performance Schema Tables for Current and Historical Events”.
配置事务事件收集Configuring Transaction Event Collection
要控制是否收集事务事件,请设置相关设备和消费者的状态:To control whether to collect transaction events, set the state of the relevant instruments and consumers:
setup_instruments表包含一个名为transaction的工具。使用此工具可以启用或禁用单个事务事件类别的收集。Thesetup_instrumentstable contains an instrument namedtransaction. Use this instrument to enable or disable collection of individual transaction event classes.setup_consumers表包含与当前和历史事务事件表名称对应的消费者值。使用这些消费者可以筛选事务事件的收集。Thesetup_consumerstable contains consumer values with names corresponding to the current and historical transaction event table names. Use these consumers to filter collection of transaction events.
transaction 工具以及 events_transactions_current 和 events_transactions_history 事务消费者默认是启用的:The transaction instrument and the
events_transactions_current and
events_transactions_history transaction
consumers are enabled by default:
mysql>SELECT NAME, ENABLED, TIMEDFROM performance_schema.setup_instrumentsWHERE NAME = 'transaction';+-------------+---------+-------+ | NAME | ENABLED | TIMED | +-------------+---------+-------+ | transaction | YES | YES | +-------------+---------+-------+ mysql>SELECT *FROM performance_schema.setup_consumersWHERE NAME LIKE 'events_transactions%';+----------------------------------+---------+ | NAME | ENABLED | +----------------------------------+---------+ | events_transactions_current | YES | | events_transactions_history | YES | | events_transactions_history_long | NO | +----------------------------------+---------+
要在服务器启动时控制事务事件的收集,请在您的 my.cnf 文件中使用如下行:To control transaction event collection at server startup, use
lines like these in your my.cnf file:
启用:Enable:
[mysqld] performance-schema-instrument='transaction=ON' performance-schema-consumer-events-transactions-current=ON performance-schema-consumer-events-transactions-history=ON performance-schema-consumer-events-transactions-history-long=ON
禁用:Disable:
[mysqld] performance-schema-instrument='transaction=OFF' performance-schema-consumer-events-transactions-current=OFF performance-schema-consumer-events-transactions-history=OFF performance-schema-consumer-events-transactions-history-long=OFF
要在运行时控制事务事件的收集,请更新 setup_instruments 和 setup_consumers 表:To control transaction 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 = 'transaction'; UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE 'events_transactions%';
禁用:Disable:
UPDATE performance_schema.setup_instruments SET ENABLED = 'NO', TIMED = 'NO' WHERE NAME = 'transaction'; UPDATE performance_schema.setup_consumers SET ENABLED = 'NO' WHERE NAME LIKE 'events_transactions%';
要仅收集特定事务事件表的事务事件,请启用 transaction 工具,同时仅启用与所需表对应的事务消费者。To collect transaction events only for specific transaction
event tables, enable the transaction
instrument but only the transaction 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”.
事务边界Transaction Boundaries
在 MySQL 服务器中,事务通过以下语句显式开始:In MySQL Server, transactions start explicitly with these statements:
START TRANSACTION | BEGIN | XA START | XA BEGIN
事务也会隐式开始。例如,当autocommit 系统变量启用时,每条语句的开始都会启动一个新事务。Transactions also start implicitly. For example, when the
autocommit system variable is
enabled, the start of each statement starts a new transaction.
当autocommit 被禁用时,紧跟在已提交事务之后的第一条语句标志着新事务的开始。随后执行的语句都是该事务的一部分,直到事务被提交。When autocommit is disabled,
the first statement following a committed transaction marks the
start of a new transaction. Subsequent statements are part of
the transaction until it is committed.
事务可通过以下语句显式结束:Transactions explicitly end with these statements:
COMMIT | ROLLBACK | XA COMMIT | XA ROLLBACK
事务也可以隐式结束,例如执行 DDL 语句、锁定语句和服务器管理语句时。Transactions also end implicitly, by execution of DDL statements, locking statements, and server administration statements.
在下面的讨论中,对 START
TRANSACTION 的引用也适用于 BEGIN、XA
START 以及 XA
BEGIN。类似地,对 COMMIT 和 ROLLBACK 的引用分别适用于 XA
COMMIT 和 XA
ROLLBACK。In the following discussion, references to
START
TRANSACTION also apply to
BEGIN,
XA
START, and
XA
BEGIN. Similarly, references to
COMMIT and
ROLLBACK apply
to XA
COMMIT and
XA
ROLLBACK, respectively.
性能模式定义了与服务器类似的事务边界。事务事件的开始和结束与服务器中相应的状态转换紧密匹配:The Performance Schema defines transaction boundaries similarly to that of the server. The start and end of a transaction event closely match the corresponding state transitions in the server:
对于显式启动的事务,事务事件在处理
START TRANSACTION语句时开始。For an explicitly started transaction, the transaction event starts during processing of theSTART TRANSACTIONstatement.对于隐式启动的事务,事务事件在前一个事务结束后,首次使用事务引擎的语句时开始。For an implicitly started transaction, the transaction event starts on the first statement that uses a transactional engine after the previous transaction has ended.
对于任何事务,无论是显式还是隐式结束,当服务器在处理
COMMIT或ROLLBACK时退出活动事务状态,事务事件就会结束。For any transaction, whether explicitly or implicitly ended, the transaction event ends when the server transitions out of the active transaction state during the processing ofCOMMITorROLLBACK.
这种方法有一些微妙的含义:There are subtle implications to this approach:
性能模式中的事务事件并未完全包括与相应的
START TRANSACTION、COMMIT或ROLLBACK语句相关的语句事件。事务事件与这些语句之间存在微小的时间重叠。Transaction events in the Performance Schema do not fully include the statement events associated with the correspondingSTART TRANSACTION,COMMIT, orROLLBACKstatements. There is a trivial amount of timing overlap between the transaction event and these statements.对于非事务型引擎起作用的语句不会影响连接的事务状态。对于隐式事务,事务事件从第一个使用事务型引擎的语句开始。这意味着仅在非事务表上操作的语句会被忽略,即使在
START TRANSACTION之后也是如此。Statements that work with nontransactional engines have no effect on the transaction state of the connection. For implicit transactions, the transaction event begins with the first statement that uses a transactional engine. This means that statements operating exclusively on nontransactional tables are ignored, even followingSTART TRANSACTION.
举例说明,考虑以下场景:To illustrate, consider the following scenario:
1. SET autocommit = OFF;
2. CREATE TABLE t1 (a INT) ENGINE = InnoDB;
3. START TRANSACTION; -- Transaction 1 START
4. INSERT INTO t1 VALUES (1), (2), (3);
5. CREATE TABLE t2 (a INT) ENGINE = MyISAM; -- Transaction 1 COMMIT
-- (implicit; DDL forces commit)
6. INSERT INTO t2 VALUES (1), (2), (3); -- Update nontransactional table
7. UPDATE t2 SET a = a + 1; -- ... and again
8. INSERT INTO t1 VALUES (4), (5), (6); -- Write to transactional table
-- Transaction 2 START (implicit)
9. COMMIT; -- Transaction 2 COMMIT
从服务器的角度来看,当表t2创建时,事务 1 结束。尽管中间对非事务表进行了更新,但事务 2 直到访问事务型表时才开始。From the perspective of the server, Transaction 1 ends when
table t2 is created. Transaction 2 does not
start until a transactional table is accessed, despite the
intervening updates to nontransactional tables.
从性能模式的角度来看,当服务器进入活动事务状态时,事务 2 开始。语句 6 和 7 不包含在事务 2 的范围内,这与服务器将事务写入二进制日志的方式一致。From the perspective of the Performance Schema, Transaction 2 starts when the server transitions into an active transaction state. Statements 6 and 7 are not included within the boundaries of Transaction 2, which is consistent with how the server writes transactions to the binary log.
事务检测Transaction Instrumentation
三个属性定义事务:Three attributes define transactions:
访问模式(只读、读写)Access mode (read only, read write)
隔离级别(
SERIALIZABLE、REPEATABLE READ等)Isolation level (SERIALIZABLE,REPEATABLE READ, and so forth)隐式(
autocommit启用)或显式(autocommit禁用)Implicit (autocommitenabled) or explicit (autocommitdisabled)
为了降低事务检测的复杂性,并确保收集到的事务数据提供完整、有意义的结果,所有事务均独立于访问模式、隔离级别或自动提交模式进行检测。To reduce complexity of the transaction instrumentation and to ensure that the collected transaction data provides complete, meaningful results, all transactions are instrumented independently of access mode, isolation level, or autocommit mode.
要选择性地检查事务历史,请使用事务事件表中的属性列:ACCESS_MODE、ISOLATION_LEVEL 和 AUTOCOMMIT。To selectively examine transaction history, use the attribute
columns in the transaction event tables:
ACCESS_MODE,
ISOLATION_LEVEL, and
AUTOCOMMIT.
可以通过多种方式降低事务监控的成本,例如根据用户、账户、主机或线程(客户端连接)启用或禁用事务监控。The cost of transaction instrumentation can be reduced various ways, such as enabling or disabling transaction instrumentation according to user, account, host, or thread (client connection).
事务与嵌套事件Transactions and Nested Events
事务事件的父事件是启动该事务的事件。对于显式启动的事务,这包括START
TRANSACTION和COMMIT AND
CHAIN语句。对于隐式启动的事务,它是上一事务结束后使用事务引擎的第一条语句。The parent of a transaction event is the event that initiated
the transaction. For an explicitly started transaction, this
includes the START
TRANSACTION and
COMMIT AND
CHAIN statements. For an implicitly started
transaction, it is the first statement that uses a transactional
engine after the previous transaction ends.
一般来说,事务是事务期间启动的所有事件的顶层父级,包括明确结束事务的语句,例如 COMMIT 和 ROLLBACK。例外是隐式结束事务的语句,例如 DDL 语句,在这种情况下,必须在执行新语句之前提交当前事务。In general, a transaction is the top-level parent to all events
initiated during the transaction, including statements that
explicitly end the transaction such as
COMMIT and
ROLLBACK.
Exceptions are statements that implicitly end a transaction,
such as DDL statements, in which case the current transaction
must be committed before the new statement is executed.
事务和存储程序Transactions and Stored Programs
事务和存储程序事件的关系如下:Transactions and stored program events are related as follows:
存储过程Stored Procedures
存储过程独立于事务运行。存储过程可以在事务中启动,并且事务可以在存储过程中启动或结束。如果在事务中调用存储过程,存储过程可以执行强制提交父事务的语句,然后启动一个新的事务。Stored procedures operate independently of transactions. A stored procedure can be started within a transaction, and a transaction can be started or ended from within a stored procedure. If called from within a transaction, a stored procedure can execute statements that force a commit of the parent transaction and then start a new transaction.
如果存储过程在事务中启动,则该事务是存储过程事件的父级。If a stored procedure is started within a transaction, that transaction is the parent of the stored procedure event.
如果事务由存储过程启动,则存储过程是事务事件的父级。If a transaction is started by a stored procedure, the stored procedure is the parent of the transaction event.
存储函数Stored Functions
存储函数被限制不能导致显式或隐式的提交或回滚。存储函数事件可以位于父事务事件中。Stored functions are restricted from causing an explicit or implicit commit or rollback. Stored function events can reside within a parent transaction event.
触发器Triggers
触发器在访问其关联的表的语句中被激活,因此触发器事件的父对象始终是激活它的语句。Triggers activate as part of a statement that accesses the table with which it is associated, so the parent of a trigger event is always the statement that activates it.
触发器不能执行导致显式或隐式提交或回滚事务的语句。Triggers cannot issue statements that cause an explicit or implicit commit or rollback of a transaction.
计划事件Scheduled Events
计划事件主体中语句的执行发生在一个新的连接中。计划事件嵌套在父事务中不适用。The execution of the statements in the body of a scheduled event takes place in a new connection. Nesting of a scheduled event within a parent transaction is not applicable.
事务和保存点Transactions and Savepoints
保存点语句作为单独的语句事件记录。事务事件包括在事务期间发出的SAVEPOINT、ROLLBACK TO
SAVEPOINT和RELEASE
SAVEPOINT语句的单独计数器。Savepoint statements are recorded as separate statement events.
Transaction events include separate counters for
SAVEPOINT,
ROLLBACK TO
SAVEPOINT, and
RELEASE
SAVEPOINT statements issued during the transaction.
事务和错误Transactions and Errors
在事务中发生的错误和警告会记录在语句事件中,但不会记录在相应的事务事件中。这包括事务特定的错误和警告,例如对非事务性表的回滚或 GTID 一致性错误。Errors and warnings that occur within a transaction are recorded in statement events, but not in the corresponding transaction event. This includes transaction-specific errors and warnings, such as a rollback on a nontransactional table or GTID consistency errors.