LIMITandOFFSETallow you to retrieve just a portion of the rows that are 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 will be returned (but possibly less,if the query itself yields less rows).LIMIT ALLis the same as omitting theLIMITclause.
OFFSETsays to skip that many rows before beginning to return rows.OFFSET 0is the same as omitting theOFFSETclause. If bothOFFSETandLIMITappear,thenOFFSETrows are skipped before starting to count theLIMITrows that are returned.
When usingLIMIT,it is important to use anORDER BYclause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows. You may be asking for the tenth through twentieth rows,but tenth through twentieth in what ordering? The ordering is unknown,unless you specifiedORDER BY.
The query optimizer takesLIMITinto account when generating a query plan,so you are very likely to get different plans (yielding different row orders) depending on what you give forLIMITandOFFSET. Thus,using differentLIMIT/OFFSETvalues to select different subsets of a query resultwill give inconsistent resultsunless you enforce a predictable result ordering withORDER BY. This is 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 order unlessORDER BYis used to constrain the order.
The rows skipped by anOFFSETclause still have to be computed inside the server; therefore a largeOFFSETcan be inefficient.
原文链接:https://www.f2er.com/postgresql/195619.html