Two methods of MongoDB paging __mangodb

Source: Internet
Author: User
Tags mongodb

Http://www.cnblogs.com/xiaolai/p/3401289.html

MongoDB paging query is done by limit (), skip (), sort (), which is a combination of three functions for paging queries.

Here's my test data.

Db.test.find (). Sort ({"Age": 1});

The first of these methods

Query the first page of data: Db.test.find (). Sort ({"Age": 1}). Limit (2);

Query the data for the second page: Db.test.find (). Sort ({"Age": 1}). Skip (2). Limit (2);

Query other pages and so on ...

The second method

Query the first page of data: Db.test.find (). Sort ({"Age": 1}). Limit (2);

Same as the first method above.

Query the data for the second page:

This is to get the value of the last record on the first page and then exclude the previous record and get the new record.

In summary, if the amount of data is not very large, you can use the first method, after all, relatively simple, if the amount of data is relatively large, use the second method is better, because this can not skip () This function, skip skip too many records, the efficiency is a little low

After careful consideration, the second method really does not fit the page, and the efficiency is not very high

For massive amounts of data, we have to do something special,

There are 2 different ways to do this

The first of these methods

Limit the number of pages, similar to Baidu Baidu's paging processing, just show the front of more than 700 records, so you do not have to consider the performance of the problem, after all, the average person just turned to the front 10 pages, they find their own needs

The following statistical results should be calculated to estimate the total number of records based on the proportion of the records identified.

The second method

We can do this, assuming it's sorted by ID, we can save the ID and the number of pages in the ID to redis/membercached,

Just like this, assuming that each page has 10 records

ID page

1 1

2 1

。。。

10 1

11 2

12 2

。。。。

20 2

So that when we look at the first page, we can just take out 10 data.

Suppose you have 100 million data, one record ID is 4 bytes, the other information is one byte, and one record is 5 bytes.

1 0000 0000 *5/(1024*1024) =476MB

This practice uses space for time, the general database query time spent mostly on the connection with the database, put in the cache, can greatly speed up the query speed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.