I have a table in PosgreSQL 9.1.9
. There's a schema:
CREATE TABLE chpl_text
(
id integer NOT NULL DEFAULT nextval('chpl_text_id_seq1'::regclass),
page_id bigint NOT NULL,
page_idx integer NOT NULL,
...
);
I have around 40000000 (40M) rows in this table. Now, there's a query:
SELECT
...
FROM chpl_text
ORDER BY id
LIMIT 100000
OFFSET N
Everything runs smoothly for N <= 5300000
. Execution plan looks like this
Limit (cost=12743731.26..12984179.02 rows=100000 width=52)
-> Index Scan using chpl_text_pkey on chpl_text t (cost=0.00..96857560.86 rows=40282164 width=52)
But for N >= 5400000
it magically changes into
Limit (cost=13042543.16..13042793.16 rows=100000 width=52)
-> Sort (cost=13029043.16..13129748.57 rows=40282164 width=52)
Sort Key: id
-> Seq Scan on chpl_text t (cost=0.00..1056505.64 rows=40282164 width=52)
Resulting in very long runtime.
How can I prevent postresql from changeing query plan for higher offsets?
Note: I am aware, that big offsets are not good at all, but I am forced to use them here.