9000多条数据要执行12秒,请大家帮忙看一下sql有问题没

我的sql是这样的
select w1.*,
w2.GID,w2.Name,w2.Model,w2.Unit,w2.MatlGID,w2.MatlName,w2.MatlModel,w2.QtyMatl,w2.CreateDate,
w3.Name as SName
from
(
select a.DeptName,a.SourceBillNo,a.EquipmentNumber,a.SID,sum(Number) as QtyPrtOut,
sum(case when a.GState='D20150312004' then a.Number else 0 end) as QtyQuad,
sum(case when a.GState='D20150312005' then a.Number else 0 end) as QtyNoQuad
from (
select C.Name as DeptName, a.* from Product_MadeTeInStockDetail a
inner join Product_MadeTeInStock b on a.PTIS=b.PTIS
inner join Hr_Dept c on b.DeptID=c.DeptID
where c.PrtSort='数控车床部'
) a group by a.DeptName,a.SourceBillNo,a.EquipmentNumber,a.SID
) w1 left join
(
select a.MOID,a.GID,a.CreateDate,
b.GID as MatlGID,b.QtyMatl,
c.Name as MatlName,c.Model as MatlModel,
d.Name,d.Model,d.Unit
from Product_MadeOrder a left join
(
select SourceBillNo,GID,sum(Number) as QtyMatl from Product_MadeSendMaterialDetail
group by SourceBillNo,GID
) b on a.MOID=b.SourceBillNo
left join Storage_Goods c on b.GID=c.GID
left join Storage_Goods d on a.GID=d.GID
)
w2 on w1.SourceBillNo=w2.MOID
left join Hr_Staff w3 on w1.SID=w3.SID


w1结果集和w2结果集直接执行,都不需要1秒,但两个结果集联合起来查询就要12秒了

实际执行计划如下:

请大家帮忙看看,有没有什么方法可以优化下。
...全文
99 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉普赛的歌 2018-11-02
  • 打赏
  • 举报
回复
引用 3 楼 wggfcusmq 的回复:
[quote=引用 1 楼 yenange 的回复:] 人家都提示了你加索引, 你先加上索引再看需要多少秒吧:
create index ix_Product_MadeSendMaterialDetail_SourceBillNo on dbo.Product_MadeSendMaterialDetail ( SourceBillNo )
非常感谢,不太会看这个执行计划,加上索引后就1秒了,效果杠杠的[/quote] 那就好, 没事就结贴吧。
  • 打赏
  • 举报
回复
引用 1 楼 yenange 的回复:
人家都提示了你加索引, 你先加上索引再看需要多少秒吧:
create index ix_Product_MadeSendMaterialDetail_SourceBillNo on dbo.Product_MadeSendMaterialDetail ( SourceBillNo )
非常感谢,不太会看这个执行计划,加上索引后就1秒了,效果杠杠的
二月十六 2018-11-02
  • 打赏
  • 举报
回复
1、加索引。2、能用inner join就别用left join。3、实在不好弄,就从表结构设计方面想想办法。
吉普赛的歌 2018-11-02
  • 打赏
  • 举报
回复
人家都提示了你加索引, 你先加上索引再看需要多少秒吧:
create index ix_Product_MadeSendMaterialDetail_SourceBillNo on dbo.Product_MadeSendMaterialDetail ( SourceBillNo )

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧