gpt4 book ai didi

sql - 关于数据库事务中不同隔离级别的一些说明?

转载 作者:行者123 更新时间:2023-12-03 21:53:02 25 4
gpt4 key购买 nike

以下是来自 Wikipedia's Isolation article 的声明关于 REPEATABLE READS

In this isolation level, a lock-based concurrency control DBMS implementation keeps read and write locks (acquired on selected data) until the end of the transaction. However, range-locks are not managed, so the phantom reads phenomenon can occur (see below).



我的问题是交易分别何时开始和结束。

如果我们以在同一链接上具有 REPEATABLE READS 隔离级别的不可重复读取为例,按照我的理解 trnsaction 1 开始
当第一个查询被触发时,即 SELECT * FROM users WHERE id = 1. DBMS 将锁定用户表,直到并且除非事务结束。
这里 最后 我的意思是当连接被回滚或提交而不是在 SELECT * FROM users WHERE id = 1 完成时.到那个时候
事务 2 会等待对吗?

问题 2 :- 现在,如果我们考虑下面给出的隔离级别和他们的行为(在同一链接中)

Isolation level     Dirty reads   Non-repeatable   Phantoms
Read Uncommitted may occur may occur may occur
Read Committed - may occur may occur
Repeatable Read - may occur -
Serializable - - -

根据我的理解,最可靠的是可序列化,然后是可重复读取,然后是读取提交,但我仍然看到使用读取提交的应用程序。是不是因为
与 Read Committed 相比,Serializable 和 Repeatable Read 的性能较差,因为在可序列化中它将是顺序的,以防万一
事务必须等待另一个事务释放锁。 对? 所以为了充分利用这三者,我们可以使用隔离
级别为 Read Committed with SELECT FOR UPDATE (实现可重复读取)。 如果需要,不确定如何实现幻读,以防读取提交
隔离级别?

最佳答案

Oracle 不支持 REPEATABLE READ隔离级别。但是,SQL Server 确实 - 并且它确实在事务选择的所有行上放置了锁,直到它结束(即:它被提交或回滚)。所以你是对的,这确实会让其他事务等待(如果他们正在更新锁定的数据)并且可能不利于并发。
对于问题 2:是的,隔离级别越高,并发事务的性能就越差,因为它们必须等待更多的锁被释放。我不确定使用 SELECT FOR UPDATE 所说的“三者兼得”是什么意思。因为 SELECT FOR UPDATE将在所有选定的行上放置行锁。
最后,这是 Oracle 手册中关于幻读的引用:

[phantom reads occur when] a transaction reruns a query returning a set of rows that satisfies a search condition and finds that another committed transaction has inserted additional rows that satisfy the condition.

For example, a transaction queries the number of employees. Five minutes later it performs the same query, but now the number has increased by one because another user inserted a record for a new hire. More data satisfies the query criteria than before, but unlike in a fuzzy read the previously read data is unchanged.



引用:
  • Data Concurrency and Consistency (甲骨文)
  • SET TRANSACTION LEVEL (SQL Server)
  • 关于sql - 关于数据库事务中不同隔离级别的一些说明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7421314/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com