SELECT property_properties.property_id FROM property_properties JOIN property_attributes as property_attributes_join1 ON property_properties.property_id = property_attributes_join1.property_id AND property_attributes_join1.attribute_group_id = 14 AND property_attributes_join1.attribute_value_id IN (58 ) JOIN property_towns ON property_properties.town_id = property_towns.town_id JOIN property_areas ON property_towns.area_id = property_areas.area_id JOIN property_regions ON property_areas.region_id = property_regions.region_id JOIN property_countries ON property_regions.country_id = property_countries.country_id AND property_countries.country_id IN ( 1 ) ORDER BY property_properties.price ASC ============================= EXPLAIN ANALYZE OUTPUT ============================= Sort (cost=5718.65..5718.65 rows=1 width=20) (actual time=24245.234..24263.632 rows=6337 loops=1) Sort Key: property_properties.price -> Nested Loop (cost=0.00..5718.64 rows=1 width=20) (actual time=938.816..24198.142 rows=6337 loops=1) -> Nested Loop (cost=0.00..5717.57 rows=1 width=24) (actual time=938.774..24046.018 rows=6337 loops=1) Join Filter: ("inner".area_id = "outer".area_id) -> Nested Loop (cost=0.00..5.20 rows=1 width=8) (actual time=0.146..5.758 rows=49 loops=1) Join Filter: ("inner".region_id = "outer".region_id) -> Seq Scan on property_regions (cost=0.00..1.24 rows=1 width=8) (actual time=0.034..0.078 rows=9 loops=1) Filter: (1 = country_id) -> Seq Scan on property_areas (cost=0.00..2.87 rows=87 width=8) (actual time=0.012..0.318 rows=89 loops=9) -> Nested Loop (cost=0.00..5712.25 rows=10 width=24) (actual time=0.115..466.696 rows=6926 loops=49) -> Nested Loop (cost=0.00..5653.47 rows=10 width=24) (actual time=0.095..279.323 rows=6926 loops=49) -> Index Scan using property_attributes_pkey on property_attributes property_attributes_join1 (cost=0.00..5593.22 rows=10 width=4) (actual time=0.055..75.591 rows=6926 loops=49) Index Cond: (attribute_group_id = 14) Filter: (attribute_value_id = 58) -> Index Scan using property_property_pkey on property_properties (cost=0.00..6.01 rows=1 width=24) (actual time=0.012..0.016 rows=1 loops=339374) Index Cond: (property_properties.property_id = "outer".property_id) -> Index Scan using property_towns_pkey on property_towns (cost=0.00..5.87 rows=1 width=8) (actual time=0.009..0.013 rows=1 loops=339374) Index Cond: ("outer".town_id = property_towns.town_id) -> Seq Scan on property_countries (cost=0.00..1.06 rows=1 width=4) (actual time=0.005..0.010 rows=1 loops=6337) Filter: (country_id = 1) Total runtime: 24284.502 ms