如題,我前兩天去參加 MongoDB 用戶組交流會時,聽講師說到數(shù)據(jù)量大的話不要使用 skip 和 limit
因為,這樣會一行一行的數(shù)到想到的頁數(shù)再取 PageSize 的數(shù)量。當然他們也提了另外一個方法,但是只略講了一句。
我在這里提問是想知道具體怎么做?求思路。
業(yè)精于勤,荒于嬉;行成于思,毀于隨。
If you want to get the "next page" or "previous page", you can do it by querying sort+limit that is greater than a certain _id.
If you want to get "page xxx", if you want to be completely accurate, there is actually no good way. The paging itself is a "count one by one" logic, and this time-consuming process cannot be avoided regardless of whether there is an index.
When the number of pages is very large, not many people care about the numbers after tens of millions. You can use redis to cache the _id list by page number and update it every once in a while.
The fundamental point of this problem is: "The b+ structure on which the index relies cannot be used for ranking calculations."
Record the last _id of the last query, and the next query will be {_id: {$gt: last_id}}