简体   繁体   English

SQL查询性能查询

[英]SQL Query Performance Query

I have a hierarchy like following Group Company Department Cost Centre Which are being stored in database like following table structure. 我有一个类似以下集团公司部门成本中心的层次结构,这些层次结构存储在数据库中,如下表结构所示。

Table Name : OrgHierarchy Columns : Id, ParentId, Level, Type 表名称: OrgHierarchy列: Id,ParentId,级别,类型

In My Employee View I have employee Cost Centre, Department, Company and Group present. 在“我的员工”视图中,我有员工成本中心,部门,公司集团

I need help in writing better performing query to find employee for a particular node in the hierarchy (it can be Group/Company/Department/Cost Centre) 在编写性能更好的查询以查找层次结构中某个特定节点的员工时,我需要帮助(可以是组/公司/部门/成本中心)

  Select * from vw_Employee  emp
    Where emp.CostCentreId = @NodeId 
    OR emp.DepartmentId=@NodeId 
    OR emp.CompanyId=@NodeId 
    OR emp.GroupId= @NodeId

Here the number of “OR” statement making the query very slow. 在这里,“ OR”语句的数量使查询非常慢。

Is there any better approach to handle the efficiently or any other approach? 有没有更好的方法可以有效地处理这种情况?

Using OR in condition makes indexis be skipped. 在条件中使用OR会使索引被跳过。 Replace it with UNION : 将其替换为UNION

Select * 
from vw_Employee  emp
Where emp.CostCentreId = @NodeId 
UNION
Select * 
from vw_Employee  emp
Where emp.DepartmentId=@NodeId 
UNION
Select * 
from vw_Employee  emp
Where emp.CompanyId=@NodeId 
UNION
Select * 
from vw_Employee  emp
Where emp.GroupId= @NodeId

Here is a good question. 是一个好问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM