Improving OFFSET performance in PostgreSQL

后端 未结 5 826
失恋的感觉
失恋的感觉 2021-01-29 20:46

I have a table I\'m doing an ORDER BY on before a LIMIT and OFFSET in order to paginate.

Adding an index on the ORDER BY column makes a massive difference to performance

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 21:31

    It seems that optimizing for large OFFSETs (especially in pagination use-cases) isn't that unusual.

    It seems a little unusual to me. Most people, most of the time, don't seem to skim through very many pages. It's something I'd support, but wouldn't work hard to optimize.

    But anyway . . .

    Since your application code knows which ordered values it's already seen, it should be able to reduce the result set and reduce the offset by excluding those values in the WHERE clause. Assuming you order a single column, and it's sorted ascending, your app code can store the last value on the page, then add AND your-ordered-column-name > last-value-seen to the WHERE clause in some appropriate way.

提交回复
热议问题