Reputation: 110
I'm using abp.io with MongoDB. I'm writing paged search and I see that mongo Count is very very slow. If I remove it, my API run around 50-60ms, but when I add it in, it's going to be 700-1000ms.
public async Task<PagedResultDto<a>> GetPagedList(dto input)
{
if (input.FilterKeyword.IsNotNullOrEmpty())
input.FilterKeyword = input.FilterKeyword.Trim();
var query = (await repo.GetMongoQueryableAsync())
.WhereIf(input.x != null, p => p.x == input.x)
.WhereIf(input.y != null, t => t.y == input.y)
.WhereIf(input.Id != null, t => t.Id == input.Id);
var totalCount = query.Count();
var pagedAndSortedResult = query.OrderBy(input.Sorting).PageBy(input).ToList();
return new PagedResultDto<a>(
totalCount,
ObjectMapper.Map<List<t>, List<a>>(pagedAndSortedResult)
);
}
Is any way to improve the performance of the Count method? Or any alternative solutions for paged searching?
Upvotes: 0
Views: 175
Reputation: 1289
Counting the results has to iterate the entire result set, instead of only looking at the top N results of your query page-by-page. This means there has to be a fast way (usually via indexes) to access the entire result set in order to make the count fast, if the collection isn't trivially small.
Here are some follow-up questions that will help us help further:
Or any alternative solutions for paged searching?
There are several strategies around paging, all depending on your requirements. I'm assuming you're getting a total count to know whether there are more pages. Depending on your use case this may not be necessary. One alternative is to determine whether there are more pages by checking if the page size == the number of results (downside: the last page will be empty if the total number of results is divisible by the page size).
Upvotes: 1