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

标签 sql tsql sql-server-2008-r2

我有一个正在开发的存储过程,有多个 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/

相关文章:

mysql - 我需要显示来自 BRAND_CAR 的所有汽车,即使其中没​​有汽车

sql-server - 如何在 Access 2010 中使用 TSQL 执行以下多层透视?

sql-server - ADO.NET 调用 T-SQL 存储过程导致 SqlTimeoutException

c# - 从 SQL Server 2008 R2 解密数据

SQL session /连接变量

sql - 子查询中不允许使用多级 GROUP BY 子句

sql - T-SQL 明年列中去年总计

sql - 从具有复合主键的表中选择过滤记录

SQL 收入最低的地点计数

sql-server - 从 varchar 列中选择 max int