Postgresql LIMIT and OFFSET

Source: Internet
Author: User
Tags postgresql

LIMIT and OFFSET allow you to retrieve just a portion of the rows that is generated by the rest of the query:

selectselect_listfromtable_expression[LIMIT {number| All}] [offsetnumber]

If a limit count is given, no more than that many rows would be returned (but possibly less, if the query itself yields les s rows). Limit all is the same as omitting the limit clause.

OFFSET says to skip this many rows before beginning to return rows. Offset 0 is the same as omitting the offset clause. If both offset and LIMIT appear, then OFFSET rows is skipped before starting to count the LIMIT rows is returned.

When using-LIMIT, it is important-to-use-ORDER BY clause-constrains the result rows into a unique ORDER. Otherwise you'll get an unpredictable subset of the "s rows. is asking for the tenth through twentieth rows, but tenth through twentieth in what ordering? The ordering is unknown, unless your specified ORDER by.

the query optimizer takes  limit into when generating a query plan is very likely to get different plans (yielding differ ENT row orders) depending on what give for limit and offset. Thus, using Different limit/offset values to select different subsets of a query Result will give inconsistent results  unless you enforce a predictable result ordering with order by. This was not a bug; It is an inherent consequence of the fact that SQL does not promise to deliver the results of a query in any particular or Der Unless order By is used to constrain the ORDER.

The rows skipped by an OFFSET clause still has to be computed inside the server; Therefore a large OFFSET can be inefficient.

Postgresql LIMIT and OFFSET

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.