关于SQL效率问题,高手来帮忙
要写一个察看部门事务的SQL语句:
1、解决部门等于该用户的部门,且该用户是部门经理或有权限察看部门事务的人(or)
2、发起部门等于该用户的部门,且该用户是部门经理或有权限察看部门事务的人(or)
3、发起人 解决人不等于该用户(等于的属于个人事务)(and)
4、事务处理明细中的解决部门是该用户的部门(or)
5、事务处理明细中的解决人是该用户的(or)
具体的语句如下,现在的问题是效率太低,造成超时(30秒)
select @conditionText = ' WHERE 1=1 ' + @Condition
+' AND (( ( '
+'((a.resolve_department ='+''''+@dempartment+''''+' or a.origin_department='+''''+@dempartment+'''' +') and '+''''+@EmployeeID+''''+ ' in ( select managerid from Wygl_Office_Department where departmentid= '+''''+@dempartment+''''+'))'
+'or ((a.resolve_department ='+''''+@dempartment +''''+'or a.origin_department='+''''+@dempartment+''''+') and ( '+''''+@EmployeeID+''''+' IN ( select EmployeeID from Wygl_System_UserAuthorization where Especially LIKE ''%M%'' )))'
+' ) '
+ ' AND a.origin_employee <> '+''''+@EmployeeID+''''
+ ' AND a.resolve_employee <> '+''''+@EmployeeID+''''+')'
+ ' OR ( A.request_id in (select request_id from wygl_manager_progressdetail where origin_department = '+''''+@dempartment+''''+' ) )'
+ ' OR ( A.request_id in (select request_id from wygl_manager_progressdetail where origin_Employee = '+''''+@EmployeeID+''''+' ) ) )'
+' ORDER BY A.request_id DESC '