Using skip() and limit()
efagerberg opened this issue · 2 comments
From going through the code it seems pagination is occurring using skip
and limit
. In mongo using skip can become quiet expensive as the collection grows, instead it is recommended that pagination be done by ordering by the _id or a timestamp field and altering the query based on values greater than the last value retrieved.
@efagerberg what if the collection is to be shown based on ordering of different fields? maybe the table which shows the records has ability to order it using the columns of the table..
enforcing and ordering for pagination doesn't seem right and causes alot more problems and would affect alot of production systems.
Thank you @alfie-max.
@efagerberg: will_paginate was designed entirely around the concepts of SQL LIMIT
+ OFFSET
(a.k.a. skip
) + COUNT
(to get total entry count and thus calculate the number of pages). This has benefits such as simplicity of implementation and use, but also has drawbacks such as large OFFSET performing poorly in specific databases and COUNT being expensive to perform (or a specific db adapter doesn't know at all how to perform it for complex queries with JOIN or GROUP BY).
instead it is recommended that pagination be done by ordering by the _id or a timestamp field and altering the query based on values greater than the last value retrieved.
Yes, this is the superior approach in some cases. Unfortunately, will_paginate cannot handle these cases, since the implementation would be entirely different, it would be app-dependent, and we'd end up building an entire new library within a library. All this is best covered by either ad-hoc code in your own app or a different library.