TODO: Database Optimization Paging

Source: Internet
Author: User

TODO: Database Optimization Paging

650) this.width=650; "class=" Alignnone size-full wp-image-925 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/11/11fb0001c697d1419eaa-1.jpg "width=" 554 "height=" 415 "style=" border:0px;vertical-align:middle;margin:0px Auto;height:auto; "alt=" 11fb0001c697d1419eaa-1.jpg "/>

The example of this article is based on the MongoDB database, other databases can also be extrapolate to optimize.

Paging using in MongoDB

A.skip (n) skips the first n matching documents;

B.limit (m) returns the m result, if the result of the match is less than m, the result of matching data is returned, and M is the specified upper number, not the lower number;

C.sort ({"Name": 1, "Address":-1}), 1 means ascending,-1 means descending.

Skipping a small number of documents with skip is also possible. But with a lot of data, skip will become very slow, and every database will have this, so try to avoid too much use of skip. So what do we do with paging, we can use the last result to calculate the next query.

1. Paging using Skip

Page1 = Db.user.find ({}). Limit (100)

Page2 = Db.user.find ({}). Skip (+). Limit (100)

Page3 = Db.user.find ({}). Skip ($). Limit (100)

2. Use the last result to calculate the next query, sorted by timestamp (timestamp)

Get first page

Page1 = Db.user.find ({}). sort ({"timestamp":-1}). Limit (10)

Gets the timestamp of the last record of the current page Lasttimestamp,

Query the next page of data according to Lasttimestamp

Nextpage=db.user.find ({"timestamp": {"$GT": Lasttimestamp}}). sort ({"timestamp":-1}). Limit (10)

This way, the query does not use skip, but make sure that the timestamp unique constraint ensures that the data in the document does not have the same value.

Wxgzh:ludong86

650) this.width=650; "class=" AlignCenter size-full wp-image-845 "src=" http://www.todosomeone.com/wp-content/uploads /2016/11/qrcode_for_gh_6bb1f39ae99c_258-1.jpg "alt=" qrcode_for_gh_6bb1f39ae99c_258-1 "width=" 258 "height=" 258 " style= "border:0px;vertical-align:middle;margin:0px auto;height:auto;text-align:center;"/>


This article is from the "12321875" blog, please be sure to keep this source http://12331875.blog.51cto.com/12321875/1875967

TODO: Database Optimization Paging

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.