Should I use LINQ’s Skip() and Take() method for paging, or implement my own paging with a SQL query?
Which is most efficient? Why would I choose one over the other?
I’m using SQL Server 2008, ASP.NET MVC and LINQ.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Trying to give you a brief answer to your doubt, if you execute the
skip(n).take(m)methods on linq (with SQL 2005 / 2008 as database server) your query will be using theSelect ROW_NUMBER() Over ...statement, with is somehow direct paging in the SQL engine.Giving you an example, I have a db table called
mtcityand I wrote the following query (work as well with linq to entities):The resulting query will be:
Which is a windowed data access (pretty cool, btw cuz will be returning data since the very begining and will access the table as long as the conditions are met). This will be very similar to:
With the exception that, this second query will be executed faster than the linq result because it will be using exclusively the index to create the data access window; this means, if you need some filtering, the filtering should be (or must be) in the Entity listing (where the row is created) and some indexes should be created as well to keep up the good performance.
Now, whats better?
If you have pretty much solid workflow in your logic, implementing the proper SQL way will be complicated. In that case LINQ will be the solution.
If you can lower that part of the logic directly to SQL (in a stored procedure), it will be even better because you can implement the second query I showed you (using indexes) and allow SQL to generate and store the Execution Plan of the query (improving performance).