如何防止自连接数据不一致?

How to keep self-joing from data inconsistence?

在RepeatableRead中,即使记录被多次查询,我们也可以保证从同一条记录中检索到相同的值。但是,在定义视图时不允许设置隔离级别。目前,我正在尝试创建一个视图和 SQL 语句,如下所示 SELECT * FROM TBL t1 INNER JOIN TBL t2 ON t1.SEQ = t2.SEQ + 1.

如您所见,我在一个会话中查询了 table 两次。这个查询可以很快,但我不能保证这两个查询之间 table 没有任何变化。这种自连接如何保持数据的一致性?

Oracle 会自动执行此操作。

来自documentation

Statement-Level Read Consistency

Oracle Database always enforces statement-level read consistency, which guarantees that data returned by a single query is committed and consistent for a single point in time.

The point in time to which a single SQL statement is consistent depends on the transaction isolation level and the nature of the query:

  • In the read committed isolation level, this point is the time at which the statement was opened. For example, if a SELECT statement opens at SCN 1000, then this statement is consistent to SCN 1000.
  • In a serializable or read-only transaction, this point is the time the transaction began. For example, if a transaction begins at SCN 1000, and if multiple SELECT statements occur in this transaction, then each statement is consistent to SCN 1000.
  • In a Flashback Query operation (SELECT ... AS OF), the SELECT statement explicitly specifies the point in time. For example, you can query a table as it appeared last Thursday at 2 p.m.