简体   繁体   English

SQL 服务器中具有多个限定过滤器的搜索引擎

[英]Search Engine with Multiple Qualification Filters in SQL Server

I am trying to build a search engine like the ones Job portals have.我正在尝试建立一个像求职门户网站那样的搜索引擎。 My query is particularly related to qualification.我的查询与资格特别相关。 My end user may query for employees who have a single or set of qualification-discipline combinations, or they may give a list of qualifications with disciplines and ask for all employees who have done any of the given qualification-discipline combinations.我的最终用户可以查询具有单个或一组资格-学科组合的员工,或者他们可以提供具有学科的资格列表并询问所有完成任何给定资格-学科组合的员工。 My script is as follows.我的脚本如下。

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

Sample query of my users is as follows.我的用户的示例查询如下。

在此处输入图像描述

This will come in my @ReqData table as below这将出现在我的@ReqData表中,如下所示

在此处输入图像描述

All employees who meet all the mentioned criteria.符合所有上述标准的所有员工。 This is captured through my @AllMandatory variable这是通过我的@AllMandatory变量捕获的

set @AllMandatory = 'Y'

The above query should give me all the employees who did B.Com with any discipline and also did their MBA in HR.上面的查询应该给我所有做过 B.Com 的员工,他们有任何学科,也做过人力资源方面的 MBA。 Following should be the query result以下应该是查询结果

Eid EName
1   E HARIHARA PUTHIRAN

Assume that with the same @ReqData table, if @AllMandatory is set to N , the result should be as follows, which means all employees who have done either B.Com with any discipline or all employees who have done MBA HR.假设同样的@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 may sometimes come with 50 to 60 rows. @ReqData有时可能带有 50 到 60 行。 To achieve this I am only able to do with looping or stored procedure recursion.为此,我只能使用循环或存储过程递归。 I have around 50000 employees and around 2,00,000 qualifications which keep growing too.我有大约 50,000 名员工和大约 2,00,000 份资格证书,而且还在不断增长。 So, this looping and recursion are making my system slow.因此,这种循环和递归使我的系统变慢。 Is there a better and easier way to solve this?有没有更好更简单的方法来解决这个问题? Is it actually possible to handle it with a single query?实际上是否可以通过单个查询来处理它?

I found a solution to achieve this without looping or recursion.我找到了一种无需循环或递归即可实现此目的的解决方案。 Posting the solution below.在下面发布解决方案。 Thank you all for the help.谢谢大家的帮助。

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)
declare @CriteriaCnt int

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 2,2 union
select 6,3

select @CriteriaCnt = count(QualId) from @ReqData

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
begin
select em.* from @Emp em
inner join
(select h.Eid, count(h.QualId) Cnt from 
(select distinct q.Eid, f.QualId 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, f.QualId from @EmpQualification q
inner join (select distinct QualId from @ReqData where DispId = 'Any') f on q.QualId = f.QualId) h
group by h.Eid
having count(h.QualId) >= @CriteriaCnt) m on m.Eid = em.Eid
end

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

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