SQL Server中Read Uncommitted与Read Committed隔离级执行逻辑问询
事务隔离级别与并发执行逻辑分析
Employees表初始数据:
| id | name | salary |
|---|---|---|
| 1 | Stanley | 500 |
| 2 | Jan | 600 |
| 3 | Zaid | 700 |
事务定义
T1(隔离级别:Read Uncommitted)
Begin Transaction S1: update Employees set salary = 2*salary where name = 'Zaid' S2: update Employees set salary = 3*salary where name = 'Zaid' Commit;
T2(隔离级别:Read Committed)
Begin Transaction S3: update Employees set salary = salary - 20 where name = 'Zaid' S4: update Employees set salary = salary - 10 where name = 'Zaid' Commit;
初步执行时间线假设(@value代表Zaid初始薪资700)
| Time | Transaction 1 | Transaction 2 | Salary(当前时间的薪资值) |
|---|---|---|---|
| 0 | Begin Transaction | @value | |
| 1 | S1 | Begin Transaction | 2 * @value |
| 2 | S3 | @value - 20 | |
| 3 | S2 | 3*(2*@value -20) | |
| 4 | S4 | (@value - 20) - 10 | |
| 5 | Commit; | 3*(2*@value -20)【此处存疑】 | |
| 6 | Commit; | (@value - 20) - 10 ? |
核心疑问
- 已知Read Uncommitted允许T1读取T2未提交的变更,Read Committed仅允许T2读取其他事务已提交的变更,但二者并发交互时的具体执行逻辑是什么?上述时间线是否准确?
- 若T2先启动,T1会等待T2提交,还是忽略T2的排他锁并设置自己的锁?是否会按如下时间线执行?
READ UNCOMMITTED是限制最低的隔离级别,因为它会忽略其他事务设置的锁。该级别下的事务可以读取其他事务未提交的修改数据,即“脏读”。
另一种时间线假设
| Time | Transaction 1 | Transaction 2 |
|---|---|---|
| 0 | Begin Transaction | |
| 1 | Begin Transaction | S3 |
| 2 | S1 | |
| 3 | S2 | |
| 4 | Commit; | |
| 5 | S4 | |
| 6 | Commit; |
内容的提问来源于stack exchange,提问作者BrainlessPOMO
相关产品推荐
相关产品推荐

