MySQL Glossary-read

99 阅读6分钟

consistent read(一致性读/快照读)

我的翻译:

使用快照信息来展现基于某时刻的查询结果的读操作,不管同时正在运行的其他事务引起的变化。 如果查询数据已经被其他事务改变了, 原始数据会基于undo log 的内容重建。 这个技术避免了一些锁的问题,锁因为强制事务等待另一个事务结束会减少并发性。

可重复读隔离级别下, 快照是基于第一个读操作执行产生的。读已提交隔离级别下,快照会重置为每次一致性读操作的时间。

一致性读是InnoDB 在读已提交和可重复读隔离级别下执行SELECT语句的默认模式。 因为一致性读不会给它访问的表设置任何锁,当一个一致性读被执行在1个表上时候其他会话可自由地修改这些表。

有道翻译:

一种读操作,它使用快照信息来显示基于某个时间点的查询结果,而不管同时运行的其他事务执行了什么更改。如果查询的数据已被另一个事务更改,则根据撤消日志的内容重构原始数据。该技术通过强制事务等待其他事务完成,避免了一些可能降低并发性的锁定问题。

对于REPEATABLE READ隔离级别,快照基于执行第一次读操作的时间。使用READ COMMITTED隔离级别,快照将重置为每次一致读操作的时间。

一致读是InnoDB在read COMMITTED和REPEATABLE read隔离级别上处理SELECT语句的默认模式。因为一致读不会在它所访问的表上设置任何锁,所以在对表执行一致读时,其他会话可以自由修改这些表。

A read operation that uses snapshot information to present(展现) query results based on a point in time, regardless of changes performed by other transactions running at the same time. If queried data has been changed by another transaction, the original data is reconstructed based on the contents of the undo log. This technique avoids some of the locking issues that can reduce concurrency by forcing transactions to wait for other transactions to finish.

With REPEATABLE READ isolation level, the snapshot is based on the time when the first read operation is performed. With READ COMMITTED isolation level, the snapshot is reset to the time of each consistent read operation.

Consistent read is the default mode in which InnoDB processes SELECT statements in READ COMMITTED and REPEATABLE READ isolation levels. Because a consistent read does not set any locks on the table it accesses, other sessions are free to modify those tables while a consistent read is being performed on the table.

For technical details about the applicable isolation levels, see Section 14.7.2.3, “Consistent Nonlocking Reads”.

See Also concurrency, isolation level, locking, READ COMMITTED, REPEATABLE READ, snapshot, transaction, undo log.

dirty read(脏读)

脏读

  • 脏读是指检索出被其他事务更新但还没提交的数据。简单来说就是读到其他事务未提交的数据。

  • 不遵守 ACID 原则

  • 确实risky, 比如我100元这边要被扣1块,另一个事务也要扣1元但是一会儿收到红包又会返回0.5元!最终结果应该是98.5,但在我这边扣直接查到99元然后扣1元赋值给98,最终加0.5是99去加所以是99.5了。

    • 因为数据可以被回滚
    • 或者在提交前再度被更新
  • 反义词就是一致性读。不仅读不到其他事务未提交更新的数据,而且也读不到同一时刻其他事务提交了的数据。

An operation that retrieves(检索) unreliable data, data that was updated by another transaction but not yet committed. It is only possible with the isolation level known as read uncommitted.

This kind of operation does not adhere(遵守) to the ACID principle of database design. It is considered very risky, because the data could be rolled back, or updated further before being committed; then, the transaction doing the dirty read would be using data that was never confirmed as accurate.

Its opposite is consistent read, where InnoDB ensures that a transaction does not read information updated by another transaction, even if the other transaction commits in the meantime(其间).

See Also ACID, commit, consistent read, isolation level, READ UNCOMMITTED, rollback.

non-repeatable read(不可重复读)

我的翻译:

当1个查询检索数据,之后相同事务内的1个查询检索数据。本来应该是相同数据,但这2个查询返回了不同的结果(被同时的另一个事务的提交改变了)。

这类操作违反了数据库设计的ACID原则。 在1个事务内, 数据应当是一致的,带有可预测和稳定的关联。

在不同的隔离级别下, 不可重复度被 串行化读和可重复级别所避免,并且被一致性读和读未提交的级别允许。

有道:

查询检索数据,同一事务中的后续查询检索本该相同的数据,但查询返回不同的结果(由同时提交的另一个事务更改)的情况。

这种操作违背了数据库设计的ACID原则。在事务中,数据应该是一致的,具有可预测和稳定的关系。

在不同的隔离级别中,可序列化读和可重复读级别禁止不可重复读,一致读和读未提交级别允许不可重复读。

The situation when a query retrieves data, and a later query within the same transaction retrieves what should be the same data, but the queries return different results(changed by another transaction committing in the meantime).

This kind of operation goes against the ACID principle of database design. Within a transaction, data should be consistent, with predictable and stable relationships.

Among different isolation levels, non-repeatable reads are prevented by the serializable read and repeatable read levels, and allowed by the consistent read, and read uncommitted levels.

See Also ACID, consistent read, isolation level, READ UNCOMMITTED, REPEATABLE READ, SERIALIZABLE, transaction.

phantom

我的翻译:

1次查询结果集出现的1行,但不在更早查询的结果集中。举个例子,如果1个查询在1个事务中运行了2次, 与此同时,另一个事务在插入或更新新的1行后提交了以至于这行会匹配where查询。

这个现象被认知为幻读。 这比不可重复读更难防范,因为锁住第一次查询结果集的所有行并没有预防了会导致幻读出现的变化。

在不同的隔离级别下,幻读被可序列化读禁止,被可重复读、一致性读还有读未提交级别所允许。

有道:

出现在查询的结果集中,但不在较早查询的结果集中的行。例如,如果一个查询在一个事务中运行两次,同时,另一个事务在插入新行或更新行后提交,以便与查询的WHERE子句匹配。

这种情况称为幻像读取。比不可重复读更难防范,因为锁定来自第一个查询结果集的所有行并不能防止导致幻影出现的更改。

在不同的隔离级别中,可序列化读级别防止幻像读,可重复读、一致读和读未提交级别允许幻像读。

A row that appears in the result set of a query, but not in the result set of an earlier query. For example, if a query is run twice within a transaction, and in the meantime, another transaction commits after inserting a new row or updating a row so that it matches the WHERE clause of the query.

This occurrence is known as a phantom read. It is harder to guard against than a non-repeatable read, because locking all the rows from the first query result set does not prevent the changes that cause the phantom to appear.

Among different isolation levels, phantom reads are prevented by the serializable read level, and allowed by the repeatable read, consistent read, and read uncommitted levels.

See Also consistent read, isolation level, non-repeatable read, READ UNCOMMITTED, REPEATABLE READ, SERIALIZABLE, transaction.