我正在尝试建立一个像求职门户网站那样的搜索引擎。我的查询与资格特别相关。我的最终用户可以查询具有单个或一组资格-学科组合的员工,或者他们可以提供具有学科的资格列表并询问所有完成任何给定资格-学科组合的员工。我的脚本如下。
declare @Qualification table(QualId int identity primary key, Qualification varchar(100))
declare @Discipline table(DispId int identity primary key, Discipline varchar(100))
declare @Emp table(Eid int identity primary key, EName varchar(100))
declare @EmpQualification table(Eid int, QualId int, DispId int)
declare @ReqData table(QualId int, DispId varchar(100))
declare @AllMandatory char(1)
insert into @Qualification
select 'Diploma' union
select 'B.E' union
Select 'MBA' union
select 'MCA' union
Select 'B.Sc' union
Select 'B.Com'
insert into @Discipline
select 'HR' union
select 'IT' union
Select 'Computers' union
select 'Agriculture' union
Select 'Civil'
insert into @Emp
select 'SANTHOSHANAND M' union
select 'VENKATA VEERA JAGADEESH MANNE' union
select 'E.SURESH KUMAR' union
select 'E HARIHARA PUTHIRAN' union
select 'SOUMYAJIT MOHANTY' union
select 'TANMOY DAS' union
select 'PANAKANTI SANTHOSH' union
select 'G NAVEEN KUMAR' union
select 'VIJAY LOGANATHAN' union
select 'SAI KIRAN GANDHE'
INSERT INTO @EmpQualification
SELECT 1,1,3 UNION
SELECT 1,5,4 UNION
SELECT 2,2,2 UNION
SELECT 2,6,3 UNION
SELECT 3,3,3 UNION
SELECT 3,2,3 UNION
SELECT 4,4,2 UNION
SELECT 4,2,3 UNION
SELECT 5,5,4 UNION
SELECT 5,3,1 UNION
SELECT 6,6,3 UNION
SELECT 6,4,3 UNION
SELECT 7,1,3 UNION
SELECT 7,5,5 UNION
SELECT 8,2,3 UNION
SELECT 8,6,3 UNION
SELECT 9,3,1 UNION
SELECT 9,1,3 UNION
SELECT 10,4,5 UNION
SELECT 10,2,5
SELECT e.Eid, E.EName Name, ql.Qualification, d.Discipline FROM @Emp E
INNER JOIN @EmpQualification Q ON E.Eid = Q.Eid
INNER JOIN @Qualification QL ON Q.QualId = QL.QualId
INNER JOIN @Discipline D ON Q.DispId = D.DispId
insert into @ReqData
select 1,'Any' union
select 5,4
set @AllMandatory = 'N'
if @AllMandatory = 'N'
select * from @Emp where Eid in (
select distinct q.Eid from @EmpQualification q
inner join (select distinct QualId, convert(int,DispId) DispId from @ReqData where DispId <> 'Any') f on q.QualId = f.QualId and q.DispId = f.DispId
union
select distinct q.Eid from @EmpQualification q
inner join (select distinct QualId from @ReqData where DispId = 'Any') f on q.QualId = f.QualId)
--else
-- My recursive procedure goes here, which consumes lot of time
我的用户的示例查询如下。
这将出现在我的@ReqData
表格中,如下所示
符合所有上述标准的所有员工。@AllMandatory
这是通过我的变量捕获的
set @AllMandatory = 'Y'
上面的查询应该给我所有从事 B.Com 的任何学科的员工,以及他们在 HR 方面的 MBA。以下应该是查询结果
Eid EName
1 E HARIHARA PUTHIRAN
假设同@ReqData
一张表,如果@AllMandatory
设置为N
,则结果应如下所示,即所有做过B.Com 任何学科的员工或所有做过MBA HR 的员工。
Eid EName
1 E HARIHARA PUTHIRAN
5 SAI KIRAN GANDHE
7 SOUMYAJIT MOHANTY
9 VENKATA VEERA JAGADEESH MANNE
@ReqData
有时可能会有 50 到 60 行。为此,我只能使用循环或存储过程递归。我有大约 50,000 名员工和大约 2,00,000 份资格证书,而且还在不断增长。因此,这种循环和递归使我的系统变慢。有没有更好更简单的方法来解决这个问题?实际上是否可以通过单个查询来处理它?