如何找到postgres sql join的瓶颈

时间:2014-11-01 21:58:34

标签: sql performance postgresql

我有一个postgres db(9.3.3)表,其中有6万个餐馆记录,每个都有一个地址。

为了看到每个有地址的餐馆,我都这样加入:

Select name,city 
from accommodations 
  inner join addresses on addresses.accommodations_id = accommodations.id

我一直认为这可能是最容易和最快速的加入,但它并没有停止运行。 那么这里有什么不对,要搜索什么。提前谢谢。

\d
List of relations
Schema  |        Name         |   Type   |  
----------+---------------------+----------
public   | cities              | table    | 
public   | cities_id_seq       | sequence | 
public   | geography_columns   | view     | 
public   | geometry_columns    | view     | 
public   | locations_id_seq    | sequence | 
public   | raster_columns      | view     | 
public   | raster_overviews    | view     |
public   | schema_migrations   | table    | 

public   | spatial_ref_sys     | table    | 
topology | layer               | table    | 
topology | topology            | table    | 
topology | topology_id_seq     | sequence | 
(17 rows)


"public.accommodations";"8232 kB"
"public.addresses";"19 MB"


"Merge Left Join  (cost=0.75..6748.33 rows=68647 width=674) (actual time=0.022..102.891 rows=66249 loops=1)"
"  Merge Cond: (accommodations.id = addresses.accommodation_id)"
"  Buffers: shared hit=9167"
"  ->  Index Scan using accommodations_pkey on accommodations  (cost=0.29..2377.71 rows=68647 width=560) (actual time=0.010..17.053 rows=66249 loops=1)"
"        Buffers: shared hit=1681"
"  ->  Index Scan using idx_addresses_accommodation_id on addresses  (cost=0.29..3370.89 rows=66250 width=114) (actual time=0.008..19.648 rows=66250 loops=1)"
"        Buffers: shared hit=7486"
"Total runtime: 108.642 ms"

1 个答案:

答案 0 :(得分:-1)

如果它真的不能停止运行,你可能想检查是否有使用该表的某些挂起的会话。

使用Python psycopg2时,我们遇到了类似的问题。一旦查询执行失败,下一次查询(无论查询的来源)将永远不会返回结果。重新启动postgres服务将解决它,并在Python中添加rollback子句查询失败可以防止再次发生这种情况。