2014-11-01 55 views
-1

我有一个postgres数据库(9.3.3)与60k记录餐厅与每个有地址。如何找到Postgres的SQL瓶颈加入

为了看到地址的每家餐馆我参加这样的:

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" 
+0

你有表上的索引...? – 2014-11-01 22:00:12

+0

我想你有address.restaurant_id和restaurant.id上的索引? – Uhla 2014-11-01 22:00:33

+0

我在restaurants.id上有一个索引,但不是在addresses.restaurants_id上的索引?我如何重新索引该列? – dc10 2014-11-01 22:02:41

回答

-1

如果从字面上不会停止运行,您可能想检查是否存在使用该表的挂起会话。

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

+0

这就是我现在所做的,但即使在外键列上添加了索引,仍然可以运行30秒 – dc10 2014-11-01 22:23:23