首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Postgres不会使用索引,这取决于where子句中id的特定值

Postgres不会使用索引,这取决于where子句中id的特定值
EN

Stack Overflow用户
提问于 2017-01-18 04:26:25
回答 1查看 214关注 0票数 2

我已经修补/阅读了一段时间,但找不到任何在这里工作的优化……我已经为连接中的相关it建立了索引,我尝试了手动真空,我还尝试了对索引进行聚类,以便查询优化器不会因为一些分散的行而认为扫描整个表的效率更高(尽管我对查询规划了解不多)。

我正在尝试获取单个id的连接结果(出于调试目的)。我发现一些单个I的查询需要大约2分钟,而大多数(99%?)1秒内返回。下面是一些explain analyze(出于保密考虑,我用sed更改了一些名称):

代码语言:javascript
复制
main=> explain analyze SELECT e.customer_id, l.*
            FROM abc.encounter e 
            JOIN abc.log l
            ON e.encounter_id = l.encounter_id
            AND e.customer_id = '1655563';
                                                                     QUERY PLAN                                                                      
-----------------------------------------------------------------------------------------------------------------------------------------------------
 Hash Join  (cost=2751.69..2566740.95 rows=13262 width=75) (actual time=122038.725..226694.004 rows=249 loops=1)
   Hash Cond: (l.encounter_id = e.encounter_id)
   ->  Seq Scan on log l  (cost=0.00..2190730.92 rows=99500192 width=66) (actual time=0.005..120825.675 rows=99500192 loops=1)
   ->  Hash  (cost=2742.81..2742.81 rows=710 width=18) (actual time=0.309..0.309 rows=89 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 13kB
         ->  Bitmap Heap Scan on encounter e  (cost=17.93..2742.81 rows=710 width=18) (actual time=0.037..0.197 rows=89 loops=1)
               Recheck Cond: (customer_id = '1655563'::text)
               Heap Blocks: exact=46
               ->  Bitmap Index Scan on idx_abc_encounter_customer_id  (cost=0.00..17.76 rows=710 width=0) (actual time=0.025..0.025 rows=89 loops=1)
                     Index Cond: (customer_id = '1655563'::text)
 Planning time: 0.358 ms
 Execution time: 226694.311 ms
(12 rows)

main=> explain analyze SELECT e.customer_id, l.*
            FROM abc.encounter e 
            JOIN abc.log l
            ON e.encounter_id = l.encounter_id
            AND e.customer_id = '121652491';
                                                                      QUERY PLAN                                                                      
------------------------------------------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=36.67..53168.06 rows=168 width=75) (actual time=0.090..0.422 rows=11 loops=1)
   ->  Index Scan using idx_abc_encounter_customer_id on encounter e  (cost=0.43..40.53 rows=9 width=18) (actual time=0.017..0.047 rows=17 loops=1)
         Index Cond: (customer_id = '121652491'::text)
   ->  Bitmap Heap Scan on log l  (cost=36.24..5888.00 rows=1506 width=66) (actual time=0.016..0.017 rows=1 loops=17)
         Recheck Cond: (encounter_id = e.encounter_id)
         Heap Blocks: exact=6
         ->  Bitmap Index Scan on idx_abc_log_encounter_id  (cost=0.00..35.86 rows=1506 width=0) (actual time=0.013..0.013 rows=1 loops=17)
               Index Cond: (encounter_id = e.encounter_id)
 Planning time: 0.361 ms
 Execution time: 0.478 ms
(10 rows)

我还要补充的是,对于一个长时间运行的查询,即使在2分钟后只返回250行,添加"LIMIT 100“也可以使查询立即返回。我调查了速度是否与查询返回的数据量有关,我没有看到任何明显的趋势。我不禁觉得Postgres错了(100倍?)关于它的哪种方法会更快。我在这里有什么选择?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-18 18:15:42

PostgreSQL对encounter的行数估计差了近10倍,我的第一个尝试是改善这一点。

为此,您可以更改列的统计目标:

代码语言:javascript
复制
ALTER TABLE abc.encounter ALTER customer_id SET STATISTICS 1000;

随后的ANALYZE将为该列收集更好的统计信息。如果1000还不够,请尝试10000。有了更好的行数估计,您就有更好的机会获得最佳计划。

如果与顺序扫描相比,重复索引扫描嵌套循环连接的成本仍然过高,您可以将参数random_page_cost从默认值4降低到更接近seq_page_cost (默认值1)的值。这将使PostgreSQL偏向于嵌套循环连接。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41706314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档