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万左右
http://blog.csdn.net/ningyuanhuo/article/details/42291303
能不用or的话,尽量不用or吧。将他们拆开成多个然后unin呢?会不会有效果?
关键是rallowdid LIKE '%,2,%'
这样的 LIKE 条件是无法利用索引的,只能全表扫描,怎么可能不慢。
看条件又是一个不符合第一范式(原子性)的字段——鄙视——要拆分成子表。
学校老师/n天学会XXX之类的速成教材,用的是不到百条的数据、史前的设计思想;这就是其中一个典型的没卵用的设计模板。