加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Postgresql LIMIT and OFFSET

发布时间:2020-12-13 17:26:53 所属栏目:百科 来源:网络整理
导读: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 row

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读