postgres: Joining a small table with large table

◇◆丶佛笑我妖孽 提交于 2019-12-25 07:48:57

问题


Postgres Server Version: server 9.1.9

explain analyze 
select * from A, B where A.groupid = B.groupid;
                                                                 QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------
 Merge Join  (cost=1.23..8.64 rows=2 width=104) (actual time=0.076..204.212 rows=3 loops=1)
   Merge Cond: (A.groupid = B.groupid)
   ->  Index Scan using A_pkey on A  (cost=0.00..68144.37 rows=1065413 width=88) (actual time=0.008..115.366 rows=120938 loops=1)
   ->  Sort  (cost=1.03..1.03 rows=2 width=16) (actual time=0.013..0.016 rows=3 loops=1)
         Sort Key: B.groupid
         Sort Method: quicksort  Memory: 25kB
         ->  Seq Scan on B  (cost=0.00..1.02 rows=2 width=16) (actual time=0.002..0.004 rows=3 loops=1)
 Total runtime: 204.257 ms
(8 rows)

Table A has 1 million+ rows. Table B has 3 rows. In actual production query, there are other where clauses on table A that reduce #of rows to 15k+, but still query takes more than 50ms and appears in our slow query logs.

Is there any way to improve performance here? I guess index scan on larger table is causing the slowness.

来源:https://stackoverflow.com/questions/21546608/postgres-joining-a-small-table-with-large-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!