嗨 StackOverflow 用户,
我遇到了这个问题,我有三个 QueryOver,每个都返回一个候选 ID 列表,然后我用它来带来这些候选。为此,我编写了以下代码。
private IQueryOver<CandidateEntity, CandidateEntity> UnionPublicWithPrivateCandidates(
IQueryOver<CandidateEntity, CandidateEntity> publicCandidates,
IQueryOver<CandidateEntity, CandidateEntity> privateCandidate,
IQueryOver<CandidateEntity, CandidateEntity> candidatesByUserRole)
{
return ActiveCandidatesQueryOver.Where(Restrictions.Disjunction()
.Add(Subqueries
.WhereProperty<CandidateEntity>(c => c.Id)
.In((QueryOver<CandidateEntity>)publicCandidates.Select(c => c.Id)))
.Add(Subqueries
.WhereProperty<CandidateEntity>(c => c.Id)
.In((QueryOver<CandidateEntity>)privateCandidate.Select(c => c.Id)))
.Add(Subqueries
.WhereProperty<CandidateEntity>(c => c.Id)
.In((QueryOver<CandidateEntity>)candidatesByUserRole.Select(c => c.Id))));
}
这将返回正确的结果,生成的查询如下所示
SELECT *
FROM Applicants
WHERE IsActive = 1
and (Id in (SELECT Id from **FirstQueryOver**)
**or** Id in (SELECT Id from **SecondQueryOver**)
**or** Id in (SELECT Id from **ThirdQueryOver**))
问题是它使用“或”。因此,查询非常缓慢。
相反,如果我写这个:
SELECT *
FROM Applicants
WHERE IsActive = 1
and (Id in (SELECT Id from **FirstQueryOver**
union SELECT Id from **SecondQueryOver**
union SELECT Id from **ThirdQueryOver**))
它几乎立即完成。
您对我应该如何重构代码以获得更好的性能有任何想法吗?
谢谢你,阿德里安。