I'm using PostgreSQL and I intend to paging. The target table contains 1M+ rows. In principle, this is straight forward
SELECT * FROM myTable ORDER BY orderCol LIMIT <pageSize> OFFSET <offset>;
Now, this is fast when the orderCol is indexed, but an order of magnitude slower when orderCol has no index. Obviously, the dbms is forced to perform a full table scan in the worst case and has to sort the data for each page requested.
[Edit: More specifically, orderCol
might change, i.e., is determined at runtime.]
[Edit2: The general assumption that indexing orderCol
improves sorting performance seems to be wrong. If I add an index to orderCol
, query time increases about 70%.]
One obvious solution would be to create a temporary table as necessary with an appropriate index and fill the table with the appropriate data (…I think). But that duplicates all the data.
Is there a way how one can "retain" a sort order between requests? Or create a temporary index?
Many thanks for your answers in advance.