我一直在努力弄清楚如何让查询规划在一段时间内变得更聪明一些,现在非常不成功。我已经和work_mem和朋友搞混了,运行vacumm analyze
很多,并尝试用order by
改变查询。我已经包含了3次使用不同偏移量的同一查询。我的印象是这个查询的效果不尽如人意。有什么想法吗?
万一它不会跳出来 - 以下查询之间的唯一变化是offset
bloomapi=# explain analyze SELECT * FROM npis WHERE provider_last_name_legal_name = 'THOMPSON' offset 250 limit 10;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=965.13..998.97 rows=10 width=2589) (actual time=568.458..577.507 rows=10 loops=1)
-> Bitmap Heap Scan on npis (cost=119.15..20382.11 rows=5988 width=2589) (actual time=58.140..577.027 rows=260 loops=1)
Recheck Cond: ((provider_last_name_legal_name)::text = 'THOMPSON'::text)
-> Bitmap Index Scan on npis_temp_provider_last_name_legal_name_idx1 (cost=0.00..117.65 rows=5988 width=0) (actual time=36.819..36.819 rows=5423 loops=1)
Index Cond: ((provider_last_name_legal_name)::text = 'THOMPSON'::text)
Total runtime: 578.301 ms
(6 rows)
bloomapi=# explain analyze SELECT * FROM npis WHERE provider_last_name_legal_name = 'THOMPSON' offset 100 limit 10;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=395.81..435.40 rows=10 width=2589) (actual time=0.397..0.440 rows=10 loops=1)
-> Index Scan using npis_temp_provider_last_name_legal_name_idx1 on npis (cost=0.00..23701.38 rows=5988 width=2589) (actual time=0.063..0.293 rows=110 loops=1)
Index Cond: ((provider_last_name_legal_name)::text = 'THOMPSON'::text)
Total runtime: 0.952 ms
(4 rows)
bloomapi=# explain analyze SELECT * FROM npis WHERE provider_last_name_legal_name = 'THOMPSON' offset 4100 limit 10;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=13993.25..14027.09 rows=10 width=2589) (actual time=9356.723..9400.021 rows=10 loops=1)
-> Bitmap Heap Scan on npis (cost=119.15..20382.11 rows=5988 width=2589) (actual time=2.968..9393.327 rows=4110 loops=1)
Recheck Cond: ((provider_last_name_legal_name)::text = 'THOMPSON'::text)
-> Bitmap Index Scan on npis_temp_provider_last_name_legal_name_idx1 (cost=0.00..117.65 rows=5988 width=0) (actual time=1.943..1.943 rows=5423 loops=1)
Index Cond: ((provider_last_name_legal_name)::text = 'THOMPSON'::text)
Total runtime: 9400.426 ms
(6 rows)
一些相关说明:
答案 0 :(得分:3)
我有根据的猜测是,大额抵消正在触发这些计划。即使您将结果限制为十行,PostgreSQL也必须考虑前面的所有行。我怀疑当您删除offset
时(例如在第一个查询中使用limit 260
),您会看到类似的运行时。
您可以使用configuration parameters禁用某些计划类型,直到查询共享相似的计划。这可以帮助您了解为什么一个计划比另一个更好。
set enable_bitmapscan = false;