Optimization Proposal - Reusing the query of specification
See original GitHub issueFor pagination, i need to get the total number records before applying the pagination. So, i do not want the query rebuilt each time; for counting the total number of records and fetching the records for the current page. Thus, i designated the EfRepository.ListForPaging. What do you think about it?
public abstract class BaseSpecification<T> : ISpecification<T>
{
public bool IsPagingEnabled { get; set; } // It is modifiable.
}
public class SpecificationEvaluator<T> where T : BaseEntity
{
public static IQueryable<T> ApplyPaging(IQueryable<T> inputQuery, ISpecification<T> specification)
{
return inputQuery.Skip(specification.Skip).Take(specification.Take);
}
}
public class EfRepository<T> : IRepository<T>, IAsyncRepository<T> where T : BaseEntity
{
// TotalCount: Total count of records before paging applied.
// List: List of records after paging applied.
public (int totalCount, IEnumerable<T> list) ListForPaging(ISpecification<T> spec)
{
// Disable paging to get total count.
spec.IsPagingEnabled = false;
var query = ApplySpecification(spec);
// Get count through query, not specification.
var totalCount = query.Count();
// Paging is applied to the existing query so rebuilding the query is eliminated.
var list = SpecificationEvaluator<T>.ApplyPaging(query, spec).AsEnumerable();
return (totalCount, list);
}
}
Usage
var filterSpecification = new CategoryFilterPaginatedSpecification(pageIndex, itemsPerPage, queryConstraints);
var (filteredCount, list) = _categoryRepository.ListForPaging(filterSpecification);
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Chapter 4. Query Performance Optimization
The server parses, preprocesses, and optimizes the SQL into a query execution plan. The query execution engine executes the plan by making calls...
Read more >Leveraging Re-costing for Online Optimization of ...
Parametric query optimization (PQO) deals with the prob- lem of finding and reusing a relatively small number of plans that can achieve good...
Read more >SQL Query Optimization and Tuning to Improve Performance
Looking for the best tips for server performance optimization and tuning SQL Server? Check our article and list of best SQL query ......
Read more >Advanced Spring Data JPA - Specifications and Querydsl
We're using the meta-model classes introduced with JPA 2.0 and generated by the Annotation Processing API. The main problem with this code is ......
Read more >Data dependencies for query optimization: a survey
In this process, query optimization is the task of finding an optimal (or at least very good) physical execution plan with respect to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I read your comments once again. Obviously, your concern is building the query twice, not the the round trip to DB. Building the query (as it is done here), is just an in-memory process, and won’t affect too much. But, in some super large apps yea it might have an impact. I got your point, I just have few notes
@expressiveco I don’t see why that would be a problem in terms of general architecture or OOP! 😃