【发布时间】:2014-05-04 22:21:27
【问题描述】:
我在PosgreSQL 9.1.9 有一张桌子。有一个架构:
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,
...
);
我在这个表中有大约 40000000 (40M) 行。 现在,有一个查询:
SELECT
...
FROM chpl_text
ORDER BY id
LIMIT 100000
OFFSET N
N <= 5300000 一切顺利。执行计划是这样的
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)
但是对于N >= 5400000,它会神奇地变成
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)
导致运行时间很长。
如何防止 postresql 更改查询计划以获得更高的偏移量?
注意:我知道,大偏移一点都不好,但我不得不在这里使用它们。
【问题讨论】:
-
您是否使用默认配置值?您应该尝试增加 work_mem 和其他内存相关参数的数量。
-
根据 postgres DOC (postgresql.org/docs/9.1/static/sql-select.html#SQL-LIMIT) 这是预期的。在这里查看类似的帖子dba.stackexchange.com/questions/32956/…。
-
你能用 EXPLAIN (ANALYZE,BUFFERS) 重复一遍吗?
标签: sql postgresql explain