major 2017-03-30 10:11 采纳率: 0%
浏览 957
已结题

关于sql 查询速度优化的问题

select count(*) FROM [View_AgentPurchaseOrder] with(nolock)

where 1=1 and (
exists (select null from userrole where rDepartment in (2) and rUser=aoUid)
or exists (select null from userrole where rDepartment in (2) and rStaff=aoSalesMan)
or exists (select null from u_t346_data where exists (select rcode from t_flowrecord where rstate=-1 and ((rallowdid like '%,2,%') or rallowdid='' or rallowdid is null) and ((rallowpid like '%,24,%') or rallowpid='' or
rallowpid is null) and (rallowuser like '%,173,%' or rallowuser='' or rallowuser is null) and rtable=346 and tcode=rcode) and tCode='code_'+cast(View_AgentPurchaseOrder.id as nvarchar(50)))
or exists (select null from u_t346_data where exists (select rcode from t_flowrecord where rUid=173 and rtable=346 and tcode=rcode) and tCode='code_'+cast(View_AgentPurchaseOrder.id as nvarchar(50)))
)
and (aostate>-50 or aoState<-50)
and aocode like 'ap%' and aoTotalAmount>=0

这条语句查询速度很慢查询完成需要38秒,各位有没有什么优化意见
如果去掉
or exists (select null from u_t346_data where exists (select rcode from t_flowrecord where rstate=-1 and ((rallowdid like '%,2,%') or rallowdid='' or rallowdid is null) and ((rallowpid like '%,24,%') or rallowpid='' or
rallowpid is null) and (rallowuser like '%,173,%' or rallowuser='' or rallowuser is null) and rtable=346 and tcode=rcode) and tCode='code_'+cast(View_AgentPurchaseOrder.id as nvarchar(50)))
or exists (select null from u_t346_data where exists (select rcode from t_flowrecord where rUid=173 and rtable=346 and tcode=
rcode) and tCode='code_'+cast(View_AgentPurchaseOrder.id as nvarchar(50))) 这两段的话就查询就只需要1秒

View_AgentPurchaseOrder 这个表数量及在200万左右

  • 写回答

3条回答 默认 最新

  • devmiao 2017-03-30 15:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?