gpt4 book ai didi

sql - 将 READ UNCOMMITTED 与 UNION ALL 一起使用

转载 作者:行者123 更新时间:2023-12-04 22:08:06 26 4
gpt4 key购买 nike

我有一个正在开发的存储过程,有多个 UNION ALL声明。这是历史数据,我被指示使用 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED .搁置关于这是否是正确方法的讨论,我想知道我是否需要在存储过程的顶部只指定一次 ISOLATION LEVEL,或者是否需要在每个 UNION ALL 之后指定它,因为它们'是不同的查询吗?

例子:

Alter procedure dbo.ExampleProcedure as
declare @StartDate datetime
declare @EndDate datetime
insert into myDB.DBO.InboundCalls

select I.Date, I.System, Count(*) as calls
from
(select Date, System, CallID from System1CallData C
Left Join someothertables as S on C.CallID = S.CallID
where (C.date >= @StartDate and C.date < @EndDate)) as I
Group by I.Date, I.System

Union ALL

select I.Date, I.System, Count(*) as calls
from
(select Date, System, CallID from System2CallData C
Left Join someothertables as S on C.CallID = S.CallID
where (C.date >= @StartDate and C.date < @EndDate)) as I
group by I.Date, I.System

Union ALL

select I.Date, I.System, Count(*) as calls
from
(select Date, System, CallID from System3CallData C
Left Join someothertables as S on C.CallID = S.CallID
where (C.date >= @StartDate and C.date < @EndDate)) as I
Group by I.Date, I.System
Order by I.Date asc, I.System asc, calls asc

我也放 SET TRANSACTION ISOLATION LEVEL之后 Alter Procedure dbo.ExampleProcedure as ,或在第一个 SELECT 之前, 或在每个嵌套 SELECT 之前?在此先感谢您的指导!

最佳答案

I'd like to know if I need to specify this ISOLATION LEVEL only once at the top of the stored procedure . . .



仅在程序顶部一次,当然除非您在程序内切换隔离级别。 isolation level reverts当 SP 退出时回到前一级别。

If you issue SET TRANSACTION ISOLATION LEVEL in a stored procedure or trigger, when the object returns control the isolation level is reset to the level in effect when the object was invoked. For example, if you set REPEATABLE READ in a batch, and the batch then calls a stored procedure that sets the isolation level to SERIALIZABLE, the isolation level setting reverts to REPEATABLE READ when the stored procedure returns control to the batch.



使用“读未提交”隔离级别对历史数据可能没有风险。我假设指示您使用该隔离级别的人知道风险并确定它是安全的。

历史数据通常要么根本不改变,要么以已知的时间间隔改变。 (比如说,每季度一次。或者每天凌晨 1:00。)我预计相对较少的人拥有对这些表的插入权限,几乎没有人拥有更新和删除权限。

您还可以测试在单个事务中运行三个单独的 insert 语句,而不是插入三个 select 语句的并集。 ORDER BY 子句在生产中可能是一个坏主意。

关于sql - 将 READ UNCOMMITTED 与 UNION ALL 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723509/

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