在已提交读
隔离级别中,同一事务中的 2 个后续选择查询可能会产生不同的结果,因为这 2 个查询之间可能存在并发更新:
transaction 1: select id from table;
=> returns [1, 2, 3]
transaction 2: delete from table where id = 2;
transaction 1: select id from table;
=> returns [1, 3]
如果事务 1 中的选择查询合并在 CTE 中会发生什么?假设我有以下虚拟查询:
with
cte_1 as (select id from table),
cte_2 as (select id from table)
select (select count(*) from cte_1, select count(*) from cte_2)
如果 cte_1
和 cte_2
的执行之间发生并发更新,我们现在是否也可能得到不同的结果?
最佳答案
每条语句都以原子方式执行,并且在整个运行时都会看到一致的数据库 View 。 “两个 CTE”是一个单个查询,因此该查询在运行时也不会看到任何(已提交的)更改
CTE 查询相当于:
select (select count(*) from (select id from table) as cte1),
(select count(*) from (select id from table) as cte2)
不相关,但是:您可能不知道 select (a,b)
在 Postgres 中与 select a,b
不同。第一个返回具有匿名记录类型(有两个字段)的单个列,第二个返回两列。
关于sql - READ COMMITTED 隔离级别下的 CTE 子句是否可能有不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041379/