0

I implemented paging using Entity Framework using .skip and .take. This works fine, but when I fetch the count of number of records in the database for 10 lakhs records (count is required for UIClient for grid page numbers) it is taking lot of time, around 600 ms which is huge.

If I don't use the count only the paging is implemented then it is taking hardly 20 to 25 ms. How to make efficient count? How can I bring down from 600 ms to say around 50 ms?

sample query that I used:

int count = (from c in dbcontext.Customer
             where c.customerName ='xyz' && c.date >= 'dateTime'
             select c.CustomerId).Count();

I have index on Name, dateTime and CustomerId is the primary key.

Thanks in advance,

Abhinay

1
  • Is this 'dateTime' value a placeholder? What is the type of c.date property? Dec 12, 2012 at 14:57

2 Answers 2

1

Can you use SQL Server Profiler to grab the query being generated from entity framework, and run it through query analyzer in SQL Server Management Studio?

From there you can see the execution plan and see where the bottleneck is happening, and tweak your code to perform better.

I think the problem will become more obvious with this information.

0

Try to use count on the records you are already using before pagination and see if performes better:

var records = (from c in dbcontext.Customer where c.customerName ='xyz' && c.date >= 'dateTime' select c.CustomerId);

var total = records.Count;
var pagedRecors = records.Skip((currentPage - 1) * recordsPerPage).Take(recordsPerPage).ToList();
1
  • Good idea - BUT: since CustomerId is the primary key, most likely it's also the clustering key, and thus your statement will result in a clustered index scan (i.e. a full table scan) so I don't think this will save any time, really.
    – marc_s
    Dec 12, 2012 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.