Skip to content

Commit

Permalink
Improve performance (#80)
Browse files Browse the repository at this point in the history
* Get total count before sorting to improve performance

* Improve readability
  • Loading branch information
artem-dudarev authored Mar 27, 2020
1 parent 16cdf78 commit c6c546b
Showing 1 changed file with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ public PricingSearchServiceImpl(Func<IPricingRepository> repositoryFactory, ICat
{
var result = new PricingSearchResult<coreModel.Price>();

ICollection<CatalogProduct> products;

using (var repository = _repositoryFactory())
{
repository.DisableChangesTracking();

var query = GetPricesQuery(repository, criteria, out products);
var query = GetPricesQuery(repository, criteria, out _);

var sortInfos = criteria.SortInfos;
if (sortInfos.IsNullOrEmpty())
{
sortInfos = new[] { new SortInfo { SortColumn = ReflectionUtility.GetPropertyName<coreModel.Price>(x => x.List) } };
}

//Try to replace sorting columns names
TryTransformSortingInfoColumnNames(_pricesSortingAliases, sortInfos);

Expand All @@ -67,10 +66,11 @@ public PricingSearchServiceImpl(Func<IPricingRepository> repositoryFactory, ICat
query = query.Skip(criteria.Skip).Take(criteria.Take);
}

var pricesIds = query.Select(x => x.Id).ToList();
result.Results = _pricingService.GetPricesById(pricesIds.ToArray())
.OrderBy(x => pricesIds.IndexOf(x.Id))
.ToList();
var ids = query.Select(x => x.Id).ToList();

result.Results = _pricingService.GetPricesById(ids.ToArray())
.OrderBy(x => ids.IndexOf(x.Id))
.ToList();
}

return result;
Expand All @@ -86,6 +86,8 @@ public PricingSearchServiceImpl(Func<IPricingRepository> repositoryFactory, ICat

var query = GetPricelistsQuery(repository, criteria);

result.TotalCount = query.Count();

var sortInfos = criteria.SortInfos;
if (sortInfos.IsNullOrEmpty())
{
Expand All @@ -94,13 +96,17 @@ public PricingSearchServiceImpl(Func<IPricingRepository> repositoryFactory, ICat

query = query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id);

result.TotalCount = query.Count();
query = query.Skip(criteria.Skip).Take(criteria.Take);
var ids = query
.Skip(criteria.Skip)
.Take(criteria.Take)
.Select(x => x.Id)
.ToList();

var pricelistsIds = query.Select(x => x.Id).ToList();
result.Results = _pricingService.GetPricelistsById(pricelistsIds.ToArray())
.OrderBy(x => pricelistsIds.IndexOf(x.Id)).ToList();
result.Results = _pricingService.GetPricelistsById(ids.ToArray())
.OrderBy(x => ids.IndexOf(x.Id))
.ToList();
}

return result;
}

Expand All @@ -114,6 +120,8 @@ public PricingSearchServiceImpl(Func<IPricingRepository> repositoryFactory, ICat

var query = GetPricelistAssignmentsQuery(repository, criteria);

result.TotalCount = query.Count();

var sortInfos = criteria.SortInfos;
if (sortInfos.IsNullOrEmpty())
{
Expand All @@ -122,17 +130,20 @@ public PricingSearchServiceImpl(Func<IPricingRepository> repositoryFactory, ICat

query = query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id);

result.TotalCount = query.Count();
query = query.Skip(criteria.Skip).Take(criteria.Take);
var ids = query
.Skip(criteria.Skip)
.Take(criteria.Take)
.Select(x => x.Id)
.ToList();

var pricelistAssignmentsIds = query.Select(x => x.Id).ToList();
result.Results = _pricingService.GetPricelistAssignmentsById(pricelistAssignmentsIds.ToArray())
.OrderBy(x => pricelistAssignmentsIds.IndexOf(x.Id))
.ToList();
result.Results = _pricingService.GetPricelistAssignmentsById(ids.ToArray())
.OrderBy(x => ids.IndexOf(x.Id))
.ToList();
}

return result;
}

#endregion


Expand Down Expand Up @@ -162,9 +173,11 @@ protected virtual IQueryable<PriceEntity> GetPricesQuery(IPricingRepository repo
Sort = criteria.Sort.Replace("product.", string.Empty),
ResponseGroup = SearchResponseGroup.WithProducts,
};

var catalogSearchResult = _catalogSearchService.Search(catalogSearchCriteria);

var productIds = catalogSearchResult.Products.Select(x => x.Id).ToArray();

//preserve resulting products for future assignment to prices
products = catalogSearchResult.Products;

Expand Down Expand Up @@ -230,8 +243,5 @@ private static void TryTransformSortingInfoColumnNames(IDictionary<string, strin
}
}
}


}
}

0 comments on commit c6c546b

Please sign in to comment.