Hi, users of StackOverflow,
I ran into this problem. I have three QueryOvers, and each of them returns a list of candidate identifiers, which I then use to list these candidates. For this, I wrote the following code.
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)))); }
This returns the correct results, and the generated query looks like
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**))
The problem is that it uses an "or". Because of this, the request is painfully slow.
If instead I write this:
SELECT * FROM Applicants WHERE IsActive = 1 and (Id in (SELECT Id from **FirstQueryOver** union SELECT Id from **SecondQueryOver** union SELECT Id from **ThirdQueryOver**))
It ends almost instantly.
Do you have any ideas on how to reorganize my code for better performance?
Thanks Adrian.
source share