Home >Database >Mysql Tutorial >How does mongoDB implement paging?
This article mainly introduces in detail the two methods of mongoDB to implement paging, which has certain reference value. Interested friends can refer to it
Paging of mongoDBQuery is performed by combining the three functions of limit(), skip(), sort() array for paging query.
The following is my test data
db.test.find().sort({"age":1});
##The first method
Query the data on the first page: db.test.find().sort({"age":1}).limit(2); Query the data on the second page: db.test.find().sort({"age":1}).skip(2).limit(2) ; Query the number of other pages and so on. . .The second method
Query the data on the first page: db.test.find().sort({"age":1}).limit( 2); # is the same as the first method above. Query the data on the second page: This is to get the value of the last record on the first page, and then exclude the previous records to get it New record In summary, if the amount of data is not very large, you can use the first method. After all, it is relatively simple. If the amount of data is relatively large, it is better to use the second method, because this You don’t need to use the skip() function, skip skips too many records, and the efficiency is a bit lowAfter careful consideration, the second method is indeed not suitable for page skipping, and the efficiency It’s not very high either
For massive data, we need to do some special processing.
There are the following two methods
The first method
Limit the number of paging pages, similar to Baidu's paging processing, which only displays the previous seven hundred records, like this There is no need to consider performance issues. After all, most people just turn to the first ten pages and find what they need.
The statistical results below should be estimated, based on the proportion of these records found. Estimate the total number of records
Second methodWe can do this, assuming it is sorted according to id, we can id follow The serial number of the page where the id is located is stored in redis/MemberCached,
Just like this, assuming that each page has 10 records
id page
1 1
twenty one
. . .
10 1
11 2
12 2
. . . .
20 2
In this way, when we check the first page, we can directly retrieve ten pieces of data.
Assume there are 100 million pieces of data, and a record id occupies 4 bytes. Other information occupies one byte, and one record occupies 5 bytes
1 0000 0000 *5/(1024*1024)=476MB
This approach uses space for time, which is generally Most of the database query time is spent on the connection to the database. Putting it in the cache can greatly speed up the query speed
The above is the detailed content of How does mongoDB implement paging?. For more information, please follow other related articles on the PHP Chinese website!