Improving OFFSET performance in PostgreSQL

后端 未结 5 824
失恋的感觉
失恋的感觉 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:38

    Instead of using an OFFSET, a very efficient trick is to use a temporary table:

    CREATE  TEMPORARY TABLE just_index AS
    SELECT ROW_NUMBER() OVER (ORDER BY myID), myID
    FROM mytable;
    

    For 10 000 000 rows it needs about 10s to be created. Then you want to use SELECT or UPDATE your table, you simply:

    SELECT * FROM mytable INNER JOIN (SELECT just_index.myId FROM just_index WHERE row_number >= *your offset* LIMIT 1000000) indexes ON mytable.myID = indexes.myID
    

    Filtering mytable with only just_index is more efficient (in my case) with a INNER JOIN than with a WHERE myID IN (SELECT ...)

    This way you don't have to store the last myId value, you simply replace the offset with a WHERE clause, that uses indexes

提交回复
热议问题