InnoDB
can avoid the overhead associated with setting up the transaction ID (TRX_ID
field) for transactions that are known to be read-only. InnoDB
可以避免为已知为只读的事务设置事务ID(TRX_ID
字段)的开销。A transaction ID is only needed for a transaction that might perform write operations or locking reads such as 事务ID仅适用于可能执行写入操作或锁定读取的事务,如SELECT ... FOR UPDATE
. SELECT ... FOR UPDATE
。Eliminating unnecessary transaction IDs reduces the size of internal data structures that are consulted each time a query or data change statement constructs a read view.消除不必要的事务ID可以减少每次查询或数据更改语句构造读取视图时所参考的内部数据结构的大小。
InnoDB
detects read-only transactions when:InnoDB
在以下情况下检测只读事务:
The transaction is started with the 事务由START TRANSACTION READ ONLY
statement. START TRANSACTION READ ONLY
语句启动。In this case, attempting to make changes to the database (for 在这种情况下,尝试更改数据库(对于InnoDB
, MyISAM
, or other types of tables) causes an error, and the transaction continues in read-only state:InnoDB
、MyISAM
或其他类型的表)会导致错误,事务将以只读状态继续:
ERROR 1792 (25006): Cannot execute statement in a READ ONLY transaction.
You can still make changes to session-specific temporary tables in a read-only transaction, or issue locking queries for them, because those changes and locks are not visible to any other transaction.您仍然可以在只读事务中更改特定于会话的临时表,或者为它们发出锁定查询,因为这些更改和锁定对任何其他事务都不可见。
The autocommit
setting is turned on, so that the transaction is guaranteed to be a single statement, and the single statement making up the transaction is a “non-locking” SELECT
statement. autocommit
设置处于启用状态,因此事务保证为单个语句,并且组成事务的单个语句是“非锁定”SELECT
语句。That is, a 也就是说,SELECT
that does not use a FOR UPDATE
or LOCK IN SHARED MODE
clause.SELECT
不使用FOR UPDATE
或LOCK IN SHARED MODE
子句。
The transaction is started without the 事务在没有READ ONLY
option, but no updates or statements that explicitly lock rows have been executed yet. READ ONLY
选项的情况下启动,但尚未执行显式锁定行的更新或语句。Until updates or explicit locks are required, a transaction stays in read-only mode.在需要更新或显式锁定之前,事务将保持只读模式。
Thus, for a read-intensive application such as a report generator, you can tune a sequence of 因此,对于报表生成器等读密集型应用程序,可以通过在InnoDB
queries by grouping them inside START TRANSACTION READ ONLY
and COMMIT
, or by turning on the autocommit
setting before running the SELECT
statements, or simply by avoiding any data change statements interspersed with the queries.START TRANSACTION READ ONLY
和COMMIT
中对InnoDB
查询进行分组,或者在运行SELECT
语句之前打开autocommit
设置,或者简单地避免查询中出现任何数据更改语句,来优化InnoDB
查询序列。
For information about 有关START TRANSACTION
and autocommit
, see Section 13.3.1, “START TRANSACTION, COMMIT, and ROLLBACK Statements”.START TRANSACTION
和autocommit
的信息,请参阅第13.3.1节,“START TRANSACTION、COMMIT和ROLLBACK语句”。
Transactions that qualify as auto-commit, non-locking, and read-only (AC-NL-RO) are kept out of certain internal 符合自动提交、非锁定和只读(AC-NL-RO)条件的事务不在某些InnoDB
data structures and are therefore not listed in SHOW ENGINE INNODB STATUS
output.InnoDB
内部数据结构中,因此不会列在SHOW ENGINE INNODB STATUS
输出中。