我有一个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"
你有表上的索引...? – 2014-11-01 22:00:12
我想你有address.restaurant_id和restaurant.id上的索引? – Uhla 2014-11-01 22:00:33
我在restaurants.id上有一个索引,但不是在addresses.restaurants_id上的索引?我如何重新索引该列? – dc10 2014-11-01 22:02:41