2014-11-01 56 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