視圖查詢語言優化

yhwdai 2015-12-17 01:44:23
現有一個視圖查詢超慢...本人小菜鳥...有沒高手幫我優化下...謝謝了...

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[apm_wip3]') and OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[apm_wip3]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE view apm_wip3     /*產生WIP資料*/
as SELECT apm_xbzbodxh.xb,
apm_xbzbodxh.zb,
apm_xbzbodxh.zb_mc,
apm_xbzbodxh.od,
apm_xbzbodxh.xh,
apm_xbzbodxh.cp_id,
apm_wip_adjust.sl,
apm_wip.rs1,
apm_wip1.cs,
apm_wip2.rs,
apm_wip01.ljrs,
apm_wip01.ljcl,
apm_wip02.ljccds,
apm_xhcs.s9,
apm_xhcs.s10,
apm_xhcs.s11,
apm_xhcs.S12,
ao_ord_v_pc1.s,
ao_ord2.ord_quan,
ao_ord2.ord_quan1,
ao_ord2.ord_check_delivery,
ao_v_cpjk1.sl as rksl,
lianchang.mosu,
lianchang.proname,
lianchang.printy,
lianchang.pinno,
lianchang.checkspec,
lianchang.distype,
lianchang.bolispec2,
lianchang.custname

FROM apm_xbzbodxh

left outer  join apm_wip  on (   apm_xbzbodxh.xb = apm_wip.xb and apm_xbzbodxh.zb = apm_wip.zb and apm_xbzbodxh.od = apm_wip.od and  apm_xbzbodxh.xh = apm_wip.xh and apm_xbzbodxh.cp_id =apm_wip.cp_id)
left outer  join apm_wip2  on (   apm_xbzbodxh.xb = apm_wip2.xb and apm_xbzbodxh.zb = apm_wip2.st2 and apm_xbzbodxh.od = apm_wip2.od and  apm_xbzbodxh.xh = apm_wip2.xh and apm_xbzbodxh.cp_id =apm_wip2.cp_id)
left outer  join apm_wip1  on (   apm_xbzbodxh.xb = apm_wip1.xb and apm_xbzbodxh.zb = apm_wip1.st1 and apm_xbzbodxh.od = apm_wip1.od and  apm_xbzbodxh.xh = apm_wip1.xh and apm_xbzbodxh.cp_id =apm_wip1.cp_id)
left outer  join lianchang on apm_xbzbodxh.xh=lianchang.orderno
left outer  join apm_wip_adjust  on (   apm_xbzbodxh.xb = apm_wip_adjust.xb and apm_xbzbodxh.zb = apm_wip_adjust.zb and apm_xbzbodxh.od = apm_wip_adjust.od and  apm_xbzbodxh.xh = apm_wip_adjust.xh and apm_xbzbodxh.cp_id =apm_wip_adjust.cp_id)
left outer  join apm_wip01  on (   apm_xbzbodxh.xb = apm_wip01.xb and apm_xbzbodxh.zb = apm_wip01.st1 and apm_xbzbodxh.od = apm_wip01.od and  apm_xbzbodxh.xh = apm_wip01.xh and apm_xbzbodxh.cp_id =apm_wip01.cp_id)
left outer  join apm_wip02  on (   apm_xbzbodxh.xb = apm_wip02.xb and apm_xbzbodxh.zb = apm_wip02.zb and apm_xbzbodxh.od = apm_wip02.od and  apm_xbzbodxh.xh = apm_wip02.xh and apm_xbzbodxh.cp_id =apm_wip02.cp_id)
left outer  join ao_ord_v_pc1  on (   apm_xbzbodxh.od = ao_ord_v_pc1.ord_id and  apm_xbzbodxh.xh = ao_ord_v_pc1.ord_p_lh and apm_xbzbodxh.cp_id =ao_ord_v_pc1.cp_id)
left outer  join ao_ord2  on (   apm_xbzbodxh.od = ao_ord2.ord_id and  apm_xbzbodxh.xh = ao_ord2.ord_p_lh and apm_xbzbodxh.cp_id =ao_ord2.cp_id)
left outer  join  ao_v_CPJK1 on  (apm_xbzbodxh.od = ao_v_CPJK1.rk_ord_id and  apm_xbzbodxh.xh = ao_v_CPJK1.rk_p_lh and apm_xbzbodxh.cp_id =ao_v_CPJK1.cp_id)
left outer  join  apm_xhcs on  ( apm_xbzbodxh.xh = apm_xhcs.xh)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
...全文
413 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
WorldMobile 2015-12-18
  • 打赏
  • 举报
回复
看你用了12个表,数据结构设计的不太合理,因为不知道你这些表上的主键及索引情况,直接这样的话没法优化,

建议你根据这些表上的条件,建议相应的索引,或者用sqlserver自带的查询优化分析器,看一下推荐的索引,创建上去,再看看

查询优化器可以参考这个

http://www.cnblogs.com/zhijianliutang/p/4175551.html
PB菜鸟 2015-12-18
  • 打赏
  • 举报
回复
引用 楼主 yhwdai 的回复:
現有一個視圖查詢超慢...本人小菜鳥...有沒高手幫我優化下...謝謝了... if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[apm_wip3]') and OBJECTPROPERTY(id, N'IsView') = 1) drop view [dbo].[apm_wip3] GO SET QUOTED_IDENTIFIER OFF  GO SET ANSI_NULLS ON  GO CREATE view apm_wip3     /*產生WIP資料*/ as SELECT apm_xbzbodxh.xb, apm_xbzbodxh.zb, apm_xbzbodxh.zb_mc, apm_xbzbodxh.od, apm_xbzbodxh.xh, apm_xbzbodxh.cp_id, apm_wip_adjust.sl, apm_wip.rs1, apm_wip1.cs, apm_wip2.rs, apm_wip01.ljrs, apm_wip01.ljcl, apm_wip02.ljccds, apm_xhcs.s9, apm_xhcs.s10, apm_xhcs.s11, apm_xhcs.S12, ao_ord_v_pc1.s, ao_ord2.ord_quan, ao_ord2.ord_quan1, ao_ord2.ord_check_delivery, ao_v_cpjk1.sl as rksl, lianchang.mosu, lianchang.proname, lianchang.printy, lianchang.pinno, lianchang.checkspec, lianchang.distype, lianchang.bolispec2, lianchang.custname FROM apm_xbzbodxh left outer  join apm_wip  on (   apm_xbzbodxh.xb = apm_wip.xb and apm_xbzbodxh.zb = apm_wip.zb and apm_xbzbodxh.od = apm_wip.od and  apm_xbzbodxh.xh = apm_wip.xh and apm_xbzbodxh.cp_id =apm_wip.cp_id) left outer  join apm_wip2  on (   apm_xbzbodxh.xb = apm_wip2.xb and apm_xbzbodxh.zb = apm_wip2.st2 and apm_xbzbodxh.od = apm_wip2.od and  apm_xbzbodxh.xh = apm_wip2.xh and apm_xbzbodxh.cp_id =apm_wip2.cp_id) left outer  join apm_wip1  on (   apm_xbzbodxh.xb = apm_wip1.xb and apm_xbzbodxh.zb = apm_wip1.st1 and apm_xbzbodxh.od = apm_wip1.od and  apm_xbzbodxh.xh = apm_wip1.xh and apm_xbzbodxh.cp_id =apm_wip1.cp_id) left outer  join lianchang on apm_xbzbodxh.xh=lianchang.orderno left outer  join apm_wip_adjust  on (   apm_xbzbodxh.xb = apm_wip_adjust.xb and apm_xbzbodxh.zb = apm_wip_adjust.zb and apm_xbzbodxh.od = apm_wip_adjust.od and  apm_xbzbodxh.xh = apm_wip_adjust.xh and apm_xbzbodxh.cp_id =apm_wip_adjust.cp_id) left outer  join apm_wip01  on (   apm_xbzbodxh.xb = apm_wip01.xb and apm_xbzbodxh.zb = apm_wip01.st1 and apm_xbzbodxh.od = apm_wip01.od and  apm_xbzbodxh.xh = apm_wip01.xh and apm_xbzbodxh.cp_id =apm_wip01.cp_id) left outer  join apm_wip02  on (   apm_xbzbodxh.xb = apm_wip02.xb and apm_xbzbodxh.zb = apm_wip02.zb and apm_xbzbodxh.od = apm_wip02.od and  apm_xbzbodxh.xh = apm_wip02.xh and apm_xbzbodxh.cp_id =apm_wip02.cp_id) left outer  join ao_ord_v_pc1  on (   apm_xbzbodxh.od = ao_ord_v_pc1.ord_id and  apm_xbzbodxh.xh = ao_ord_v_pc1.ord_p_lh and apm_xbzbodxh.cp_id =ao_ord_v_pc1.cp_id) left outer  join ao_ord2  on (   apm_xbzbodxh.od = ao_ord2.ord_id and  apm_xbzbodxh.xh = ao_ord2.ord_p_lh and apm_xbzbodxh.cp_id =ao_ord2.cp_id) left outer  join  ao_v_CPJK1 on  (apm_xbzbodxh.od = ao_v_CPJK1.rk_ord_id and  apm_xbzbodxh.xh = ao_v_CPJK1.rk_p_lh and apm_xbzbodxh.cp_id =ao_v_CPJK1.cp_id) left outer  join  apm_xhcs on  ( apm_xbzbodxh.xh = apm_xhcs.xh) GO SET QUOTED_IDENTIFIER OFF  GO SET ANSI_NULLS ON  GO
这么多表关联 为什么都用left join 难道把所有的数据都检索出来,把没必要left join 的换成inner join
jlwei888 2015-12-17
  • 打赏
  • 举报
回复
看着就晕,谁知道你需求和原来各表结构是什么,其实能实现功能就OK

740

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 脚本语言
社区管理员
  • 脚本语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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