Query Optimization for EF Query - sql

I am using EF core to perform a complex query. I need to use group by and pick the top result from the group and then perform filter on the top rows of each group.
When I use EF profiler, it shows that query fetched 21647 rows and the final result after the filter is 28. I assume that EF uses the where query after retrieving the columns from DB.
Here is the structure of query.
private IQueryable<long> GetAssignedIds(int? lpId = null, int? customerTenantId = null)
{
var islpAdmin = _permissionChecker.IsGranted(AppPermissions.Pages_PP_lpAdmin);
var query = (
from -------QUERY--------------------
join -------QUERY--------------------
join -------QUERY--------------------
join -------QUERY--------------------
select new
{
-----SELECT VARIABLES----------
})
.OrderBy(c => c.Priority)
.GroupBy(c => new { c.ScpId, c.OrderId })
.Select(c => c.FirstOrDefault())
.WhereIf(lpId.HasValue, c => c.lpId == lpId)
.WhereIf(islpAdmin, c => c.lpId == currentTenantId)
.WhereIf(!islpAdmin, c => c.eId == currentTenantId)
.Select(c => c.ScoId)
.Distinct();
}
How can I optimize this query? I know .Select(c => c.FirstOrDefault()) is the problem here but I'm not able to find proper alternative here.

Related

Multiple Conditions in Nhibernate Left Join

Please need some help converting this sql query to nhibernate
select a.ID, count(b.ID)
from appusers a
left join weeklytasks b on a.ID = b.TaskOwner and b.taskstatus = 1
group by a.ID
It's difficult to answer without knowing your entities, mappings, used technology(ICreteria API, QueryOver, Linq).
But I can suggest this solution using QueryOver:
AppUser ownerAlias = null;
WeeklyTask taskAlias = null;
var result = Session.QueryOver(() => taskAlias)
.JoinAlias(x => x.TaskOwner,
() => ownerAlias,
NHibernate.SqlCommand.JoinType.RightOuterJoin,
Restrictions.Where(() => taskAlias.Status == 1))
.SelectList(list => list
.SelectGroup(x => ownerAlias.Id)
.SelectCount(x => x.Id))
.List<object[]>();
or this:
var result = Session.QueryOver<WeeklyTask>()
.Where(x => x.Status == 1)
.Right.JoinQueryOver(x => x.TaskOwner)
.SelectList(list => list
.SelectGroup(x => x.TaskOwner.Id)
.SelectCount(x => x.Id))
.List<object[]>();
Please notice that in this approach your WeeklyTask entity must contains mapped reference to AppUser entity.

How would one create this query using NHibernate QueryOver instead of Linq?

I need to use QueryOver instead of Linq but am struggling to recreate the following query:
public IQueryable<AuctionItem> GetLiveAuctionItems(){
repository.Query<AuctionItem>().Where(IsInActiveAuction()
}
public static Expression<Func<AuctionItem, bool>> IsInActiveAuction()
{
var now = SystemTime.Now();
var expression = PredicateBuilder.True<AuctionItem>();
return expression.And
(x => x.Lots.Any(
z => z.Auction.StartTime < now && z.Auction.EndTime > now && !z.DateWithdrawn.HasValue
&& z.DateApproved.HasValue));
}
I realise this creates subqueries but when I try to create using queryover I get errors stating projections needed.
Any help is much appreciated.
A quick draft with a clear how to steps. The first part, the subquery could look like this:
QueryOver<Lot> subQuery =
QueryOver.Of<Lot>(() => lot)
// Lot WHERE
.WhereRestrictionOn(() => lot.DateWithdrawn).IsNull
.AndRestrictionOn(() => lot.DateApproved).IsNotNull
// Auction JOIN
.JoinQueryOver<Auction>(l => l.Auction, () => auction)
// Auction WHERE
.Where(() => auction.StartTime < now)
.Where(() => auction.EndTime > now)
// AuctionItem.ID SELECT == projection
.Select(Projections.Property(() => lot.AuctionItem.ID))
;
So, this will return the AuctionItem.ID, which does meet our seraching criteria. And we can use it like this:
AuctionItem auctionItem = null;
var query = session.QueryOver<AuctionItem>(() => auctionItem)
.WithSubquery
.WhereProperty(() => auctionItem.ID)
.In(subQuery)
...

NHibernate QueryOver subquery to return only newest record

I have a query in Nhibernate QueryOver which brings back a collection of episode objects (episode being a spell of care) which in turn has a collection of episode statuses as a property of each episode. However I want to change this so that each episode only brings back the latest status update for that episode instead of all of them.
The SQL to do this is as follows:
SELECT *
FROM DIPEpisode e
INNER JOIN DIPEpisodeStatus s on s.EpisodeID = e.SequenceID
WHERE e.ClientID = '1000001'
AND s.SequenceID IN (
SELECT TOP 1 SequenceID
FROM DIPEpisodeStatus s
WHERE s.EpisodeID = e.SequenceID
ORDER BY StatusRecordedDate DESC
)
I have written the following query which gives me almost exactly what I need
var statuses =
QueryOver.Of<DIPEpisodeStatus>()
.OrderBy(x => x.StatusRecordedDate).Desc
.Select(x => x.Id).Take(1);
DIPEpisodeStatus statusAlias = null;
return
session.QueryOver<DIPEpisode>()
.JoinQueryOver(x => x.DIPEpisodeStatuss, () => statusAlias)
.Fetch(x => x.AgencyID).Eager
.Fetch(x => x.DIPEpisodeStatuss).Eager
.Where(e => e.ClientID.Id == this.clientId)
.WithSubquery.WhereProperty(x => x.Id).Eq(statuses)
.List();
This generates the following SQL:
SELECT *
FROM DIPEpisode this_
inner join DIPEpisodeStatus statusalia1_
on this_.SequenceID = statusalia1_.EpisodeID
WHERE statusalia1_.ClientID = '1000001' /* #p0 */
and statusalia1_.SequenceID = (SELECT TOP (1 /* #p1 */) this_0_.SequenceID as y0_
FROM DIPEpisodeStatus this_0_
ORDER BY this_0_.StatusRecordedDate desc)
As you can see, the only thing missing is the where clause from the subquery. What changes do I need to make to the query in order to generate this extra where clause and pull back only the most recent status update?
Thanks
Ben
the collection DIPEpisodeStatuss is always initialized with all entities because it would break changetracking otherwise. you could either define a filter for the collection or return a DTO with what you want. Also the fetch will be ignored because it can not eager load and filter in one sql statement.
NHibernate filters are explained here
defining Filters in FNH
how it would be done with a DTO
// assuming SequneceID and StatusRecordedDate correlates
var subquery = QueryOver.Of<DIPEpisode>()
.Where(e => e.ClientID.Id == this.clientId)
.JoinAlias(e => e.DIPEpisodeStatuss, () => statusAlias)
.Select(Projections.Max(() => statusAlias.SequenceID));
// or as in question
var subquery = QueryOver.Of<DIPEpisode>()
.Where(e => e.ClientID.Id == this.clientId)
.JoinAlias(e => e.DIPEpisodeStatuss, () => statusAlias)
.OrderByDescending(() => statusAlias.StatusRecordedDate)
.Select(() => statusAlias.SequenceID)
.Take(1);
DIPEpisodeDto dto = null;
DIPEpisodeStatus statusAlias = null;
return session.QueryOver<DIPEpisode>()
.Where(e => e.ClientID.Id == this.clientId)
.JoinQueryOver(e => e.DIPEpisodeStatuss, () => statusAlias)
.WithSubquery.WhereProperty(estatus => estatus.Id).Eq(statuses)
.SelectList(list => list
.Select(e => e.Whatever).WithAlias(() => dto.Whatever)
.Select(() => statusAlias.SquenceId).WithAlias(() => dto.StatusId)
...
)
.TransFormUsing(Transformers.AliasToBean<DIPEpisodeDto>())
.List();
or using LINQ
var query = from e in session.Query<DIPEpisode>()
from s in e.DIPEpisodeStatuss
where e.ClientID.Id == this.clientId
where s.Id == (
from e2 in session.Query<DIPEpisode>()
from s2 in e2.DIPEpisodeStatuss
orderby s2.StatusRecordedDate descending
select s2.Id)
.First()
select new DIPEpisodeDto
{
e.Prop1,
Status = s,
};
return query.List<DIPEpisodeDto>();

Linq to NHibernate: Select multiple sums at once

Is there a way to select multiple sums at once using Linq to NHibernate?
Now I have
int? wordCount = (from translation in session.Query<TmTranslation>()
where translation.SatisfiesCondition
select translation.TranslationUnit)
.Sum(x => (int?)(x.WordCount + x.NumberCount)) ?? 0;
int? tagCount = (from translation in session.Query<TmTranslation>()
where translation.SatisfiesCondition
select translation.TranslationUnit)
.Sum(x => (int?)(x.TagCount)) ?? 0;
int? characterCount = (from translation in session.Query<TmTranslation>()
where translation.SatisfiesCondition
select translation.TranslationUnit)
.Sum(x => (int?)(x.CharacterCount)) ?? 0;
which generates three different SQL queries. In SQL I can grab them all three at once, but is there a way to do this in Linq to NHibernate?
Thank you.
this should help get you started with QueryOver way...
ResultDTO dtoAlias = null; //placeholder alias variable
var dto = session.OueryOver<TmTranslation>()
.Where(x => x.SatisfiesCondition)
//Change this to the actual type of Translation property
.JoinQueryOver<Translation>(x => x.Translation)
.SelectList(list => list
//we can sum these columns individually to keep query simple,...add them together later
.SelectSum(x => x.WordCount).WithAlias(() => dtoAlias.WordCountTotal)
.SelectSum(x => x.NumberCount).WithAlias(() => dtoAlias.NumberCountTotal)
//add more select sums to the select list
)
.TransformUsing(Transformers.AliasToBean<ResultDTO>())
.SingleOrDefault<ResultDTO>();
Having multiple aggregate functions within a Select() call works and results in a single SQL command sent to the database. For example:
var result = session.Query<TmAssignment>()
.Select(a => new
{
Words = session.Query<TmTranslation>().Where(s => s.Assignment == a).Sum(u => (int?) u.WordCount) ?? 0,
Others = session.Query<TmTranslation>().Where(s => s.Assignment == a).Sum(u => (int?)(u.TagCount + u.NumberCount)) ?? 0,
}).ToList();
A potentially simpler solution I use is to do an artificial GroupBy then project to an anonymous object:
eg.
session.Query<TmTranslation>()
.Where(o => o.SatisfiesCondition)
.Select(o => o.TranslationUnit)
.GroupBy(o => 1)
.Select(o => new
{
WordCount = o.Sum(x => (int?)(x.WordCount + x.NumberCount)) ?? 0,
TagCount = o.Sum(x => (int?)(x.TagCount)) ?? 0,
CharacterCount = o.Sum(x => (int?)(x.CharacterCount)) ?? 0
})
.Single();
This also produces just a single SQL statement and reduces the duplication nicely.

NHibernate QueryOver how to join on non declared relationship

How to do the following join to return Users who have access to a Company given a company id.
The problem is there is no explicit relationship using a User object between UserAccess and User they simply join on the string property Username:
User(Username, Name)
UserAccess(Username, Company)
Company(Id)
Session.QueryOver<Company>()
.Where(c => c.Id == companyId)
.JoinQueryOver<UserCompanyAccess>(u => u.UserAccessList)
.JoinQueryOver<User>(u => **Nope no property, just a string**
could be done with a subquery
var subquery = QueryOver.Of<Company>()
.Where(c => c.Id == companyId)
.JoinQueryOver<UserCompanyAccess>(u => u.UserAccessList)
.Select(uca => uca.UserName);
var users = session.QueryOver<User>()
.WithSubquery.WhereProperty(u => u.Name).In(subquery)
.List();
As of 5.1.0, it is possible to make hibernate generate an actual sql join on an undeclared (unmapped) relationship. E.g. all orders sorted by customer's spending:
var criteria = _session
.CreateCriteria<Order>("order");
criteria
.CreateEntityAlias(
"customer",
Restrictions.EqProperty("order.customerId", "customer._id"),
JoinType.LeftOuterJoin,
typeof(Customer).FullName)
.AddOrder(new Order("customer._lifetimeSpending", ascending:false));
return criteria.List<Order>();
Also possible with QueryOver (sample from NHibernate docs):
Cat cat = null;
Cat joinedCat = null;
var uniquelyNamedCats = sess.QueryOver<Cat>(() => cat)
.JoinEntityAlias(
() => joinedCat,
() => cat.Name == joinedCat.Name && cat.Id != joinedCat.Id,
JoinType.LeftOuterJoin)
.Where(() => joinedCat.Id == null)
.List();