我的桌子很大,里面有产品。我需要以非常高的偏移量选择几种产品(下面的示例)。Postgresql 索引和性能手册建议在 ORDER BY + 最终条件使用的列上创建索引。一切都是桃子,没有使用任何种类。但是对于高偏移值 LIMIT 是非常昂贵的。有人知道这可能是什么原因吗?
以下查询可以运行几分钟。
Indexes:
"product_slugs_pkey" PRIMARY KEY, btree (id)
"index_for_listing_by_default_active" btree (priority DESC, name, active)
"index_for_listing_by_name_active" btree (name, active)
"index_for_listing_by_price_active" btree (master_price, active)
"product_slugs_product_id" btree (product_id)
EXPLAIN SELECT * FROM "product_slugs" WHERE ("product_slugs"."active" = 1) ORDER BY product_slugs.name ASC LIMIT 10 OFFSET 14859;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------
Limit (cost=26571.55..26589.43 rows=10 width=1433)
-> Index Scan using index_for_listing_by_name_active on product_slugs (cost=0.00..290770.61 rows=162601 width=1433)
Index Cond: (active = 1)
(3 rows)