Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Skip/Take is very slow when number to skip is big

So, code is very simple:

var result = dbContext.Skip(x).Take(y).ToList();

When x is big (~1.000.000), query is very slow. y is small - 10, 20.

SQL code for this is: (from sql profiler)

SELECT ...
FROM ...
ORDER BY ...
OFFSET x ROWS FETCH NEXT y ROWS ONLY

The question is if anybody knows how to speed up such paging?

like image 619
berliner Avatar asked Dec 30 '15 14:12

berliner


People also ask

Is Addrange slow?

30 rows per seconds is certainly not "in the ballpark". That is extremely slow. Use SQL Server Profiler to capture some execution plans for those inserts. You'll probably find some slow statements there.

Is Entity Framework slower than ado net?

Entity framework is ORM Model, which used LINQ to access database, and code is autogenerated whereas Ado.net code is larger than Entity Framework. Ado.net is faster than Entity Framework.


1 Answers

I think OFFSET .. FETCH is very useful when browsing the first pages from your large data (which is happening very often in most applications) and have a performance drawback when querying high order pages from large data.

Check this article for more details regarding performance and alternatives to OFFSET .. FETCH.

Try to apply as many filters to your data before applying paging, so that paging is run against a smaller data volume. It is hard to imagine that the user wants no navigate through 1M rows.

like image 147
Alexei - check Codidact Avatar answered Sep 19 '22 13:09

Alexei - check Codidact