我有两个非常大的多选查询。所有选择都已加入(左、内和右连接)。例子:
Declare @p2 Table (
...
)
Insert into @p2
Select * from D
join E on D.id = E.id
left join F on E.id2 = F.id
....
Select * from(
(Select * from A
join B on A.id = B.id
join C on B.id2 = C.id
....
) as PART1
right join
@p2 as PART2
on PART1.ID = PART2.ID)
这段代码运行良好,需要大约 20 秒才能执行。我运行执行计划,它显示 57% 的时间用于插入。为了优化这个查询,我创建了一个查询,例如:
Select * from(
(Select * from A
join B on A.id = B.id
join C on B.id2 = C.id
....
) as PART1
right join
(Select * from D
join E on D.id = E.id
left join F on E.id2 = F.id
....) as PART2
on PART1.ID = PART2.ID)
我预计这会显着提高速度,但结果却慢了 4 倍。你知道为什么吗 ?