我希望你们能帮助我。我有以下SQL查询,我认为它不是很重,但需要大约5分钟才能完成。如果您有其他方法可以完成此操作,请与我们联系:
update rep
set rep.StatusID= 2,
rep.Available= 1,
rep.User= @user
from dbo.MCP_Rep_Compensation_Plan rep
left join dbo.MCP_Compensation_Plan compensationplan on compensationplan.Compensation_Plan_ID = @compplan_id and compensationplan.Active = 1
left join dbo.MRRS_Cycle actualcycle on actualcycle.CycleID = compensationplan.CycleID and actualcycle.Active = 1
left join dbo.MRRS_Cycle lastcycle on lastcycle.Consecutive = actualcycle.Consecutive -1 and lastcycle.Active = 1
where rep.Active = 1 and rep.ID_Compensation_Plan = @compplan_id and exists(
select OrderID
from dbo.MCP_Orders
where Active = 1 and Order_cycle = lastcycle.CycleID and OrderRepID = rep.RepID
and Order_Status in(28,30))
答案 0 :(得分:2)
我确实看到一些地方可以重写您的查询,但我不确定在没有查看执行计划并确保您拥有适当的索引的情况下它会有多大帮助。
例如,确保在第一次加入补偿计划表时包含实际的加入条件。通过加入compensationplan.Compensation_Plan_ID
和rep.ID_Compensation_Plan
来完成此操作。
此外,由于您在相关的现有子查询中使用了某些表,因此我认为不需要OUTER JOINs
。
以下是更新的查询:
update rep
set rep.StatusID= 2,
rep.Available= 1,
rep.User= @user
from dbo.MCP_Rep_Compensation_Plan rep
join dbo.MCP_Compensation_Plan compensationplan on
compensationplan.Compensation_Plan_ID = rep.ID_Compensation_Plan and compensationplan.Active = 1
join dbo.MRRS_Cycle actualcycle on
actualcycle.CycleID = compensationplan.CycleID and actualcycle.Active = 1
join dbo.MRRS_Cycle lastcycle on
lastcycle.Consecutive = actualcycle.Consecutive -1 and lastcycle.Active = 1
where rep.Active = 1
and rep.ID_Compensation_Plan = @compplan_id
and exists(
select OrderID
from dbo.MCP_Orders
where Active = 1
and Order_cycle = lastcycle.CycleID
and OrderRepID = rep.RepID
and Order_Status in(28,30)
)