简体   繁体   English

SQL中的事务

[英]transactions in SQL

I have two transactions that are the following: 我有以下两项交易:

T1: T1:

Begin Transaction;
S1: Insert Into Salary Values (103, 60000);
S2: Update Salary Set salary = salary + 10000 Where id = 101;
Commit;

T2: T2:

Begin Transaction;
S3: Select Avg(salary) As a1 From Salary;
S4: Select Avg(salary) As a2 From Salary; 

Assume that the individual statements S1, S2, S3, and S4 always execute atomically. 假设各个语句S1,S2,S3和S4始终自动执行。 Suppose initially there are two tuples in Salary: (101,30000) and (102,50000). 假设工资中最初有两个元组:(101,30000)和(102,50000)。 Each transaction runs once and commits. 每个事务运行一次并提交。 Transaction T1 always executes with isolation level Serializable (the highest level). 事务T1始终以隔离级别Serializable(最高级别)执行。

a) If transaction T2 executes with isolation level Serializable, what possible pairs of valuesa1 and a2 are returned by T2? a)如果事务T2以隔离级别Serializable执行,则T2返回哪些可能的值对a1和a2?

b) If transaction T2 executes with isolation level Read-Committed, what possible pairs of values a1 and a2 are returned by T2? b)如果事务T2在隔离级别为Read-Committed的情况下执行,则T2返回哪些可能的值对a1和a2?

c) If transaction T2 executes with isolation level Read-Uncommitted what possible pairs of values a1 and a2 are returned by T2? c)如果事务T2以隔离级别“未提交”执行,则T2返回哪些可能的值对a1和a2?

Here are my answers: 这是我的答案:

a) S1 – S2 – S3 – S4 Value: a1: 50000 , a2: 50000 a) S1 – S2 – S3 – S4值:a1:50000,a2:50000

S3 – S4 – S1 – S2 Value: a1: 40000, a2: 40000 S3 – S4 – S1 – S2值:a1:40000,a2:40000

But I'm a bit confused on how read-committed and how read-uncommitted work. 但是我对读取提交和读取未提交的工作方式感到困惑。 Any help with B and C, would be a great help. 对B和C的任何帮助,将是一个很大的帮助。

  • read-committed = show only what was already committed (or SAVED IN DB) read-committed =仅显示已提交的内容(或SAVED IN DB)
  • read-uncommitted = read what is currently in the database (regardless of commit). read-uncommitted =读取数据库中当前的内容(与提交无关)。

given a long transaction, before you issue a "commit" command, all the inserts/updates/deletes cannot be viewed unless you do a "read-uncommitted". 给定一个长事务,在发出“提交”命令之前,除非执行“未提交读”,否则无法查看所有插入/更新/删除。 under the hood, the data you insert/update are locked for normal viewing. 在幕后,您插入/更新的数据将被锁定以进行常规查看。 sometimes even the entire table is locked - you cannot view any record at all unless you do a read uncommitted. 有时甚至整个表都被锁定-除非您未提交读操作,否则根本无法查看任何记录。

for fast operations (eg less than 1 sec) you would not really feel the difference between the two but to test the difference, you should open a different connection for the T2 and run T1 without the "commit" command, then you will feel the "lock" on read-committed. 对于快速操作(例如,少于1秒),您不会真正感觉到两者之间的差异,但要测试差异,您应该为T2打开一个不同的连接并在不使用“ commit”命令的情况下运行T1,那么您会感觉到“锁定”为已提交读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM